我正在编写一个Python,它通过ctype从OS上调用Security.framework函数。
我能够用Python创建一个CFDictionaryRef和CFStringRef,并将字符串存储在字典中,但现在我正在添加Security.framework查询对象(kSecClass、kSecMatchLimit、kSecReturnAttributes、…)我得到了以下坠机:
objc[28201]: Method cache corrupted. This may be a message to an invalid object, or a memory error somewher
我使用ctype将数组指针传递给dll,并返回指向使用dll中的malloc创建的双重数组的指针。返回Python时,我需要一种将指针转换为数组或Python列表的快速方法。
我可以使用这个列表comp,但是它很慢,因为有32万个数据点:
list_of_results = [ret_ptr[i] for i in range(320000)]
理想情况下,我将用Python创建数组并将其传递给dll,但我必须使用dll中的malloc创建它,因为这是一个动态数组,在这个数组中,我事先不知道会有多少个数据元素(尽管返回指针也返回数据元素的数量,所以我知道返回到Python时有多少) --我使用
下面的python代码段正在生成MyPy“问题”(在vscode中)。
my_struct = MyStruct()
#! set mutable flag to true to place data in our object.
fcntl.ioctl( dev_hand.fileno(), my_ioctl_id, my_struct, True )
错误是:
参数3到"ioctl“具有不兼容的类型"my_struct";预期的"Unionint,str"
MyStruct是一个ctype结构。使用ioctl()和ctype结构的所有示例都显
字节对象是。它不支持项目分配:
>>> 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"
>>&
下面的代码片段似乎是返回一个句柄而不是一个值,同时获取我的ctype结构的c_int属性。
如何将方法get_val更改为返回o_num或o_str的值?
import ctypes
class my_struct(ctypes.Structure):
_fields_=[('o_num',ctypes.c_int),('o_str',ctypes.c_wchar_p),('o_bool',ctypes.c_int),('o_err',ctypes.c_int)]
class my_substruct(ctypes.S
我正在尝试将一段VBA代码移植到Python。此工作包括在Windows DLL中调用函数。函数需要一个指向C结构的指针(在VBA中,它们被称为"Type")作为参数。上述结构包含固定长度的字符串以及固定长度的字符串数组。我很难找到一种用ctype用Python来表达这一点的方法。
原始的VBA代码包含如下语句:
Public Type elements
elementA As String * 48
elementB(3) As String * 12
End Type
在C语言中,这可以用以下方式表示,我认为:
struct elements
{
c
我正在尝试从memory 中删除密码字符串。
我写了一小段代码:
import ctypes, sys
def zerome(string):
location = id(string) + 20
size = sys.getsizeof(string) - 20
#memset = ctypes.cdll.msvcrt.memset
# For Linux, use the following. Change the 6 to whatever it is on your computer.
print ctypes.string_at(l
我有以下代码
import ctypes
pBuf = ctypes.cdll.msvcrt.malloc(nBufSize)
# wrote something into the buffer
如何使用Python 2.5将缓冲区的内容保存到文件中?
正如你可能已经知道的,这是行不通的,给TypeError: argument 1 must be string or read-only buffer, not int
f = open("out.data","wb"
f.write(pBuf)