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.
Wrapper allows for easy replacement of unmanaged APIs
This refactoring functionality will produce more readable and maintainable code
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 (usafe 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);
}