title: C++ LoadLibrary ERROR_NOACCESS "Invalid access to memory location."
body: <p>OK, so I have a situation in which I call <code>LoadLibrary</code> on a DLL that I wrote. This call to LoadLibrary returns error #998, or <code>ERROR_NOACCESS</code> "Invalid access to memory location."</p>
  
  <p>The DLL in question uses MFC in one configuration, and not in another; only the MFC configuration has this problem. It <em>used to work</em>, but I have no idea what I changed: I'd actually moved on to the non-MFC version and been tinkering quite a lot with that and I have no idea what I could have done that affected the MFC version.</p>
  
  <p>I don't know a lot about DLLs. The original loading code was actually given to me, and I haven't changed it. Below is that code:</p>
  
  <pre><code>// submodule loading
  #ifndef MFC
  // Project uses standard windows libraries, define an entry point for the DLL to handle loading/unloading
  BOOL WINAPI DllMain(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
  {
      _MESSAGE("DllMain called.");
      switch(dwReason)
      {
      case DLL_PROCESS_ATTACH:    // dll loaded
          hModule = (HMODULE)hDllHandle;  // store module handle
          _MESSAGE("Attaching Submodule ..."); 
          break;
      case DLL_PROCESS_DETACH:    // dll unloaded
          _MESSAGE("Detaching Submodule ...");      
          break;
      }   
      return true;
  }
  #else
  // Project uses MFC, we define here an instance of CWinApp to make this a 'well-formed' DLL
  class CSubmoduleApp : public CWinApp
  {
  public:
      virtual BOOL InitInstance()
      {// dll loaded
          hModule = m_hInstance;  // store module handle
          _MESSAGE("Attaching Submodule ...");
          return true;
      }
      virtual int ExitInstance()
      {// dll unloaded
         _MESSAGE("Detaching Submodule ...");      
         return CWinApp::ExitInstance();
      }
  } gApp;
  #endif
  </code></pre>
  
  <p>Obviously, <code>MFC</code> is defined in the MFC configuration, and not otherwise.</p>
  
  <p>I doubt this is enough information to solve this problem; I realize that. What I'm actually hoping to learn is <em>where to look for problems</em> that might cause this error. I'll be happy to supply any information you need — once I know it's needed.</p>
  
  <p>Thanks for any tips.</p>