Postmessage from a win32 dll to MFC application

Hi Folks,

I am trying to PostMessage() from a win32 dll to a MFC appliacation , butthe message is not reaching the application (I am handling this message in CMainframe class) .. I am in th edebug mode and I get an exception in IsKindOf() function , so the message is in the MFC area.

What could be the problem with teh postmessage .

Please help ..

Regards,
JLD
[405 byte] By [justlikedat] at [2007-11-20 11:31:44]
# 1 Re: Postmessage from a win32 dll to MFC application
May we see the code?
cilu at 2007-11-10 22:25:12 >
# 2 Re: Postmessage from a win32 dll to MFC application
I am extremely sorry cilu, can not upload the files . I have used many threads inside my win32 dll which fill a structure and I send te hpointer to that structre as lparam . Is that a probblem and the size of the structure is around 1000 bytes.
justlikedat at 2007-11-10 22:26:13 >
# 3 Re: Postmessage from a win32 dll to MFC application
I did not ask you to upload the project file. Just posting the relevant code. Structure size has no influence. Where is the exception thrown from?
cilu at 2007-11-10 22:27:16 >
# 4 Re: Postmessage from a win32 dll to MFC application
I have used many threads inside my win32 dll which fill a structure and I send te hpointer to that structre as lparam . Is that a probblem and the size of the structure is around 1000 bytes.While sending/posting from threads you must not use MFC classes - the MFC is not thread-safe for window classes. Also while sending pointers between threads you must ensure the pointed memory is alive (not destructed by posting thread) while message handling.

Dll has nothing common with your problem - it's all about threads.
Igor Vartanov at 2007-11-10 22:28:21 >
# 5 Re: Postmessage from a win32 dll to MFC application
You mean to say I should not send/post from win32 dll to mfc application ? What may be the other way around ? and I have taken care the life of teh pointers , I will not delete it untill I copy it to my application memory.
justlikedat at 2007-11-10 22:29:20 >
# 6 Re: Postmessage from a win32 dll to MFC application
And the exception is thrown soon after I postmessge from the dll . Interesting point I observed is sometimes I am able to recieve the message in my UI (may be while I am debugging it) . Delay caused is helping here(?) , anyways its not true always.
justlikedat at 2007-11-10 22:30:19 >
# 7 Re: Postmessage from a win32 dll to MFC application
Igor meant not sending objects containing MFC data or derived from MFC classes. For that you need an MFC extension DLL.
cilu at 2007-11-10 22:31:23 >
# 8 Re: Postmessage from a win32 dll to MFC application
No I dont have any data containing MFC objects expect the HWND of teh CMainframe which I store in the dll , using an exported function from the dll. and the data which I send is a pointer to the normal C structure .
justlikedat at 2007-11-10 22:32:21 >
# 9 Re: Postmessage from a win32 dll to MFC application
... and the data which I send is a pointer to the normal C structure .Could it happen then this structure goes out of scope (and so the pointer points in the wrong memory) in the moment the posted message being processed?
VictorN at 2007-11-10 22:33:19 >
# 10 Re: Postmessage from a win32 dll to MFC application
You mean to say I should not send/post from win32 dll to mfc application ? No. Man, are you listening to me? Forget the dll-to-mfc thing. It's not what you should think about.

You may send/post from a thread different from main GUI thread, but do not use MFC CWin-derived classes inside working threads for this purpose. Instead, use plain Win32 API functions that are perfectly thread safe.
Igor Vartanov at 2007-11-10 22:34:24 >
# 11 Re: Postmessage from a win32 dll to MFC application
No I dont have any data containing MFC objects expect the HWND of teh CMainframe which I store in the dll , using an exported function from the dll. and the data which I send is a pointer to the normal C structure .Believe me, if you use plain Win32 API and take care about keeping structure alive during message handling, all should work fine. But since it doesn't, you definitely fail to follow some of these rules properly.
Igor Vartanov at 2007-11-10 22:35:28 >
# 12 Re: Postmessage from a win32 dll to MFC application
And the exception is thrown soon after I postmessge from the dll . Interesting point I observed is sometimes I am able to recieve the message in my UI (may be while I am debugging it) . Delay caused is helping here(?) , anyways its not true always.Since you are using threads, and you've noticed that you have some sporatic success, the issue seems to be a synchronization problem. When you post a message from one thread to another, you have no guarantee when the message will be processed by the receiving thread. As such, if the message contains a pointer to some data on the originating thread, how are you insuring that the data hasn't gone out of scope when the receiving thread is accessing it or that the originating thread isn't also accessing the data? This sounds like a classic [lack of] thread synchronization problem.
Arjay at 2007-11-10 22:36:20 >
# 13 Re: Postmessage from a win32 dll to MFC application
I will have a look at the synchronization problem , meanwhile as victor said if the pointer is out of scope while being processed then how to make sure that it is not ?
justlikedat at 2007-11-10 22:37:21 >
# 14 Re: Postmessage from a win32 dll to MFC application
Could it happen then this structure goes out of scope (and so the pointer points in the wrong memory) in the moment the posted message being processed?

