I need to use a Windows API function in my VB.NET application. I need to pass an ASCII string of text to that function. This string is stored as a zero-terminated byte array. What I currently do is I convert the byte array into a string and then I pass the string to the Windows API function.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String)
Dim b(20) As Byte
Dim bHandle As GCHandle = GCHandle.Alloc(b, GCHandleType.Pinned)
b(0) = 65
Dim h As Integer = SendMessage(cH, &HC, 0, System.Text.Encoding.ASCII.GetString(b))
What I want to do instead is to pass a pointer to the original byte array, to avoid creating a string instance.
The byte array is pinned.
I don't really understand marshalling yet.
Is that possible to do and how it can be done?
I think what you need is to pass address of the pinned array, not original string:
Dim h As Integer = SendMessage(cH, &HC, 0, bHandle.AddrOfPinnedObject())