> For the complete documentation index, see [llms.txt](https://docs.gapvelocity.ai/vbuc/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gapvelocity.ai/vbuc/issues-troubleshooting/safe-and-unsafe-methods-layer.md).

# Safe and Unsafe Methods Layer

### Win-API

The Microsoft Windows application programming interface (Win API):

* Allows calls to Windows OS functions directly.
* All programs interact with the Windows API directly or through some other API.

Many system resources can be managed using this API.

Applications employ them to perform special functions not available from the programming language natively:

* Working with the Windows registry.
* Interacting directly with the user interface.

Implemented in several DLLs that reside on the Windows folder, such as kernel32.dll, advapi32.dll, user32.dll, etc.

These libraries contain what is called unmanaged code in .NET: code that is compiled directly to a binary that can be executed directly in the CPU.

The .NET platform provides the Platform Invocation Services also known as "PInvoke" to interact with unmanaged code from a managed environment.

The VB6 AI Migrator (migration tool) identifies all Windows API calls in the original VB6 code and translates them into their corresponding "PInvoke" signature:

**Original VB6 Code**

```
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
```

**Upgraded C# Code**

```csharp
[DllImport("advapi32.dll", EntryPoint = "RegQueryValueExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
extern public static int RegQueryValueEx(int hKey, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszValueName, int dwReserved, ref int lpdwType, System.IntPtr lpbData, ref int cbData);
```

### Code Organization

All the signatures found in the original code are organized by library, resulting in a class per library.

* The code is better organized.
* The original definitions are commented out.
* The required interoperability data-type marshalling is generated.
* The error handling is added for the upgraded API calls.
* The correct data types for pointer-type parameters are generated.

<figure><img src="/files/pITUfSH67UXEe6xnM66B" alt=""><figcaption><p>PInvoke classes</p></figcaption></figure>

**Example for the advapi32 class**

```csharp
namespace ProjectSupport.PInvoke.UnsafeNative {
  [System.Security.SuppressUnmanagedCodeSecurity]
  public static class advapi32 {
    [DllImport("advapi32.dll", EntryPoint = "RegQueryValueExA", 
    CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    extern public static int RegQueryValueEx(int hKey, 
    [MarshalAs(UnmanagedType.VBByRefStr)] ref string 
    lpszValueName, int dwReserved, ref int lpdwType, System.IntPtr lpbData, ref int cbData);
  }
}

namespace ProjectSupport.PInvoke.SafeNative {
  public static class advapi32 {
    public static int RegQueryValueEx(int hKey, ref string 
    lpszValueName, int dwReserved, ref int lpdwType, ref int lpbData, ref int cbData) {
      int result = 0;
      GCHandle handle = GCHandle.Alloc(lpbData, GCHandleType.Pinned);
      try {
        IntPtr tmpPtr = handle.AddrOfPinnedObject();
        CompendiaSupport.PInvoke.UnsafeNative.advapi32.RegQueryValueEx(hKey, ref lpszValueName, 
        dwReserved, ref lpdwType, tmpPtr, ref cbData);
        lpbData = Marshal.ReadInt32(tmpPtr);
      }
      finally {
        handle.Free();
      }
      return result;
    }
  }
}
```

### Looking for alternatives?

Microsoft provides an article with a list of [Windows API functions](https://docs.microsoft.com/en-us/previous-versions/dotnet/articles/aa302340\(v%3Dmsdn.10\)?redirectedfrom=MSDN) and their mappings to .NET.

In the summary, they explicitly say that: "This article identifies the Microsoft .NET Framework version 1.0 or 1.1 APIs that provide similar functionality to Microsoft Win32 functions."

The replacement of API functions with .NET alternatives should be done on a case-by-case basis.

* Imports of the required namespaces are needed.
* Can require modification on the method's logic.
* Creation of new objects
* Calling of the new method(s).
* How to retrieve the return value.

**Example for the RegQueryValueEx method:**

| Win32 function  | Description                                                                                  | .NET Framework API                   |
| --------------- | -------------------------------------------------------------------------------------------- | ------------------------------------ |
| RegQueryValueEx | Retrieves the type and data for a specified value name associated with an open registry key. | Microsoft.Win32.RegistryKey.GetValue |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gapvelocity.ai/vbuc/issues-troubleshooting/safe-and-unsafe-methods-layer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
