How to get string in .NET from legacy StrPtr
At work we have a lot of legacy VB6 code. I am implementing custom
messaging between the old VB6 code and a new VB.NET project. I would like
to send the name property of a VB6 control and the name length via the
SendMessage api call in VB6 and assemble the string in the .NET project.
The code in VB6 looks like this:
SendMessage hwnd, WM_CONTROLNAME, StrPtr(sControlName),
CLng(Len(sControlName))
So I'm sending a custom windows message WM_CONTROLNAME to the hwnd of the
.NET form, with a wParam StrPtr of the string and a lParam with the length
of the string.
In the .NET project I have a message handler that calls a function GetText
to reassemble the string from the pointer and string length passed in the
message params:
Protected OverRides Sub WndProc(ByRef m as message)
If m.msg = WM_CONTROLNAME Then
p_sControlName = GetText(m.wParam, m.lParam)
Else
Mybase.WndProc(m)
End If
End Sub
This all works properly up to the point that GetText is supposed to return
the original string. I've tried to fill a StringBuilder with the
GetWindowText API call using the StrPtr and and length to no success. I've
also tried CopyMemory but that doesn't seem to be correct either. What is
the best way to take this pointer and string length and turn it into a
string in my GetText method? Thanks in advance.
No comments:
Post a Comment