在 Visual Studio 2017 年创建的简单 DLL 无法在 XP 中加载

Simple DLL created in Visual Studio 2017 doesn't load in XP

平台工具集 - 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.

中编译时工作正常

Download VS017 solution

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