无法在 python 中加载 C++ dll
Unable to load C++ dll in python
我有一个 C++ dll 我想在 Python,
>>> from ctypes import *
>>> mydll = cdll.LoadLibrary("C:\TestDll.dll")
直到现在没有错误,系统似乎在做我想做的事,但是当我尝试访问 mydll
时,Python IDLE 中的智能显示如下,
从上图可以看出,智能并没有显示dll的任何可用功能,但是当我检查相同的TestDll.dll和dumpbin /EXPORTS TestDll.dll
时,它有45个功能,但是none 这些功能可用于 python。
请注意:关于这个主题有几个问题,我尝试了以下建议但没有用,
- incompatible version of Python installed or the DLL [TestDll.dll & Python 都是 32 位版本]
- I think ctypes is the way to go [同样的问题,看不到任何功能,但加载了 dll]
现在我的问题是如何加载所有可用函数(如dumpbin所示)?
编辑 1
根据eryksun的建议,我取得了一些进展。 TestDll.dll
附带一个头文件 TestDll.h
(可惜我之前错过了这个文件),从中我可以看到可用的导出函数。
TestDll.h:
_stdcall Function1 (const char* prtFileString, cont char* prtDescrip, struct FileParams* ptrParsms);
struct FileParams
{
float val1;
void* pMarker;
};
现在我尝试了以下方法,
>>> mydll = CDLL("c:\TestDll.dll")
>>> Function1 = mydll.Function1
>>> Function1.restype = c_int
到现在为止一切正常,但是当我尝试定义 argTypes 时,不确定如何为结构定义它?
>>> Function1.argtypes = (c_char_c, c_char_c, ???)
非常感谢任何建议。
根据您的 header,这里有一个虚拟 C 文件(针对 Windows),可用于测试 ctypes
包装器。您提到了 C++,但示例是 C 接口,这是一件好事,因为 ctypes
使用 C 函数而不是 C++。
x.c
#include <stdio.h>
struct FileParams
{
float val1;
void* pMarker;
};
__declspec(dllexport) _stdcall Function1 (const char* prtFileString, const char* prtDescrip, struct FileParams* ptrParsms)
{
printf("%s\n%s\n%f %p\n",prtFileString,prtDescrip,ptrParsms->val1,ptrParsms->pMarker);
}
这是 ctypes
包装器。请注意 ctypes
不检查 DLL 并自动加载所有函数。您必须自己声明函数、参数和 return 值。有关 Structure
语法,请参阅 ctypes:structures and unions。
另请注意,对于具有 _stdcall
函数的 DLL,请使用 WinDLL
而不是 CDLL
。
x.py
#!python3
import ctypes
class FileParams(ctypes.Structure):
_fields_ = [('val1',ctypes.c_float),
('pMarker',ctypes.c_void_p)]
x = ctypes.WinDLL('x')
x.Function1.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(FileParams)]
x.Function1.restype = ctypes.c_int
fp = FileParams(1.234,None)
x.Function1(b'abc',b'def',ctypes.byref(fp))
并且输出:
abc
def
1.234000 0000000000000000
我有一个 C++ dll 我想在 Python,
>>> from ctypes import *
>>> mydll = cdll.LoadLibrary("C:\TestDll.dll")
直到现在没有错误,系统似乎在做我想做的事,但是当我尝试访问 mydll
时,Python IDLE 中的智能显示如下,
从上图可以看出,智能并没有显示dll的任何可用功能,但是当我检查相同的TestDll.dll和dumpbin /EXPORTS TestDll.dll
时,它有45个功能,但是none 这些功能可用于 python。
请注意:关于这个主题有几个问题,我尝试了以下建议但没有用,
- incompatible version of Python installed or the DLL [TestDll.dll & Python 都是 32 位版本]
- I think ctypes is the way to go [同样的问题,看不到任何功能,但加载了 dll]
现在我的问题是如何加载所有可用函数(如dumpbin所示)?
编辑 1
根据eryksun的建议,我取得了一些进展。 TestDll.dll
附带一个头文件 TestDll.h
(可惜我之前错过了这个文件),从中我可以看到可用的导出函数。
TestDll.h:
_stdcall Function1 (const char* prtFileString, cont char* prtDescrip, struct FileParams* ptrParsms);
struct FileParams
{
float val1;
void* pMarker;
};
现在我尝试了以下方法,
>>> mydll = CDLL("c:\TestDll.dll")
>>> Function1 = mydll.Function1
>>> Function1.restype = c_int
到现在为止一切正常,但是当我尝试定义 argTypes 时,不确定如何为结构定义它?
>>> Function1.argtypes = (c_char_c, c_char_c, ???)
非常感谢任何建议。
根据您的 header,这里有一个虚拟 C 文件(针对 Windows),可用于测试 ctypes
包装器。您提到了 C++,但示例是 C 接口,这是一件好事,因为 ctypes
使用 C 函数而不是 C++。
x.c
#include <stdio.h>
struct FileParams
{
float val1;
void* pMarker;
};
__declspec(dllexport) _stdcall Function1 (const char* prtFileString, const char* prtDescrip, struct FileParams* ptrParsms)
{
printf("%s\n%s\n%f %p\n",prtFileString,prtDescrip,ptrParsms->val1,ptrParsms->pMarker);
}
这是 ctypes
包装器。请注意 ctypes
不检查 DLL 并自动加载所有函数。您必须自己声明函数、参数和 return 值。有关 Structure
语法,请参阅 ctypes:structures and unions。
另请注意,对于具有 _stdcall
函数的 DLL,请使用 WinDLL
而不是 CDLL
。
x.py
#!python3
import ctypes
class FileParams(ctypes.Structure):
_fields_ = [('val1',ctypes.c_float),
('pMarker',ctypes.c_void_p)]
x = ctypes.WinDLL('x')
x.Function1.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.POINTER(FileParams)]
x.Function1.restype = ctypes.c_int
fp = FileParams(1.234,None)
x.Function1(b'abc',b'def',ctypes.byref(fp))
并且输出:
abc
def
1.234000 0000000000000000