I will have a look at the synchronization problem , meanwhile as victor said if the pointer is out of scope while being processed then how to make sure that it is not ?
justlikedat at 2007-11-10 22:38:27 >
# 15 Re: Postmessage from a win32 dll to MFC application
Could you tell me who loads the dll? Is it that MFC application or is there some other application, which loads the dll?
visharad at 2007-11-10 22:39:24 >
# 16 Re: Postmessage from a win32 dll to MFC application
Another surprising thing I observed is even If I dnt send a postmessage I get an exception @ isKindOf function ..
Its after WaitForSingleObject() in my thread is executed . I am unable to crack it beyond that :(
justlikedat at 2007-11-10 22:40:28 >
# 17 Re: Postmessage from a win32 dll to MFC application
Show your code prodicing "an exception ".
VictorN at 2007-11-10 22:41:25 >
# 18 Re: Postmessage from a win32 dll to MFC application
HANDLE SocketRead = GetEventID();
//------waiting till the event handler is ready
while(1)//----will return the event handler
{
WaitForSingleObject(SocketRead,INFINITE);

After this I get the exception .. but for the first time it does not throw the exception , after I process teh data and send the postmessage it comes back to this thread and produces the havoc.
justlikedat at 2007-11-10 22:42:36 >
# 19 Re: Postmessage from a win32 dll to MFC application
Could you tell me who loads the dll? Is it that MFC application or is there some other application, which loads the dll?
I load the dll from MFC application ..
justlikedat at 2007-11-10 22:43:27 >
# 20 Re: Postmessage from a win32 dll to MFC application
... I get an exception in IsKindOf() function , so the message is in the MFC area.

HANDLE SocketRead = GetEventID();
//------waiting till the event handler is ready
while(1)//----will return the event handler
{
WaitForSingleObject(SocketRead,INFINITE);
After this I get the exception ..Well, where "After this"?

Dear justlikedat, the "code" snippet you have posted has nothing to do with the "IsKindOf() function" exception " in the MFC area" :mad:
Please, either provide the actual information or don't more expect any useful help. :cool:
VictorN at 2007-11-10 22:44:29 >
# 21 Re: Postmessage from a win32 dll to MFC application
I am extremely sorry but the exception is thrown inside the BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const function .. It is called by the framework not me :(

I can paste the callstack if guys are interested.
justlikedat at 2007-11-10 22:45:31 >
# 22 Re: Postmessage from a win32 dll to MFC application
I am extremely sorry but the exception is thrown inside the function .. It is called by the framework not me :( :D
I am glad you already understand it! But it is called to prove one of your MFC objects! Which one? Why don't you want to show us this object? :confused: :confused: :confused: :confused:

I can paste the callstack if guys are interested.You should have done it in your OP! :(
And don't forget to post a couple of lines after WFSO call!
VictorN at 2007-11-10 22:46:40 >
# 23 Re: Postmessage from a win32 dll to MFC application
The callstack list I get after I get the exception .

> mfc80ud.dll!CObject::IsKindOf(const CRuntimeClass * pClass=0x782e6768) mfc80ud.dll!CFrameWnd::GetActiveView() Line 1176 + 0x20 bytes C++
mfc80ud.dll!CFrameWnd::OnActivateTopLevel(unsigned int wParam=1, long lParam=1243856) Line 947 + 0x17 bytes C++
mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=878, unsigned int wParam=1, long lParam=1243856, long * pResult=0x0012f8d4) Line 2004 + 0x11 bytes C++
mfc80ud.dll!CWnd::WindowProc(unsigned int message=878, unsigned int wParam=1, long lParam=1243856) Line 1741 + 0x20 bytes C++
mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x00359670, HWND__ * hWnd=0x006a1028, unsigned int nMsg=878, unsigned int wParam=1, long lParam=1243856) Line 240 + 0x1c bytes C++
mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x006a1028, unsigned int nMsg=878, unsigned int wParam=1, long lParam=1243856) Line 389 C++
mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x006a1028, unsigned int nMsg=878, unsigned int wParam=1, long lParam=1243856) Line 407 + 0x15 bytes C++
.
.Assembly
.
mfc80ud.dll!CWnd::SendMessageW(unsigned int message=878, unsigned int wParam=1, long lParam=1243856) Line 42 + 0x42 bytes C++
mfc80ud.dll!_AfxHandleActivate(CWnd * pWnd=0x00359670, unsigned int nState=1, CWnd * pWndOther=0x00000000) Line 181 C++
mfc80ud.dll!CWnd::OnWndMsg(unsigned int message=6, unsigned int wParam=1, long lParam=0, long * pResult=0x0012fc34) Line 1778 C++
mfc80ud.dll!CWnd::WindowProc(unsigned int message=6, unsigned int wParam=1, long lParam=0) Line 1741 + 0x20 bytes C++
mfc80ud.dll!AfxCallWndProc(CWnd * pWnd=0x00359670, HWND__ * hWnd=0x006a1028, unsigned int nMsg=6, unsigned int wParam=1, long lParam=0) Line 240 + 0x1c bytes C++
mfc80ud.dll!AfxWndProc(HWND__ * hWnd=0x006a1028, unsigned int nMsg=6, unsigned int wParam=1, long lParam=0) Line 389 C++
mfc80ud.dll!AfxWndProcBase(HWND__ * hWnd=0x006a1028, unsigned int nMsg=6, unsigned int wParam=1, long lParam=0) Line 407 + 0x15 bytes C++
.
.Assembly
.
mfc80ud.dll!AfxInternalPumpMessage() Line 153 + 0x13 bytes C++
mfc80ud.dll!CWinThread::PumpMessage() Line 896 C++
mfc80ud.dll!CWinThread::Run() Line 625 + 0xd bytes C++
mfc80ud.dll!CWinApp::Run() Line 889 C++
mfc80ud.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, wchar_t * lpCmdLine=0x00020892, int nCmdShow=1) Line 47 + 0xd bytes C++
UI.exe!wWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, wchar_t * lpCmdLine=0x00020892, int nCmdShow=1) Line 29 C++
UI.exe!__tmainCRTStartup() Line 578 + 0x35 bytes C
UI.exe!wWinMainCRTStartup() Line 403 C

