C++ LoadLibrary ERROR_NOACCESS “对内存位置的访问无效。”
好的,我遇到了一种情况,我在我编写的 DLL 上调用 LoadLibrary
。对 LoadLibrary 的调用返回错误 #998,或“ERROR_NOACCESS”“对内存位置的访问无效”。
有问题的 DLL 在一种配置中使用 MFC,而在另一种配置中则不使用;只有MFC配置有这个问题。它曾经可以工作,但我不知道我改变了什么:我实际上已经转向非 MFC 版本并且对此做了很多修改,我不知道我可以拥有什么影响 MFC 版本的操作。
我对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
显然,MFC
是在 MFC 配置中定义的,而不是其他情况。
我怀疑这些信息是否足以解决这个问题;我意识到这一点。我实际上希望了解的是在哪里寻找可能导致此错误的问题。一旦我知道需要,我将很乐意提供您需要的任何信息。
感谢您的任何提示。
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."
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.
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
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.
Thanks for any tips.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这个问题是由我的一个朋友回答的(不知道他是否有 StackOverflow 帐户;不会纠缠他回答两次)。
问题是我有一个全局对象,它的类有一个构造函数,该构造函数调用依赖于另一个全局对象的函数(具有讽刺意味的是,所讨论的函数是
_MESSAGE
,但到了 < code>DllMain 或InitInstance
被调用,该函数工作正常)。 C++ 不允许指定全局变量的初始化顺序,因此当该全局变量的构造函数运行时(当计算机尝试加载 DLL 时),它会通过尝试使用另一个尚未初始化的全局变量而导致内存错误。尚未创建。所以...这就是答案。这是一个非常具体的案例,但我想如果其他人发现他们遇到了 998 错误并且需要知道要检查哪些类型的问题,那么需要注意的是:确保所有全局变量都是独立的!
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).
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 timeDllMain
orInitInstance
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.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!