Solved

LoadLibrary - Explicit Linking in an MFC Extension DLL

Posted on 2000-03-09
4
1,274 Views
Last Modified: 2013-11-20
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;
}

0
Comment
Question by:princejose
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Accepted Solution

by:
simmvar earned 150 total points
ID: 2600086
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
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2600122
Hi simmvar,

princejose told LoadLibray() fails, not GetProcAddress()...

Hi princejose,

Check the path of the dll. To load the dll without a specified path the dll must reside in one of the following directories:

1. The path from which the application's executable is loaded
2. The current working directory
3. For NT the window's SYSTEM32 directory
4. The window's SYSTEM directory
5. The windows directory
6. any directory set with the PATH environment variable

Best would be to change the dll's project settings to build the dll directly to the applications debug/release directory.

you should even check for following reasons (from MSDN):
--------------------------------------------------------------------------------------------------------------------
Windows 95: If you are using LoadLibrary to load a module that contains a resource whose numeric identifier is greater than 0x7FFF, LoadLibrary fails. If you are attempting to load a 16-bit DLL directly from 32-bit code, LoadLibrary fails. If you are attempting to load a DLL whose subsystem version is greater than 4.0, LoadLibrary fails. If your DllMain function tries to call the Unicode version of a Win32 function, LoadLibrary fails.
--------------------------------------------------------------------------------------------------------------------

hope that helps,

ZOPPO
0
 

Expert Comment

by:simmvar
ID: 2603160
Hi prince

sorry

please check whether your dll is a regular dll or not and the mfc dlls are staticallty linked or shared if shared then check whether u have all the mfc dlls reqd. the mod_not_found could be for a another dll used by your dll

simmvar
0
 

Author Comment

by:princejose
ID: 2627721
Hi Zoppo and Zimmvar & Others,

 Sorry for the delay in commenting about your suggestions. I had found a temporary solution to my problem here. When I keep the dll to be loaded(MyDLL.dll) in WinNT/System32 dir it gets loaded by my extension dll. But the mystery remains why it does not load the MyDLL.dll when it is placed in the same directory as where the extension dll lies.

Well I thought the dll search sequence suggested by Zoppo was correct.

1. The path from which the application's executable/Extension DLL is loaded

2. The current working directory
3. For NT the window's SYSTEM32 directory
4. The window's SYSTEM directory
5. The windows directory
6. any directory set with the PATH environment variable

But you can see that MyDLL is placed at path 1 as explained above. But LoadLibrary still does not load MyDLL.dll. Another part of this problem is that if I just make an application/exe and keep MyDLL at the same place as exe it loads MyDLL. But not in case of an Extension DLL.

Is there any such restrictions for extension DLLs that the dlls and extension dll tries to load should be in System32 dir. I donot think so.

So if any of you can figure out, it will be great and very helpful...

Thank you very much!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 54
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 1,003
VBA "SendKeys" Syntax for Multiple Keystrokes 7 133
mapBully challenge 6 196
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question