windows C++ LoadLibrary ERROR_NOACCESS “对内存位置的访问无效。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7693596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:13:36  来源:igfitidea点击:

C++ LoadLibrary ERROR_NOACCESS "Invalid access to memory location."

c++windowsdllloadlibrary

提问by KRyan

OK, so I have a situation in which I call LoadLibraryon a DLL that I wrote. This call to LoadLibrary returns error #998, or ERROR_NOACCESS"Invalid access to memory location."

好的,所以我遇到了调用LoadLibrary我编写的 DLL的情况。此对 LoadLibrary 的调用将返回错误 #998,或ERROR_NOACCESS“对内存位置的访问无效”。

The DLL in question uses MFC in one configuration, and not in another; only the MFC configuration has this problem. It used to work, 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.

有问题的 DLL 在一种配置中使用 MFC,而在另一种配置中不使用;只有 MFC 配置有这个问题。它曾经可以工作,但我不知道我改变了什么:我实际上已经转向非 MFC 版本并对此进行了大量修补,但我不知道我可以做些什么来影响 MFC 版本。

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:

我对 DLL 了解不多。原来的加载代码其实是给我的,我没改。下面是那个代码:

// 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

Obviously, MFCis defined in the MFC configuration, and not otherwise.

显然,MFC是在 MFC 配置中定义的,而不是其他情况。

I doubt this is enough information to solve this problem; I realize that. What I'm actually hoping to learn is where to look for problemsthat might cause this error. I'll be happy to supply any information you need — once I know it's needed.

我怀疑这是否足以解决此问题;我意识到这一点。我实际上希望了解的是在哪里寻找可能导致此错误的问题。我很乐意提供您需要的任何信息——一旦我知道它是需要的。

Thanks for any tips.

感谢您提供任何提示。

回答by KRyan

OK, this question was answered by a friend of mine (no idea if he has a StackOverflow account; not going to pester him with answering it twice).

好的,这个问题是我的一个朋友回答的(不知道他是否有 StackOverflow 帐户;不会纠缠他回答两次)。

The deal is that I had a global object, the class of which had a constructor that called a function that depended upon another global object (ironically enough, the function in question was _MESSAGE, but by the time DllMainor InitInstancegets called, that function works fine). C++ doesn't allow you to specify the order in which globals get initialized, so when this global's constructor got run (when the computer attempted to load the DLL), it caused a memory error by attempting to use another global that hadn't been created yet.

交易是我有一个全局对象,它的类有一个构造函数,该构造函数调用依赖于另一个全局对象的函数(具有讽刺意味的是,所讨论的函数是_MESSAGE,但到时候DllMain或被InitInstance调用时,该函数工作正常) . C++ 不允许您指定全局变量的初始化顺序,因此当这个全局的构造函数运行时(当计算机试图加载 DLL 时),它通过尝试使用另一个尚未被初始化的全局变量而导致内存错误尚未创建。

So... that's the answer. A really specific case, but I guess if anyone else finds they're getting 998 errors and need to know what sorts of problems to check, this is something to look for: make sure all your globals are independent!

所以……这就是答案。一个非常具体的案例,但我想如果其他人发现他们收到 998 错误并且需要知道要检查哪些类型的问题,那么需要注意的是:确保您的所有全局变量都是独立的!