Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have a regular MFC DLL, which has a dialog resource and class. If I call the dialog with DoModal() from my app without using threading, all is OK. If I call the dialog with DoModal() from a CWinThread, the dialog shows, but its parent is not my app, so it kind of behaves like a modeless dialog, ie I can click back to my app when the dialog is still showing. Which is not great.

I especially made a very small project which demonstrates the problem, it's here (not sure if there is a repository to use for codeproject questions..?), so if anyone is interested maybe they can have a look please?

Thanks for any advice.
Posted
Comments
ARopo 14-Mar-11 6:14am    
Not clear what you want to acheive here. Do you want a modal or modeless dialog, why do you want to run a modal dialog on a separate thread?
Sandeep Mewara 14-Mar-11 6:25am    
Comment from OP:
I need a modal dialog. All my application's commands run in a separate thread, for a variety of reasons. My 'real' application is a CWinAppEx. The sample project I made is dialog based just for the purposes of explaining the problem (and behaves in the same way). I can see why getting a dialog to show a 'nested' modal dialog in a separate thread seems strange but it's really just that way to demonstrate the problem in the simplest manner. Thanks for your interest.

My best way of dispaying model dailog is using AfxLoadLibrary()

This may be not you accepted but,
try this,
in dll project
you have a dialog,
Define
__declspec (dllexport)void WINAPP DllCall(){
 CYouDlg myDlg;
 myDlg.DoModel();
}


and in calling project define
extern void WINAPP DllCall()

at the top of the calling class.

and then
HINSTANCE TempHandle;
TempHandle=AfxLoadLibrary("YourDllName");
SetResourceHandle(TempHandle);
DllCall();


this work so fine as implemented dll for 50 dialogs using same methods.
dll using Threads gives some time problem of linking.

This article may help you.
Dll linking[^]
its just suggestion
Thanks.
 
Share this answer
 
Comments
morrisecl 15-Mar-11 6:42am    
Thank you Santosh, this seems to be working well.
I assume you meant AfxSetResourceHandle(). I also had to remove AFX_MANAGE_STATE(AfxGetStaticModuleState()) from my DLL code to make it work, which worries me a little. Either this is going to work fine, or it's a time bomb... but it does seem to be working well now.
In fact I replaced the AFX_MANAGE_STATE code in the DLL with the resource switching code you gave me, and also code later on to restore the resource handle to what it was before I switched it.

Does that sound OK to you?
[no name] 16-Mar-11 1:50am    
Its ok i was used same method for implementing dll for my project
I'm still not sure what the exact problem you are having is but, using your example you can pass the parent dialog all the way through to the winThread and onto the dialog, this then stops clicking behide loosing the window.

so:

Starting the dialog passing in the parent which is this
C++
::PostThreadMessage (mMyThread->m_nThreadID, RUNDLGMSG, (WPARAM)mRunDLLDialogs, (LPARAM)this);


Changed your function to accept the parent
C++
_declspec(dllexport) void runDLLDlg (CWnd*);


Passed the parent through in lParam
C++
void MyThread::RunDlg(WPARAM wParam, LPARAM lParam)
{
    if (wParam != 0)
    {
        runDLLDlg ((CWnd*)lParam);
    } else {
        Dlg1 dlg1((CWnd*)lParam);
        dlg1.DoModal();
    }
}


pass all the way through
C++
void runDLLDlg (CWnd* pParent)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());
	DLLDlg1 dlg1(pParent);
	dlg1.DoModal();
}
 
Share this answer
 
Comments
morrisecl 15-Mar-11 6:39am    
Thanks again. I believe this would certainly work. But in my app the dialogs can get called all over the place, the parent can vary a lot! Sometimes the parent is the main app, sometimes it's a dialog, and I need the function in the DLL to just show the dialog as a modal dialog on top of whatever, regardless of threads etc. I cannot go through all possible calling code to set up pointers to a parent.
Sincerely, I appreciate your help.

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