VBUC Documentation | Mobilize.Net
Mobilize.NetForumsBlogDocumentation Home
  • Mobilize.Net VBUC
  • Introduction
  • Install and Licenses
  • Get Started
  • Migration Guide
  • VBUC Features
  • Mappings Grammar
  • Generated Code
  • Upgrade Options
    • Data Access
    • Grids
    • Microsoft
    • Sheridan
    • Others
    • Code Conversion
    • C# Features
  • Third-party controls
  • Best Practices
  • EWIs
    • Warnings
    • Issues
    • ToDos
    • Notes
  • Issues and Troubleshooting
    • Microsoft.VisualBasic Uses
    • Safe and Unsafe Methods Layer
    • Third Party Components
    • Migration Process
    • Classic ADO Conversion to ADO.NET
    • VB6 and .NET integer division
    • VB6 On Error Statements
    • Running a .NET Core application in a machine with no Visual Studio installed
    • Databases issues
    • Unsupported assemblies on .NET Core and .NET 5
    • Icon Extraction Issues
    • HTTPS sites are not loaded in Windows XP
    • Short-Circuit Boolean Logic in C#
    • Assessment Report Issue
  • Knowledge Base
    • FAQ
      • Does the VBUC support the Sheridan VB 6.0 controls suit?
      • How effective is the Visual Basic Upgrade Companion tool at converting an application's front end?
      • What controls does the Visual Basic Upgrade Companion tool supports?
      • How does the VBUC handle VB6 Collections?
      • Can the VBUC migrate intrinsic VB6 functions and libraries?
      • Where is the source code for the support (helper) classes used by the VBUC?
      • How does the VBUC convert ADO to ADO.NET?
      • Can the Visual Basic Upgrade Companion tool be customized?
      • What are Stubs?
    • How-To
      • App.Config and Database Access
      • Avoid reflection in Hot Paths
      • Convert ESRI ArcGIS Visual Basic 6.0 applications to .NET
      • Drag and Drop Conversion Steps
      • Inserting elements in a ListView
      • String vs StringBuilder
      • Word Automation in VB6 vs .NET
      • Configure default Factory Database provider
      • GetPrivateProfileString Windows API working in migrated code
      • Upgrade projects with shared files
  • Release Notes
Powered by GitBook
On this page
  • Win-API
  • Code Organization
  • Looking for alternatives?

Was this helpful?

  1. Issues and Troubleshooting

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 VBUC (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

[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.

Example for the advapi32 class

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?

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

PreviousMicrosoft.VisualBasic UsesNextThird Party Components

Last updated 1 year ago

Was this helpful?

Microsoft provides an article with a list of and their mappings to .NET.

Windows API functions
PInvoke classes