MessageWindow not responding to SendMessage from Unmanaged code

  • Thread starter Thread starter Victor Winters
  • Start date Start date
V

Victor Winters

I am using instances of MessageWindow class throughout my application. It has worked flawlessly up to this point. Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If I create the same message using the Message class on the managed side and SendMessage with that, it works. Here is the code:

managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd, Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
MessageWindow.SendMessage(ref msg);

Thanks Victor
 
Are you sure you're sending messages to a valid
MessageWindow class? I've had almost the same scenario you
describe working with no problems.

-Alex
--
Alex Yakhnin, .NET CF MVP
-----Original Message-----
I am using instances of MessageWindow class throughout my
application. It has worked flawlessly up to this point.
Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If
I create the same message using the Message class on the
managed side and SendMessage with that, it works. Here is
the code:
managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd,
Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
 
Out of pure interest, how do you define WM_ACTIVATE in your managed code?
I am using instances of MessageWindow class throughout my application. It has worked flawlessly up to this point. Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If I create the same message using the Message class on the managed side and SendMessage with that, it works. Here is the code:

managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd, Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
MessageWindow.SendMessage(ref msg);

Thanks Victor
 
public const int WM_ACTIVATE = 0x0006;
But I never hit the first break point in WndProc, which tells me no message came in (I even tried other messages).
Out of pure interest, how do you define WM_ACTIVATE in your managed code?
I am using instances of MessageWindow class throughout my application. It has worked flawlessly up to this point. Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If I create the same message using the Message class on the managed side and SendMessage with that, it works. Here is the code:

managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd, Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
MessageWindow.SendMessage(ref msg);

Thanks Victor
 
Yes. At the end of the message I explained how I'm able to send the same
message to the MessageWindow from managed code. I found some code that
looks exactly like mine with one difference being initializing the Hwnd
variable in c++.

I currently do this:
HWND mMessageHandler;

I will change it to:
HWND mMessageHandler = NULL;
 
The first order of troubleshooting would be to launch CeSpy++ from eVC tools menu, find a window with the class name _NETCF_AGL_MSG_ and watch the messages that are sent to it. Of course you can also try finding the window by handle.
public const int WM_ACTIVATE = 0x0006;
But I never hit the first break point in WndProc, which tells me no message came in (I even tried other messages).
Out of pure interest, how do you define WM_ACTIVATE in your managed code?
I am using instances of MessageWindow class throughout my application. It has worked flawlessly up to this point. Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If I create the same message using the Message class on the managed side and SendMessage with that, it works. Here is the code:

managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd, Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
MessageWindow.SendMessage(ref msg);

Thanks Victor
 
Thanks for the help guys, I found the problem. It is on the c++ side. For some reason the hwnd is nullified after the RegisterMessageHandler routine ends. I have verified that I receive a valid Hwnd. I even sent a message to it and it worked. So I just have to figure out why the Hwnd nullifys after the routine ends.
I am using instances of MessageWindow class throughout my application. It has worked flawlessly up to this point. Here's my steps:
1. Create MessageWindow class.
2. Send Hwnd to unmanaged code (I have a dll I created in evc).
3. Save the Hwnd to a variable in the dll.
4. At some point the dll invokes SendMessage to this Hwnd.

The MessageWindow on the managed side never responds. If I create the same message using the Message class on the managed side and SendMessage with that, it works. Here is the code:

managed code (register mAppMessages (a MessageWindow) to unmanaged side)
Library.RegisterMessageHandler(mAppMessages.Hwnd);

unmanaged code saves Hwnd
HWND mMessageHandler;
__declspec( dllexport ) void RegisterMessageHandler(HWND hwnd)
{
mMessageHandler = hwnd;
}

unmanaged code sends message
SendMessage(mMessageHandler, WM_ACTIVATE, 9, 12345);

Nothing happens. But when I do this on the managed side it works
Message msg = Message.Create(mAppMessages.Hwnd, Library.WM_ACTIVATE, (IntPtr)9, (IntPtr)12345);
MessageWindow.SendMessage(ref msg);

Thanks Victor
 
Back
Top