C ++ LoadLibrary ERROR_NOACCESS“对存储器位置的访问无效”。 [英] C++ LoadLibrary ERROR_NOACCESS "Invalid access to memory location."

查看:133
本文介绍了C ++ LoadLibrary ERROR_NOACCESS“对存储器位置的访问无效”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,所以我有一种情况,我在我写的一个DLL上调用$ code LoadLibrary 。对LoadLibrary的此调用返回错误#998或 ERROR_NOACCESS 访问内存位置无效。

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

问题在一个配置中使用MFC,而不是在另一个配置中使用;只有MFC配置有这个问题。它曾经用于工作,但我不知道我改变了什么:我实际上转移到非MFC版本,并且修补了很多,我不知道我可以有完成影响MFC版本。

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。原来的加载代码实际上是给我的,我没有改变它。以下是代码:

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:

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

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

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

我怀疑这是足够的信息来解决这个问题;我意识到我真正希望学习的是在哪里可以找到可能导致此错误的问题。我很乐意提供您需要的任何信息 - 一旦我知道需要的话。

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 problems that might cause this error. I'll be happy to supply any information you need — once I know it's needed.

感谢任何提示。

推荐答案

好的,这个问题是由我的一个朋友回答的(不知道他有没有StackOverflow帐户;不要劝他两次回答)。

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).

这个交易是我有一个全局对象,它的类有一个构造函数,它调用一个依赖另一个全局对象的函数(讽刺的是,这个函数是 _MESSAGE ,但在 DllMain InitInstance 被调用的时候,该函数工作精细)。 C ++不允许你指定全局变量被初始化的顺序,所以当这个全局的构造函数运行时(当计算机试图加载DLL时),它引起了一个内存错误,试图使用另外一个没有被创建了。

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 DllMain or InitInstance gets 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.

所以...这是答案。一个非常具体的案例,但是我想如果有人发现他们遇到998个错误,需要知道要检查哪些问题,这是需要寻找的:确保所有的全局变量都是独立的!

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!

这篇关于C ++ LoadLibrary ERROR_NOACCESS“对存储器位置的访问无效”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