0

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.

EDIT: This is the code in the the .NET wndProc override that successfully gets my string. Per the answerer's suggestion I used WM_COPYDATA. After passing the structure by reference in SendMessage from VB6, I marshal it back to the structure in .NET. The string pointer in the structure is now valid in VB.NET and I marshal it back to a string.

    If m.Msg = WM_COPYDATA Then
      Dim StringInfo As cds = New cds
      Dim sControlName As String = String.Empty
      StringInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(cds)), cds)
      sControlName = Marshal.PtrToStringAnsi(StringInfo.lpData, StringInfo.cbData)
    End If
4

1 回答 1

2

您必须记住,指针的地址是相对于特定进程的。因此,您发送的指针(它只是一个整数)对接收进程没有任何意义(它只是一个不相关内存的随机地址)。

您应该WM_COPYDATA改用:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649011%28v=vs.85%29.aspx

于 2013-09-14T06:52:47.410 回答