我在Python语言中使用ctype来处理libgphoto2。以下代码在32位计算机上成功,但在64位计算机(Linux、Ubuntu)上失败,并出现分段错误:
import ctypes
gp = ctypes.CDLL('libgphoto2.so')
a = gp.gp_library_version(0)
x = ctypes.c_char_p.from_address(a)
x.value
libphoto2、ctype和python都来自存储库,所以我假设问题出在我的代码中;我需要以一种更通用的方式来考虑指针。gp_library_version()返回cons
我刚刚开始使用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
作为COM自动化程序的一部分,我需要调用用VB.net编写的函数。
VB程序需要byref参数,其定义如下
Function GetBeamWidth(ByRef pfBeamWidth As Single) As Integer
我在下面的VB代码中测试了这个函数,完全没有问题。
Dim y As Single
myObject.GetBeamWidth(x)
'myOject is an instance of the ActiveX server object, which implements function GetBeamWidth
但是,我不知道如何在python中调用这
我正在为C库编写一个带有ctype的python包装器,并且我有一个返回结构的C函数。但是该结构是在另一个C文件中定义的,该文件是由swig包装的。
我简化了结构和代码。
这个结构是由swig包装的。
struct point {
int x;
int y;
};
此函数由ctype包装。
struct point add_points(struct point a, struct point b) {
struct point c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
Python包装器。
import swig
字节对象是。它不支持项目分配:
>>> bar = b"bar"
>>> bar[0] = b"#"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytes' object does not support item assignment
str对象也是不可变的:
>>> bar = "bar"
>>&
对不起,如果标题不准确,我不能百分之百肯定它正确地描述了情况:
我正在尝试使用FreeTDS的ctype模块与C库进行接口。我有一些代码运行得出奇的好,但遇到了一个障碍。我不知道如何将下面dbbind()调用的最后一个参数转换为ctype。
下面的C例子是:
/* these variable types are defined in the FreeTDS library */
DBINT customer_id;
DBCHAR company_name[255];
DBFLT8 avg_income;
/* <snip> */
/* Now bin
我有一个C函数:
void func(int n, int m)
{
int Array[n][m];
make_magic(Array);
}
生成二维数组并进行处理。我在python3.5中的ctype中使用这个函数:
from ctypes import *
magic = CDLL('lib_name')
magic.func(n,m)
那么我如何在python中获取这个2D数组呢?
我有一个C++程序:
class X
{
private:
int a;
public:
int func(char *str);
};
现在我想在我的Python程序中调用函数func。我该怎么做呢?
我已经成功地使用ctype从我的Python程序中调用了C函数。但是,我似乎不知道如何调用定义在C++对象中的函数。
我已经使用过ctype,所以我想弄清楚如何用这种方式来实现它。然而,我对其他技术持开放态度。另外,我的项目设置了一个条件,我不应该在我的C++程序中使用包装器函数。