Post text message

DPrometheus

Regular
Joined
Jan 7, 2009
Messages
50
Location
The Netherlands
Hey there

For the several applications rolled out right now we want a single module to handle inter-application communication. i.e. we have one application (called App1 for simplicity) and with a button in this application we switch to another application (called App2). we want to exchange information such as patient number etc.
My guess was to use Postmessage / sendmessage for this. All works fine, until we got to the point in which we have to communicate strings instead of numbers. An integer was easy to convert to a Intptr with

new IntPtr(intValue)

however creating an intptr from a string and send this pointer won't work.
We also saw there was an overloaded sendmessage with a stringbuilder, but the receiving application receives other characters then those which were sent. As a third we tried to dissassemble the string and send it as bytes. as in this solution. While this solution works my co-workers ain't happy with such a kind of a work-a-round. if we can't send the string this project will come to an end, and we are bound to use an even more cumbersome solution, namely something with an invisible button, which we can change its caption with a call to SetWindowText. Thereafter on a text change we can read the string and dependent on some codes we can trace back what kind of 'message' is posted.

I'm definitely convinced we shouldn't go for this, but if we can't find a way to send strings with a postmessage or sendmessage, I have no other option then to give in. :p

Does someone know if it can be achieved in .NET? WM_SETTEXT is capable of sending strings, so somehow it should be possible in my opinion. Also in native code (where I program in @home) this ain't a problem, and is definitely possible.

Someone any suggestions or tips?
Maybe it's just me who's stubborn, I just want to know what's best to do, and if possible know how to send strings using messages.

thanks in advance

~DP
 
It seems odd to me that he string builder doesn't work. Can you post the code that sends/processes the message?

Maybe you could allocate some global memory to store the string in, and then send the handle to that memory to the target app. The target app would be responsible for releasing the memory. It appears you could do this with the API methods GlobalAlloc, GlobalLock, GlobalUnlock, anu GlobalFree.
 
Also see this post

' Import
Code:
 ' Post message to handle
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage( _
     ByVal hWnd As IntPtr, _
     ByVal Msg As UInteger, _
     ByVal wParam As IntPtr, _
     ByVal lParam As IntPtr) As Boolean
    End Function

<DllImport("user32.dll")> _
    Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _
   ByVal wParam As IntPtr, ByVal lParam As StringBuilder) As IntPtr
    End Function

' Message registration .. both app's

Code:
CONSTANTS.MSG_STRINGWISSEL = RegisterWindowMessage("StringWissel")


sending app
Code:
SendMessage(hWnd, CInt(CONSTANTS.MSG_STRINGWISSEL), Nothing, New StringBuilder("TEST PATIENT"))

Receiving app
Code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        ' Select appropriate message
        Select Case m.Msg
            ' Case we want to switch patients
            Case CInt(CONSTANTS.MSG_PATIENTWISSEL)
                Try
                    'Activate this screen
                    Me.Activate()
                    Dim ptr As IntPtr = m.WParam
                    Dim patNr As Integer = ptr.ToInt32
                    ' Change patient
                    ChangePatient(patNr)
                Catch ex As Exception
                    ' In case of errors list them to the user
                    MsgBox(ex.Message)
                End Try

                ' Case we want to send text
            Case CInt(CONSTANTS.MSG_STRINGWISSEL)
                ' Add received data to string object
                ' _BS.BuildString(m.LParam)
                Dim b As StringBuilder
                b = CType(m.GetLParam(GetType(StringBuilder)), StringBuilder)
                Me.Text += " : " + b.ToString()
                'Me.Text += " : " + m.LParam.ToString()
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub

Although I feel the Alloc could work as well, Allocation forces us to an exception for receiving string data. As well as strings could be send to (third party) applications which (might) not do anything with the string, thus leaving it into memory.
 
Last edited:
Back
Top