Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I developed a regular dll for my mfc app.Loading the dll using afxLoadLibrary and geting the function exported in the dll using GetProcAddress both worked well ,but i got an error 1814 when processing the CreateDialog function in the dll.In the msdn ,i got the idea that the dll used by mfc should add AFX_MANAGE_STATE(AfxGetStaticModuleState( )) at the begining of every function in the dll,but it didn't work.
There is a dialog template resource in the dll. I also tried AfxSetResourceHandle,but it didn'd work.
C++
HMODULE exd = AfxLoadLibrary("exd.dll");
if( mem!=NULL ){
    ::AfxSetResourceHandle((HINSTANCE)exd);
    MyPROC myproc = (MyPROC)::GetProcAddress(exd,"proc");
    if( myproc!=NULL) {
        (*myproc)(::GetModuleHandle(NULL),NULL);
    }else{
        MessageBox("Load Function error","Error",NULL);
    }
}else{
    MessageBox("Load Dll error","Error",NULL);
}

The Code in the dll:
C++
extern "C"  __declspec(dllexport)  void proc(HINSTANCE hInstance = NULL ,HWND parent = NULL)
{
	AFX_MANAGE_STATE(AfxGetStaticModuleState());//it does not work....
        HINSTANCE hr = ::AfxFindResourceHandle(MAKEINTRESOURCE(IDD_MEMORYCORNER),RT_DIALOG);//it finds the resource!!
	if( hr == NULL ){
		MessageBox(NULL,"Find Resource Failed","DLL Error Code",NULL);
	}else{
		::AfxSetResourceHandle(hr);//i dont know whether it works,The 1814 continued being there.
	}
	HWND hDlg = ::CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MEMORYCORNER),parent,DlgProc); //Error 1814 but MAKEINTRESOURCE(IDD_MEMORYCORNER)!=0
	if( hDlg  != NULL ){
		ShowWindow(hDlg ,SW_NORMAL);
		MSG msg;
		while( GetMessage(&msg,hDlg ,NULL,NULL)){
			::IsDialogMessage(hDlg ,&msg);
		}
	}
	::itoa(::GetLastError(),membuf,10);
	MessageBox(NULL,membuf,"DLL Error Code",NULL);
}

Anyone helps,thanks.
Posted
Updated 8-Dec-12 0:28am
v6
Comments
chaau 10-Dec-12 1:15am    
I think the problem is that you use the passed-in HINSTANCE as a first argument to the CreateDialog function. I think, in your ::CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MEMORYCORNER),parent,DlgProc) line you need to replace hInstance with ::GetModuleHandle(NULL), so that the instance of the dll is used for the resource template
Keanu L 10-Dec-12 2:49am    
Thanks.The hInstance passed to the dll is the value of GetModuleHandle(NULL),it did not work.I have solved the problem.The first argument of createDialog is to tell the function where to access the resource template.The Dialog Resource built in the dll but the main app space,so the instance passed to the function should owns the dialog template resource.i got the instance which there is a resource in and passed it to the function ,then it worked!The Dialog in the dll showed up!!
Member 10833195 17-Sep-14 20:10pm    
how to get the which there is a resource in and passed it to the function?

1 solution

The hInstance passed to the dll is the value of GetModuleHandle(NULL),it did not work.I have solved the problem.The first argument of createDialog is to tell the function where to access the resource template.The Dialog Resource built in the dll but the main app space,so the instance passed to the function should owns the dialog template resource.i got the instance which there is a resource in and passed it to the function ,then it worked!The Dialog in the dll showed up!!
 
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