IronPython cdll 无法加载库。如果不使用 IronPython 则有效
IronPython cdll Cannot load library. Works if not using IronPython
我有一个 python 脚本加载了一个 'c' dll,它在使用 python 2.7 (Anaconda) 时工作正常。这是我用来测试错误的测试脚本:
import os
import ctypes
ctypes.cdll.LoadLibrary(os.path.abspath("a_c_library.dll"))
这在使用 python 2.7 执行时工作正常:
python test.py
。没有出现错误,之后我也可以使用该库。
如果我在我的 C# 程序中使用 IronPython,或者甚至只是像这样使用命令行:
"C:\Program Files\IronPython 2.7\ipy.exe" test.py
或 ipy32.exe,我收到以下错误:
Traceback (most recent call last):
File "test.py", line 3, in <module>
File "C:\Program Files\IronPython 2.7\Lib\ctypes\__init__.py", line 445, in LoadLibrary
File "C:\Program Files\IronPython 2.7\Lib\ctypes\__init__.py", line 366, in __init__
OSError: IronPython.Runtime.Exceptions.OSException: cannot load library **THE ABSOLUTE PATH TO MY DLL WHICH IS CORRECT**
为什么我在使用 IronPython 时无法加载库?我在 Windows 10。同样,我可以在使用正常 python 时使用该库,但 IronPython 给我一个相同代码的错误。
感谢您的宝贵时间!
编辑:我尝试使用该代码直接从 C# 加载相同的 dll:
[DllImport("a_c_library.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "AFunction")]
static extern IntPtr Unmanged_AFunction();
然后调用 unmanged 函数,我收到以下错误:System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format.
尝试了 x86 和 x64 构建。 x64 给出此错误:System.DllNotFoundException: 'Unable to load DLL 'a_c_library.dll': The specified module could not be found.
即使将 dll 复制到与 x64 exe 相同的位置。
如果不能从 C# 加载这个库,很难想象 IronPython 会有什么不同;除了包装相同的 .NET API,它什么也做不了。
如果您查看 the source,LoadLibrary
所做的是:
IntPtr res = NativeFunctions.LoadDLL(library, mode);
… 如果它失败了,你只会得到更少的错误信息:
throw PythonOps.OSError("cannot load library {0}", library);
至于那个NativeFunctions.LoadDLL
, it's just a bit of wrapper code to decide whether to call dlopen
out of libdl.so
(on macOS and Linux), or to call LoadLibrary
out of kernel32
(否则)。
因此,如果调用本机 Windows 函数 LoadLibrary
在 C# 中不起作用,使用 ctypes
将以完全相同的方式失败,但是 return 你一个不太有用的错误信息:而不是告诉你(如评论中提到的)System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format'
,它只是告诉你 cannot load library
.
我有一个 python 脚本加载了一个 'c' dll,它在使用 python 2.7 (Anaconda) 时工作正常。这是我用来测试错误的测试脚本:
import os
import ctypes
ctypes.cdll.LoadLibrary(os.path.abspath("a_c_library.dll"))
这在使用 python 2.7 执行时工作正常:
python test.py
。没有出现错误,之后我也可以使用该库。
如果我在我的 C# 程序中使用 IronPython,或者甚至只是像这样使用命令行:
"C:\Program Files\IronPython 2.7\ipy.exe" test.py
或 ipy32.exe,我收到以下错误:
Traceback (most recent call last):
File "test.py", line 3, in <module>
File "C:\Program Files\IronPython 2.7\Lib\ctypes\__init__.py", line 445, in LoadLibrary
File "C:\Program Files\IronPython 2.7\Lib\ctypes\__init__.py", line 366, in __init__
OSError: IronPython.Runtime.Exceptions.OSException: cannot load library **THE ABSOLUTE PATH TO MY DLL WHICH IS CORRECT**
为什么我在使用 IronPython 时无法加载库?我在 Windows 10。同样,我可以在使用正常 python 时使用该库,但 IronPython 给我一个相同代码的错误。
感谢您的宝贵时间!
编辑:我尝试使用该代码直接从 C# 加载相同的 dll:
[DllImport("a_c_library.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "AFunction")]
static extern IntPtr Unmanged_AFunction();
然后调用 unmanged 函数,我收到以下错误:System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format.
尝试了 x86 和 x64 构建。 x64 给出此错误:System.DllNotFoundException: 'Unable to load DLL 'a_c_library.dll': The specified module could not be found.
即使将 dll 复制到与 x64 exe 相同的位置。
如果不能从 C# 加载这个库,很难想象 IronPython 会有什么不同;除了包装相同的 .NET API,它什么也做不了。
如果您查看 the source,LoadLibrary
所做的是:
IntPtr res = NativeFunctions.LoadDLL(library, mode);
… 如果它失败了,你只会得到更少的错误信息:
throw PythonOps.OSError("cannot load library {0}", library);
至于那个NativeFunctions.LoadDLL
, it's just a bit of wrapper code to decide whether to call dlopen
out of libdl.so
(on macOS and Linux), or to call LoadLibrary
out of kernel32
(否则)。
因此,如果调用本机 Windows 函数 LoadLibrary
在 C# 中不起作用,使用 ctypes
将以完全相同的方式失败,但是 return 你一个不太有用的错误信息:而不是告诉你(如评论中提到的)System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format'
,它只是告诉你 cannot load library
.