Question

OK, this problem has been bugging me for a long time.

I have the code for the service which communicates a string to the client via PostMessage:

 Public Sub SendToClient(msgs As String, types As Integer, hwnd As Long)

        postMessage(hwnd, 0, Nothing, msgs)

    End Sub

Then I have the client that receives the string:

 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If (m.Msg = 0) Then
            Dim a as string
            a = Marshal.PtrToStringAuto(m.LParam)
           end if
        MyBase.WndProc(m)
    End Sub

However, the client sends an error, or some jumble of binary data or even just a blank string sometimes. By the way, the m.LParam is a number.

Can someone tell me what is the right way to send/receive strings via postmessage.

Était-ce utile?

La solution

I do it like this:

Public Sub SendMessageToApp(ByVal NombreVentana As String, ByVal Mensaje As String, ByVal sender As Form)
        Dim hWnd As IntPtr
        Dim mCopyData As COPYDATASTRUCT

        hWnd = CType(FindWindow(Nothing, NombreVentana), IntPtr)
        Dim message As New System.Text.StringBuilder

        If (CInt(hWnd) <> 0) Then

            message.Append(Mensaje)

            Dim pCopyData As IntPtr = Marshal.AllocHGlobal(message.Length() + 40)


            mCopyData.lpData = Marshal.StringToHGlobalAnsi(message.ToString)
            mCopyData.cbData = message.Length
            mCopyData.dwData = CType(_messageID, IntPtr)
            Marshal.StructureToPtr(mCopyData, pCopyData, False)

            SendMessage(hWnd, WM_COPYDATA, CInt(sender.Handle), pCopyData)
            Marshal.FreeHGlobal(mCopyData.lpData)
            Marshal.FreeHGlobal(pCopyData)

        End If
    End Sub

Receiver window:

Declarations and definitions:

 Const WM_COPYDATA As Integer = 74
    Const SIG_LENGTH As Integer = 36
    Const MAX_COPY_LENGTH As Integer = 128
    Const SigConnect As String = "F7B82657-BD18-4ee6-B182-78721293821C"
    Dim CDCount As Integer

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
    ByVal lParam As IntPtr) As Integer

    Dim hWndSender As Integer
    Private Const _messageID As Integer = 10

    'Estructura obligatoria para poder utilizar el API CopyData
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure COPYDATASTRUCT
        Public dwData As IntPtr
        Public cbData As Integer
        Public lpData As IntPtr
    End Structure

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = WM_COPYDATA Then
            Dim Estructura As COPYDATASTRUCT
            Dim tipo As Type = Estructura.GetType
            Dim message As String

            Estructura = CType(m.GetLParam(GetType(COPYDATASTRUCT)), COPYDATASTRUCT)

            'Here you get the message
            message = Marshal.PtrToStringAnsi(Estructura.lpData, Estructura.cbData)

        End If
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top