我刚刚开始使用ctypes,并且想使用我在python中使用ctypes导出到dll文件中的C++类。假设我的C++代码如下所示:
class MyClass {
public:
int test();
...
我知道创建一个包含这个类的.dll文件,然后使用ctype在python中加载这个.dll文件。现在,我如何创建一个MyClass类型的对象并调用它的测试函数?对于ctype,这是可能的吗?或者,我会考虑使用SWIG或Boost.Python,但对于小型项目来说,ctype似乎是最简单的选择。
我发现这里有几篇关于使用ctype卸载dll的文章,我遵循了据说从ctype导入的方式。
file = CDLL('file.dll')
# do some stuff here
handle = file._handle # obtain the DLL handle
windll.kernel32.FreeLibrary(handle)
但是,我在python64位上,我的dll也是为x64编译的,我从上面的最后一行中得到了一个错误:
argument 1: <class 'OverflowError'>: int too long to
我通过ctype把一个dll集成到python中,这个dll需要设置一个回调函数。但是dll回调函数声明如下
void setCallBack(void(*)(Data &a))
Data is C struct, declare as below
typedef struct {
int index,
....
}Data
我不清楚如何通过ctype设置它,我尝试如下,但返回"WindowsError: error -1073741795 Windows Error 0xC000001D“错误,您能帮我举一个如何设置回调的例子吗?
mydll = cdll.
我正在尝试为预先编写的DLL文件使用Ctype编写一个Python包装器,但一直在为指针问题而苦苦挣扎。
具体地说,编译成DLL的C++函数的一个简化示例是:
double compute_result(double var1, double var2, char *output_message1, char *output_message2){
// Computational steps are all here, e.g.
double result = var1 + var2;
// then an information message is passed
我已经将代码从C导出到myexported.dll。反过来,myexported.dll依赖于平台/qwindows.dll (QT)。,它与myexported.dll位于同一个目录中。
我就是这样使用ctype的:
#myapp.py
from ctypes import *
mydll = WinDLL("myexported.dll")
mydll.independant_foo() #works fine
mydll.dependant_foo() #prompts the error and exits the program (python.exe)
当我运行脚
我定义了一个从BindingList派生的泛型类,它有一个嵌套的非泛型类:
class Generic<T> : BindingList<Generic<T>.Inner>
{
public class Inner
{
public object Foo { get; set; }
}
}
当尝试通过如下动态引用访问Value属性时,mscorlib中会出现StackOverflowException:
dynamic d = new Generic<string>.Inner();
var value =
我想在python app中运行带有.net框架4.7的C#动态链接库。我在dll中使用dllExport nuget作为我的方法,因为使用这种方式的ctype可以访问我的方法。示例: using System.Runtime.InteropServices;
namespace MyNameSpace
{
public class MyClass
{
[DllExport("MyMethod", CallingConvention = CallingConvention.StdCall)]
public static int
使用ctype "windll.LoadLibrary“将设备的DLL导入到Python中,首先,我想使用DLL的一个函数,即返回库版本。有一个带有DLL的头文件,其中定义了如下函数:
/** * \brief Returns the library version in the format 'AA.BB.CC.DD'
(e.g. '1.0.0.0'). * * OUTPUT PARAMETER * \param version
Version number of the library */
#if
我有一个Delhi,它公开了一个具有以下签名的函数:
Function MyFunc(ObjID : Cardinal) : TMyRec; stdcall;
其中记录是这样定义的:
type TMyRec = record
Count : Cardinal;
Items : array of TMyItemRec;
end;
type TMyItemRec = record
ID : Cardinal;
Params : array of String;
end;
现在我的问题是:如何使用Python ctype访问MyFunc调用dll的结果?我编写了两个模拟类型的类。