Python在Windows 11中无法加载C语言DLL(在Windows 10中可以加载)。

huangapple go评论99阅读模式
英文:

Python won't load C-language DLL in Windows 11 (that loads in Windows 10)

问题

我正在运行Python 3.8.5在Windows 10专业版,版本10.0.19045 Build 19045。我有一个应用程序,在这个应用程序中,我已经将部分处理移到了一个DLL中,这个DLL是使用Microsoft Visual Studio C++ 2019编写的。我加载了我的库DLL并且调用其中的函数没有问题。

我的同事现在开始测试,并报告在Windows 11下无法加载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;
}

这在Windows 10上打印出42,在Windows 11上报告“无法加载”。

提前感谢!

英文:

I'm running Python 3.8.5 on Windows 10 Pro, Version 10.0.19045 Build 19045. I have an application where I have moved part of the processing to a DLL, written in C using Microsoft Visual Studio C++ 2019. I'm loading my library DLL and calling functions within it with no problems.

My colleagues have now started testing, and report the DLL is 'unable to load' under Windows 11. Any ideas why?

I have been able to duplicate the bug with the following minimal application:

test.py

import ctypes
import _ctypes

dll = ctypes.WinDLL(&quot;./test_dll.dll&quot;)
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) &amp;&amp; _MSC_VER&gt;=1020
#pragma once
#endif

#include &lt;stdint.h&gt;
#include &lt;windows.h&gt;


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

#ifdef __cplusplus
extern &quot;C&quot; {
#endif

	MYDLL int WINAPI RunTest(int seed);

#ifdef __cplusplus
}
#endif

#endif

test.cpp:

#include &quot;pch.h&quot;
#include &quot;test.h&quot;

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

This prints 42 on Windows 10 and is 'unable to load' on Windows 11

Thanks in advance!

答案1

得分: 0

问题已解决!原来与Windows版本无关,而是在没有安装Visual Studio的机器上使用了Debug构建。再加上Python提供的不够详细的错误信息,使得问题难以排查。当我尝试从C++程序中调用测试DLL时,它准确地告诉了我缺少哪个库,问题就显而易见了。

英文:

The problem is solved! It turned out not to be anything to do with the Windows version, rather it was the use of a Debug build on a machine that didn't have Visual Studio installed. Aided and abetted by Python's less-than-completely-informative error message. As soon as I tried calling the test DLL from a C++ program, that told me exactly which library was missing, it became obvious.

huangapple
  • 本文由 发表于 2023年3月7日 02:09:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654359.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定