.
justlikedat at 2007-11-10 22:47:40 >
# 24 Re: Postmessage from a win32 dll to MFC application
Dear Victor,
Thanks for all your help , I knw its really frustrating if you dont have enough info to find a solution to teh problem .
Please keep asking me about the info you need I ll try best of my knowledge to provide it.

As this exception is thrown from the dll which is a win32 dll there is no question of any mfc objects joinning the party . I would have said CMainfrm object if I had sent a postmessage, but this exception occurs even when I dont send any data to my application from the dll.
justlikedat at 2007-11-10 22:48:33 >
# 25 Re: Postmessage from a win32 dll to MFC application
mfc80ud.dll!CWnd::SendMessageW(unsigned int message=878, unsigned int wParam=1, long lParam=1243856) Line 42 + 0x42 bytes C++
What window do you send this message to? How did you get/obtain this window handle?
mfc80ud.dll!CObject::IsKindOf(const CRuntimeClass * pClass=0x782e6768) mfc80ud.dll!CFrameWnd::GetActiveView() Line 1176 + 0x20 bytes C++
Take a look at the code around the "CFrameWnd::GetActiveView() Line 1176 + 0x20 bytes". What do you see there?
VictorN at 2007-11-10 22:49:33 >
# 26 Re: Postmessage from a win32 dll to MFC application
All the calls to the functions have been done by the framework , and all the functions are called or invoked to display the GUI (doc/view).
1. I initialize my application and the dll. my GUI is idle now
2. I get the data from the socket and in my dll I process it , try to send it to the GUI .
3. now if the data is relevent I postmessage it to the GUI else not .

After this if the dll does not get any data then the thread waiting for the data will enter suspended mode .

If I minimize and maximize my application it behaves just like it should , but if I minimize my GUI and run my thread(dll) and try to maximize it I get the exception . so the root cause is in displaying/activating the GUI after my dll/thread is executed . but I have left it to the framework , I have not handled this situation .

Is it because the control is not being transferred to my main GUI thread ?
justlikedat at 2007-11-10 22:50:38 >
# 27 Re: Postmessage from a win32 dll to MFC application
3. now if the data is relevent I postmessage it to the GUI else not .This is the problem. How large is this data? What form does this data arrive in (a string, a structure, etc.)? I ask so I can suggest an alternate approach based on the type of data.
Arjay at 2007-11-10 22:51:38 >
# 28 Re: Postmessage from a win32 dll to MFC application
I send the pointer to the data i,e structure pointer .Its size would be around 1000 bytes.
And as I said earlier I get the exception even I if am not posting the message to the GUI .
Sometimes I get Out of memory message from the GUI module ! but origin of the message is still unknown ,.
justlikedat at 2007-11-10 22:52:44 >
# 29 Re: Postmessage from a win32 dll to MFC application
I send the pointer to the data i,e structure pointer .Its size would be around 1000 bytes.
And as I said earlier I get the exception even I if am not posting the message to the GUI .
Sometimes I get Out of memory message from the GUI module ! but origin of the message is still unknown ,.To debug this, we need two things.

1) The code to the message handler that processes the message in the UI thread.
2) The entire thread proc code.

We need to see both snippets to make sure you are declaring the message handler prototype properly. And also make sure the thread proc code isn't accessing MFC objects directly.

Post the code and I'll show you how to use a thread safe queue to transfer the data between threads.
Arjay at 2007-11-10 22:53:39 >