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
Upgraded C# Code
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
Looking for alternatives?
Microsoft provides an article with a list of Windows API functions 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:
Last updated