Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Hey there

 

I'm trying to send some messages to another program.

Somehow they never arrive.

 

The sending application on button click:

Dim info As New ProcessStartInfo("C:\Users\mso\Documents\Visual Studio 2005\Projects\SwitchApplication\MedicalRecords\bin\Debug\MedicalRecords.exe")
           Dim process As Process
           process = process.Start(info)
           ' Wait for process to be started
           Threading.Thread.Sleep(1500)
           hWnd = process.MainWindowHandle

           Dim Buffer As New StringBuilder(128)
           Buffer.Append(ComboBox1.SelectedValue.ToString)
           MsgBox(Buffer.ToString & "Sent to " & hWnd.ToString)
           SendMessage(hWnd, MSG_PATIENTWISSEL, Nothing, Buffer)

 

The ' receiving' application

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
       Select Case m.Msg
           Case CONSTANTS.MSG_PATIENTWISSEL
               Me.Activate()
               _patNr.Append(m.GetLParam( GetType(System.Text.StringBuilder)))
               System.Threading.Thread.Sleep(1500)
               Me.Text += ":" & _patNr.ToString

               MsgBox(_patNr.ToString & " received!")
           Case Else
               MyBase.WndProc(m)
       End Select
   End Sub

Private Sub MedicalRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       ' Shows right handle
       Me.Text = Me.Handle
   End Sub

 

however the handles correspond to eachother, so I'm pretty sure it's the right handle. Also the CONSTANTS.MSG_PATIENTWISSEL constant matches on both applications. The Send messagebox shows up, but the receive messagebox doesn't.

 

Anybody has an idea of what I'm doing wrong?

 

thanks,

~ DP

 

EDIT:

If I put a System.Diagnostics.Debugger.Break() call between

Case CONSTANTS.MSG_PATIENTWISSEL

and

Me.Activate()

the program crashes. (As there is no debugger attached to the second application)

 

Weird thing is, that it does reach this code. Although the text variable of the form doesn't change, as well as the messagebox seem to be refusing to pop up.

 

The receiving application also takes focus when the message is actually sent.

Edited by DPrometheus

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Posted (edited)

* The focus could also be a result of the call to

process.Start(info)

 

I found a little bit more information.. Apparently you have to register your custom messages and can't just use identical numbers to identify messages.

However problem ain't solved yet. Since all messages receive the value 0.

 

I changed the SendMessage declaration to

Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, _

ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr

instead of a StringBuilder as lParam.. However if some one has an idea of how I can use the stringbuilder I'm all ears.

 

Then I sent the message like:

SendMessage(hWnd, CInt(MSG_PATIENTWISSEL), Nothing, CType(ComboBox1.SelectedValue.ToString.Trim, IntPtr))

 

and receive it like this:

Case CONSTANTS.MSG_PATIENTWISSEL
               Try
                   Me.Activate()
                   _patNr.Append(m.GetLParam(GetType(IntPtr)).ToString)
...

 

At the load event I register MSG_PATIENTWISSEL as

CONSTANTS.MSG_PATIENTWISSEL = RegisterWindowMessage("PatientWissel")

 

Still I receive '0' as nr. instead of a 9 digit number what I sent.. ???

 

 

SOLVED:

The problem was in converting IntPtr to Integers and lParam used byref instead of wParam, so used that one instead.

 

' Receive:

Dim ptr As IntPtr = m.WParam

Dim patNr As Integer = ptr.ToInt32

 

' Send:

SendMessage(hWnd, CInt(MSG_PATIENTWISSEL), New IntPtr(CInt(ComboBox1.SelectedValue.ToString.Trim)), Nothing)

Edited by DPrometheus

My Development System

Intel Core i7 920 @2.66Ghz

6 GB DDR3 SDRAM

Windows 7 Ultimate x64 & Windows Vista Home Premium x64 dual boot

GeForce GTX295 1.8 GB

3.5 TB HD

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...