Help with custom message broadcasting(Inter process communication)
-
Hi I have a service written in Visual C++ 6 that needs to interact with an application written in vb.net. In my service I have used : DWORD mymsg=RegisterWindowMessage("my_message") to register a unique custom message. I then use: BOOL b=SendNotifyMessage(HWND_BROADCAST,mymsg,NULL,NULL) to broadcast my message. However, I can't seem to be able to capture this message in the Windowproc function of my vb.net application. Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer Dim mymsg As Integer = RegisterWindowMessage("my_message") Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) if m.Msg=mymsg then --do something-- end if end sub Please help! :sigh: P.S.:I need to show a pop up in my .net application when it receives this message. The message values have all registered properly in both applications and are returned to be the same when I print them. Also the SendNotitfyMessage in my service returns a non zero value(supposed to indicate success). What do I do?? What am I doing wrong?
-
Hi I have a service written in Visual C++ 6 that needs to interact with an application written in vb.net. In my service I have used : DWORD mymsg=RegisterWindowMessage("my_message") to register a unique custom message. I then use: BOOL b=SendNotifyMessage(HWND_BROADCAST,mymsg,NULL,NULL) to broadcast my message. However, I can't seem to be able to capture this message in the Windowproc function of my vb.net application. Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Integer Dim mymsg As Integer = RegisterWindowMessage("my_message") Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) if m.Msg=mymsg then --do something-- end if end sub Please help! :sigh: P.S.:I need to show a pop up in my .net application when it receives this message. The message values have all registered properly in both applications and are returned to be the same when I print them. Also the SendNotitfyMessage in my service returns a non zero value(supposed to indicate success). What do I do?? What am I doing wrong?
The message registered with
RegisterWindowMessage
are valid only for a session. You cannot use this for your case since a service runs as a different user than the application. You can instead use a named event for your purpose. Use CreateEvent[^] to open/create an event both the service and the application. Now your application can wait on the event handle that will be set from the service. Just remember to give a name for the last parameter to the API. And also remember to prefix the name with"Global\"
. So you could typically you a name like"Global\my_message"
.«_Superman_» I love work. It gives me something to do between weekends.
-
The message registered with
RegisterWindowMessage
are valid only for a session. You cannot use this for your case since a service runs as a different user than the application. You can instead use a named event for your purpose. Use CreateEvent[^] to open/create an event both the service and the application. Now your application can wait on the event handle that will be set from the service. Just remember to give a name for the last parameter to the API. And also remember to prefix the name with"Global\"
. So you could typically you a name like"Global\my_message"
.«_Superman_» I love work. It gives me something to do between weekends.
Well, here's the thing. 1.I'm able to communicate from the app. to the service using the ControlService function but not the other way around. 2.You say that it is only valid for a session. But as you can see I've used RegisterWindowMessage in the service as well as app.(to get the same SYSTEMWIDE unique -that's what msdn says- value for my message both inside the service and the app.) So if the app is reloaded from another user's context the app will gather the value of the message again. 3.I've even had the values displayed in both my service and app and they are the same. Thus the message is definitely unique and visible to both. 4.I even printed a log of all the messages recvd by wndproc of the app but the broadcasted msg never reaches the wndproc despite being successfully broadcasted from the service. As for your idea of using named events, I did that and again the same issue was encountered. The service says that the event was set but the application never captures the event. Just to confirm, while using CreateEvent I still do need to follow the escaping conventions right? i.e I used CreateEvent(NULL,0,0,"Global\\my_message") in the service and CreateEvent(Nothing,0,0,"Global\my_message") in the app.(since vb.net doesn't need to escape backslash) Here's the code: HANDLE MY_EVENT=CreateEvent(NULL,false,false,"Global\\my_message"); SetEvent(MY_EVENT); In vb.net: Const SYNCHRONIZE As Integer = &H100000L Dim MY_EVENT As Integer Private Declare Function OpenEvent Lib "kernel32" Alias "OpenEventA" (ByVal dwDesiredAccess As Long, ByVal inheritHandle As Integer, ByVal lpName As String) As Integer Private Declare Function WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer Sub New() InitializeComponent() Me.TopLevel = True Try my_event_thread = New Thread(AddressOf MyEventWatcher) Catch ex As Exception MsgBox("Error in initializing! " + ex.Message) Application.Exit() End Try End Sub Private Sub MyEventWatcher() Try MY_EVENT = OpenEvent(SYNCHRONIZE, 1, "Global\my_message") While True WaitForSingleObject(MY_EVENT, INFINITE) MsgBox("Done") End While Catch ex As Exception MsgBox("Exception:" + ex.Message) End Try End Sub What should I do now?Any ideas?:confused: Thanks for the help! <
-
The message registered with
RegisterWindowMessage
are valid only for a session. You cannot use this for your case since a service runs as a different user than the application. You can instead use a named event for your purpose. Use CreateEvent[^] to open/create an event both the service and the application. Now your application can wait on the event handle that will be set from the service. Just remember to give a name for the last parameter to the API. And also remember to prefix the name with"Global\"
. So you could typically you a name like"Global\my_message"
.«_Superman_» I love work. It gives me something to do between weekends.
Hi thanks for all the advice. I've managed to take care of it. I had to make the service interactive in order to make my application visible to it and I found no other way of making it visible due to the different user contexts. So you were right:) But one thing that I haven't quite understood is, why didn't events work? I mean a "Global" event should not be hindered by contexts/sessions right? But even the global events raised by the service were not visible to my application. Can you explain?:confused:
-
Hi thanks for all the advice. I've managed to take care of it. I had to make the service interactive in order to make my application visible to it and I found no other way of making it visible due to the different user contexts. So you were right:) But one thing that I haven't quite understood is, why didn't events work? I mean a "Global" event should not be hindered by contexts/sessions right? But even the global events raised by the service were not visible to my application. Can you explain?:confused:
I'm not exactly sure why global names with events didn't work. I'm sure it should work because I have done the exact same thing to communicate between a service and an application.
«_Superman_» I love work. It gives me something to do between weekends.
-
I'm not exactly sure why global names with events didn't work. I'm sure it should work because I have done the exact same thing to communicate between a service and an application.
«_Superman_» I love work. It gives me something to do between weekends.
hmm..and did you make your service interactive or not? By the way, did you look at the code that I had posted, the one using global events? Just to be sure I did it right.