Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC DLL
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.
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:
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 8-Dec-12 0:51am
Edited 8-Dec-12 1:28am
v6
Comments
Andrew Cherednik at 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
zenkicpp at 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 at 17-Sep-14 20:10pm
   
how to get the which there is a resource in and passed it to the function?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!!
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,740
1 DamithSL 5,769
2 Sergey Alexandrovich Kryukov 5,424
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100