Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.
i have a MFC GUI and a dll in C++. The GUI has a edit control, where the user is provided with information regarding what operation is currently carried out. i thought of calling "PostMessage" from the dll to pass that information to the GUI. ie, from the dll:-

PostMessage(hWnd, WM_USER, 0,0); // hWnd - windows handle

How can i embed the message to be displayed in the GUI with the help of WPARAM and LPARAM?
Suppose "File operation failed" is the message to be displayed in the GUI, how can i add it in the PostMessage call?

In the dialog class, following message map is given:-
ON_MESSAGE(WM_USER, &CMainDlg::OnMessage)

an event handler for this is also added:-

LRESULT CMainDlg::OnMessage(WPARAM wParam,LPARAM lParam)
{
// i have to add the message obtained here to edit control.

return 0;
}
Posted
Updated 14-Jul-10 23:30pm
v2

A DLL is mapped in the address space of the process that load it, then you can allocate a buffer for your string in the DLL and pass a pointer to it through the LPARAM: this is what a lot of pre-made Windows messages do.
 
Share this answer
 
1) Try to declare your str as
static LPCTSTR for a test only :)

2) If it will work
and you will need to use PostMessage(..) (not SendMessage(..)) -

allocate the string memory by the sender (for example new CString(..))
and release it by the receiver (for example delete) after your wanted processing :)
 
Share this answer
 
Yes, we have to delete the pointer... :)

Please
compile the both parts in Debug configuration,
configure the debugger to stop at any exception,
start it by F5 and arrive your exception at the deleting.

Then - post hier the function of the exception,
its stack and maybe the list of the last failed assertions.

Thank you :)
 
Share this answer
 
I added :-

LPCTSTR str = _T("This is the message for GUI");
LPARAM lParam = *str;

PostMessage(hWnd, WM_MESSAGE, 0, lParam);


In the OnMessage function,
LRESULT CMAinDlg::OnMessage(WPARAM wParam,LPARAM lParam)
{
LPCTSTR xx= (LPCTSTR)(&lParam);
AfxMessageBox(xx);
return 0;
}

This is showing onlt "T" in the message box. How can i display full string in the GUI? I read that LPARAM has lower and higher coordinates. I dont know how to use LPARAM for displaying the string. Can anyone provide me a good solution?
 
Share this answer
 
i got it. Thanks. static LPCTSTR didnt work. But rest of the steps worked.
:)
In the caller, i put like :-
CString *str = new CString(_T("This is the message from dll"));
PostMessage(hWnd, WM_MESSAGE, 0,(LPARAM)str);
In GUI handler:-
LRESULT CMainDlg::OnMessage(WPARAM wParam,LPARAM lParam)
{
CString* s = (CString *)lParam;
m_MessageLog.SetWindowTextW(*s);
delete s; // EXCEPTION OCCURS
return 0;
}
But when i give delete s in the GUI, its causing an exception. Do we have to delete the pointer actually? What is the purpose of deleting it?
 
Share this answer
 
Is a bad programming technique to allocate something on an executable (dll or exe) and delete it from another one, and more generally it is an error: since the two executables could use different memory handlers, doing so could give you exceptions, crashes and memory leaks that are very difficult to debug.

If you need to allocate something and pass it across dll boundaries, you could do it using GlobalAlloc() or provide an exported dll function that delete the object.

Another thing: you could pass MFC objects (like CString) across the dll boundaries only if both the dll and exe that will share the object are dinamically linked to MFC and the dll is an MFC extension dll.
Otherwise you could get serious memory management problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900