VB to .NET

Advanced Windows APIs conversion with refactoring

Redundant API declarations will be converted to a single declaration, in an API declarations project.
All uses of these APIs will be redirected to this single declaration
The VBUC creates a wrapper for each unmanaged API declaration.
Using this Wrapper files allows for easy replacement of unmanaged APIs
This refactoring functionality will produce more readable and maintainable code

For example if you had a VB6 project with a couple of forms like the following:

Screenshot of project form folder

When this project is migrated using the VBUC it will generate a Visual Studio Solution like the following:

Solution Explorer tab Screenshot

Notice that a new project is added. In the lastest version of the VBUC this project will usually be called with the same name as your VBUC solution + Support. So for example if your solution name is UpgradeSolution1 a folder called UpgradeSolution1Support will be created with a project with the same name.
Two subfolder will be created:

  • PInvokeSafeMethods:
  • PInvokeUnSafeMethods

Inside those folder a file will be created grouping PInvoke calls per DLL. If your API calls were for example to user32.dll then a file with that name will generated and all PInvoke calls to that DLL will be arranged inside that file.

Below you can see an example of how the code is upgraded.

VB6 Code

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

Private Sub Command1_Click()
  Dim titleForm1 As String
  titleForm1 = String(GetWindowTextLength(Me.hwnd) + 1, Chr$(0))
   GetWindowText Me.hwnd, titleForm1, Len(titleForm1)
End Sub

.NET Code

File: user32.cs (safe methods)
public static class user32
{
public static int GetWindowText(int hwnd,ref string lpString, int cch)
  {
  return WinAPI.UnsafeNative.user32.GetWindowText(hwnd,ref lpString,
    cch);
  }
  public static int GetWindowTextLength( int hwnd)
  {
   return WinAPI.UnsafeNative.user32.GetWindowTextLength(hwnd);
  }
}

File: user32.cs (unsafe methods) [System.Security.SuppressUnmanagedCodeSecurity]
public static class user32
{

[DllImport("user32.dll", EntryPoint = "GetWindowTextA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
extern public static int GetWindowText( int hwnd, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpString, int cch);

[DllImport("user32.dll", EntryPoint = "GetWindowTextLengthA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
extern public static int GetWindowTextLength( int hwnd); }

File: Form1.cs
private void Command1_Click(Object eventSender, EventArgs eventArgs)
{
  string titleForm1 = new string(Strings.Chr(0));
  GetWindowTextLength(this.Handle.ToInt32()) + 1);
  GetWindowText(this.Handle.ToInt32(), ref titleForm1,
  titleForm1.Length);
}
Talk To An Engineer