Link to home
Start Free TrialLog in
Avatar of MartinD
MartinD

asked on

MFC Extension DLL

My Application loads a number of Dll's during runtime using
AfxLoadLibrary.
Each Dll has an exported function called InitDll.
If a dll allocated memory, the program chrashes everywhere.
I think that the Dll destroys somthing in my app.
What can i Do now?
Avatar of Tommy Hui
Tommy Hui

If you run the program in the debugger, do you get a stack trace that may indicate what might be the problem?

One possibility is to ensure that all the DLLs allocate memory and free the memory themselves. You should not pass memory from the DLL and expect the EXE to free it and vice versa.

Avatar of MartinD

ASKER

I've reduced the program that all dll's do nothing.
It still chrashes.
The loop in my main program look like this:
<outer loop>
 HKEY root;
 HINSTANCE h;
 h=AfxLoadLibrary("<dllname>");
 if(h){
  InitDll* fcn=GetProcAddress(h,"InitDll");
  if(fcn){
   (*fcn)(root,h);
  }
 }
<end loop>

Code in the DLL:
extern "C" void APIENTRY
InitDll(HKEY root,HINSTANCE hinst){
      // Insert this DLL into the resource chain
      new CDynLinkLibrary(ParameterInspectorDLL);

      CMultiDocTemplate* pParTemplate;
      pParTemplate = new CSmartMultiDocTemplate(
            IDR_PARTYPE,
            RUNTIME_CLASS(CParameterInspectorDoc),
            RUNTIME_CLASS(CParameterInspectorFrame),
            RUNTIME_CLASS(CParameterInspectorCtrlView));
}

I've tried the same function without any code in the InitDll function. Doesn't work.

But:
In my main application:
//   (*fcn)(root,h);
no chrash;
Avatar of MartinD

ASKER

It is generally possible to load an extension DLL with AfxLoadLibrary().

The HuskDll example links the .lib files to the application.
Were you actually able to trace into the dll code?
If The return value from loadlibrary is < 32 there was an error.

I see you are only checking for NULL
Avatar of MartinD

ASKER

Yes, I am able to trace into the dll code.

The first three dll's work perfect.
If I try to load another, the program chrashs.
This does not depend on the order, the dlls are loaded.

Take a look at AFX_MANAGE_STATE and the related stuff.
Please provide a stack trace for the crash you experience.  Please also indicate which version of MFC you're using.

Please show the declaration you're using for your pointer to function in the calling program.

.B ekiM


ASKER CERTIFIED SOLUTION
Avatar of AlFa
AlFa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial