Question : LoadLibrary - Explicit Linking in an MFC Extension DLL

I have an Extension DLL inside which I am trying to load another DLL with the usual LoadLibrary()(Explicit Linking). But LoadLibrary() fails with an error code 126 which says
"MODULE_NOT_FOUND".

If anyone about to suggest me that the DLL I am trying to load might not be in the search path, I had tried giving the full path of the DLL in LoadLibrary() too.The result is the same.

Are there any restrictions in loading a library from an Extension DLL. If not then whats the problem here.

I am attaching my code here.

HINSTANCE       hLib;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


static AFX_EXTENSION_MODULE ConverterDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
      // Remove this if you use lpReserved
      UNREFERENCED_PARAMETER(lpReserved);

      if (dwReason == DLL_PROCESS_ATTACH)
      {
            TRACE0("CONVERTER.DLL Initializing!\n");
            
            // Extension DLL one-time initialization
            if (!AfxInitExtensionModule(ConverterDLL, hInstance))
                  return 0;

            // Insert this DLL into the resource chain
            // NOTE: If this Extension DLL is being implicitly linked to by
            //  an MFC Regular DLL (such as an ActiveX Control)
            //  instead of an MFC application, then you will want to
            //  remove this line from DllMain and put it in a separate
            //  function exported from this Extension DLL.  The Regular DLL
            //  that uses this Extension DLL should then explicitly call that
            //  function to initialize this Extension DLL.  Otherwise,
            //  the CDynLinkLibrary object will not be attached to the
            //  Regular DLL's resource chain, and serious problems will
            //  result.

            new CDynLinkLibrary(ConverterDLL);
      }
      else if (dwReason == DLL_PROCESS_DETACH)
      {
            TRACE0("CONVERTER.DLL Terminating!\n");
            // Terminate the library before destructors are called
            AfxTermExtensionModule(ConverterDLL);
      }
      return 1;   // ok
}

extern "C" _declspec(dllexport) BOOL MyFunction(long,char** szParam)
{

  int             ret;

  #ifdef __cplusplus
      int (WINAPI *MYDLL) (char *,char *,char,short);
  #else
      int (WINAPI *MYDLL) (char *,char *,char,short);
  #endif
  hLib=LoadLibrary("MYDLL.dll");
  DWORD errcode=GetLastError();
  if(hLib!=NULL)
  {
      MYDLL= (int (WINAPI *)(char *,char *,char,short)) GetProcAddress( hLib, "_MYDLL@16" );
    if(MYDLL!=NULL)
            ret=(*MYDLL)(szParam[0],szParam[1],"NATIVE",11);  // nonzero return indicates error    
    FreeLibrary(hLib);   }
  else
  {
      AfxMessageBox("The DLL MYDLL.dll missing in the directory specified!!");
            CString szError;
            szError.Format("The Error Code is %d",errcode);
            AfxMessageBox(szError);
  }
  return TRUE;
}

Answer : LoadLibrary - Explicit Linking in an MFC Extension DLL

Hi princejose

The module not found error comes because in your dll MYDLL you would have exported the function MYDLL with the extern "C" _declspec so it will not have any decorated name as _MYDLL@16 so if your function name is MYDLL call it as "MYDLL" in the function name in the GetProcAddress().

wishing you success

With regards

simmvar
Random Solutions  
 
programming4us programming4us