You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 11下Python无法加载可在Win10运行的C语言DLL求助

Windows 11无法加载VS2019编译的C DLL的排查思路

复现环境与代码

test.py

import ctypes
import _ctypes

dll = ctypes.WinDLL("./test_dll.dll")
dll.RunTest.restype = ctypes.c_int
dll.RunTest.argtypes = [ctypes.c_int]
print(dll.RunTest(0))
_ctypes.FreeLibrary(dll._handle)

test.h

#ifndef TEST_H
#define TEST_H

#if defined(_MSC_VER) && _MSC_VER>=1020
#pragma once
#endif

#include <stdint.h>
#include <windows.h>


#if !defined(MYDLL)
# if defined(TESTDLL_EXPORTS)
#  define MYDLL extern "C" __declspec(dllexport)
# else
#  define MYDLL extern "C" __declspec(dllimport)
# endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

    MYDLL int WINAPI RunTest(int seed);

#ifdef __cplusplus
}
#endif

#endif

test.cpp

#include "pch.h"
#include "test.h"

MYDLL int WINAPI RunTest(int seed) {
    return 42;
}

原因分析与排查方向

  • VC++运行时库依赖问题
    VS2019编译的DLL默认依赖对应版本的VC++ Redistributable运行时库。Windows 10可能因之前的软件安装预装了该库,但Windows 11系统可能未包含:

    • 若编译时用/MD(动态链接运行时):需让同事安装VS2019对应的VC++运行时包,注意匹配DLL的x86/x64架构
    • 若编译时用/MT(静态链接运行时):会把运行时库嵌入DLL,可避免依赖问题,需确认编译选项是否正确设置
  • 架构不匹配
    检查Python与DLL的架构是否一致:

    • 用python -c "import sys; print(sys.maxsize > 2**32)"判断Python是64位(输出True)还是32位(输出False)
    • 用VS自带的dumpbin /headers test_dll.dll查看DLL的架构,确保两者完全匹配,否则会直接加载失败
  • 安全软件拦截
    Windows 11的安全防护机制更严格,未签名的DLL可能被Windows Defender或第三方安全软件拦截。可临时关闭实时保护测试,或给DLL添加数字签名解决长期问题

  • 路径错误
    确认同事运行脚本时,当前工作目录下存在test_dll.dll,或改用绝对路径加载(比如ctypes.WinDLL("C:\\path\\to\\test_dll.dll")),避免相对路径导致的文件找不到问题

  • 系统兼容性设置
    右键DLL文件→属性→兼容性,勾选“以兼容模式运行这个程序”并选择Windows 10,部分旧编译配置的DLL可能需要该设置才能在Windows 11正常加载

  • 额外依赖库缺失
    用dumpbin /dependents test_dll.dll(VS工具)查看DLL依赖的其他系统库,确认Windows 11上这些库是否存在且版本兼容

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 05:47:32