在Visual Studio 2017中创建的简单DLL不会在XP中加载



Platform Toolset - Visual Studio 2017 - Windows XP (v141_xp(

运行时库 - 多线程(无 CRT 依赖项(

DLL 在 Vista+ 上加载正常,但在 XP SP2 (x86( 上失败,错误代码ERROR_NOACCESS(对内存位置的访问无效(

DLL代码:

#include <windows.h>
#include <string>
BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
static std::string test;
break;
}
}
return TRUE;
}

执行代码:

#include <Windows.h>
#include <tchar.h>
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
HMODULE hModule = LoadLibrary(L"dll.dll");
if (hModule)
MessageBoxA(0, "It works", "Info", MB_ICONINFORMATION);
else
{
char buff[10];
_itoa_s(GetLastError(), buff, 10, 10);
MessageBoxA(0, buff, "GetLastError()=", MB_ICONEXCLAMATION);
}
return 0;
}

Visual Studio 2015 也有同样的问题。虽然它在Visual Studio 2013中编译时工作正常。

下载 VS017 解决方案

Windows XP有这个限制。当您加载带有LoadLibrary的 DLL 时,该特定 DLL 中不能有static数据。这是无法解决的。你需要找到另一种方法。

最新更新