astankovic
asked on
Sending a Windows Message in DLL
Hi,
I have the following code to send a custom message to an application. This code is in a exported DLL function that is called by the system.
But the message never gets sent. What am I doing wrong?
WINLOGONDLL_API UINT SendTestMessage()
{
UINT iMsg = RegisterWindowMessage(MY_M ESSAGE_UNL OCK);
SendMessage(hWnd, iMsg, NULL, NULL);
return iMsg;
}
MSDN says following gor SendMessage()
"The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling."
Do I need to do custom marshaling? How do i do that?
Thanks
Alex
I have the following code to send a custom message to an application. This code is in a exported DLL function that is called by the system.
But the message never gets sent. What am I doing wrong?
WINLOGONDLL_API UINT SendTestMessage()
{
UINT iMsg = RegisterWindowMessage(MY_M
SendMessage(hWnd, iMsg, NULL, NULL);
return iMsg;
}
MSDN says following gor SendMessage()
"The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling."
Do I need to do custom marshaling? How do i do that?
Thanks
Alex
ASKER
Hi, thanks for your comment. Yes, I checked the RegisterWindowMessage(), I get a value of 49946
I'm now trying to verify if I have a valid handle. I have another exported DLL function:
// hWnd is global variable that gets passed to SendMessage()
WINLOGONDLL_API HWND SetWindow(HWND appWindow)
{
hWnd = appWindow;
return hWnd;
}
This function is being called by a C# app when it starts. As you suggested, I think there is something wrong going in that part.
In C# app i do following:
MessageBox.Show(this.Handl e.ToString ()); //window handle
MessageBox.Show(SetWindow( this.Handl e).ToStrin g()); //window handle returned by the DLL function
But they both show different values. I'm not sure if that's a correct way to inspect HWND... I'm not sure what type is HWND, integer, long?
Thanks for you help,
Alex
I'm now trying to verify if I have a valid handle. I have another exported DLL function:
// hWnd is global variable that gets passed to SendMessage()
WINLOGONDLL_API HWND SetWindow(HWND appWindow)
{
hWnd = appWindow;
return hWnd;
}
This function is being called by a C# app when it starts. As you suggested, I think there is something wrong going in that part.
In C# app i do following:
MessageBox.Show(this.Handl
MessageBox.Show(SetWindow(
But they both show different values. I'm not sure if that's a correct way to inspect HWND... I'm not sure what type is HWND, integer, long?
Thanks for you help,
Alex
>>I'm not sure what type is HWND, integer, long?
A HWND is a void* - try to output the HWND's value this way:
WINLOGONDLL_API HWND SetWindow(HWND appWindow)
{
#ifdef _DEBUG
char scBuf [ 256];
wsprintf ( acBuf, "HWND: 0x%8.8x\n", hWnd);
OutputDebugString ( acBuf);
#endif
hWnd = appWindow;
return hWnd;
}
WINLOGONDLL_API UINT SendTestMessage()
{
#ifdef _DEBUG
char scBuf [ 256];
wsprintf ( acBuf, "HWND: 0x%8.8x\n", hWnd);
OutputDebugString ( acBuf);
#endif
UINT iMsg = RegisterWindowMessage(MY_M ESSAGE_UNL OCK);
SendMessage(hWnd, iMsg, NULL, NULL);
return iMsg;
}
DebugView (http://www.sysinternals.com/ntw2k/freeware/debugview.shtml) is a great tool for displaying such debug messages.
A HWND is a void* - try to output the HWND's value this way:
WINLOGONDLL_API HWND SetWindow(HWND appWindow)
{
#ifdef _DEBUG
char scBuf [ 256];
wsprintf ( acBuf, "HWND: 0x%8.8x\n", hWnd);
OutputDebugString ( acBuf);
#endif
hWnd = appWindow;
return hWnd;
}
WINLOGONDLL_API UINT SendTestMessage()
{
#ifdef _DEBUG
char scBuf [ 256];
wsprintf ( acBuf, "HWND: 0x%8.8x\n", hWnd);
OutputDebugString ( acBuf);
#endif
UINT iMsg = RegisterWindowMessage(MY_M
SendMessage(hWnd, iMsg, NULL, NULL);
return iMsg;
}
DebugView (http://www.sysinternals.com/ntw2k/freeware/debugview.shtml) is a great tool for displaying such debug messages.
ASKER
Ok, that found the error. I think I'm missing some DLL fundamentals. Does each application that loads the DLL gets a separate "copy" of DLL? It seems that it does..
Because, i was setting hWnd from one application (C#), but calling SendTestMessage() from another (VB 6 for testing purposes)
So when I call SendTestMessage() from VB 6 app, the hWnd 0x00000000.
But when i call both SetWindow() and SendTestMessage() from C# app, hWnd has the same value in both functions.
So the question now i guess is how to set hWnd from one app, and use it in a call from another?
Thanks you for pointing me to DebugView, it is really great.
Alex
Because, i was setting hWnd from one application (C#), but calling SendTestMessage() from another (VB 6 for testing purposes)
So when I call SendTestMessage() from VB 6 app, the hWnd 0x00000000.
But when i call both SetWindow() and SendTestMessage() from C# app, hWnd has the same value in both functions.
So the question now i guess is how to set hWnd from one app, and use it in a call from another?
Thanks you for pointing me to DebugView, it is really great.
Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW, see als http://support.microsoft.com/default.aspx?kbid=125677 ("HOWTO: Share Data Between Different Mappings of a DLL")
As you are not passing anything in the parameters: No. Are you sure the window handle is valid? Have you checked the return value of 'RegisterWindowMessage()'?