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

标签 c++ dll visual-studio-2017 windows-xp static-variables

<分区>

平台工具集 - Visual Studio 2017 - Windows XP (v141_xp)

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

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

动态链接库代码:

#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 数据。它无法解决。你需要找到另一种方法。

关于c++ - 在 Visual Studio 2017 中创建的简单 DLL 不会在 XP 中加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52641480/

相关文章:

c++ - 使/欺骗 Visual C++ 正确地缩进宏结构

c++ - 为什么 Visual Studio C++ 即使在不需要它的文件上也需要包含 "StdAfx.h"?

c++ - 在 C++ 中实现没有类的命名空间

c++ - 是否可以为同时使用 Windows 和 Linux 的客户端使用相同的 DLL

c++ - 如何在 VS 2017 中编译其他编译器的代码

c++ - Isavailable 不是 QSound 的成员

c++ - CLR Dll UI 创建了太多表单

c# - 类型 'Newtonsoft.Json.JsonConvert' 存在于 'Newtonsoft.Json.dll' 和 'NuGetApi2.dll' 中

c# - 当我的解决方案不在我的 C : drive? 中时,.editorconfig 在 Visual Studio 2017 中不起作用

windows-10 - 拖放到在 Windows 10 上运行的 Visual Studio 2017 不起作用