在python中使用Structure和char数组

时间:2018-01-29 14:35:29

标签: python

我正在使用ctype。我想使用Structure作为Argument.here是我的代码:

Time="AuditSuccess"
class SecurityVarToCdll(Structure):
   _fields_ = [("Time",c_char_p),("EventID",c_char_p),("Keyword",c_char_p),("Level",c_int),("User",c_char_p)]
Var=SecurityVarToCdll()
Var.Time=c_type_p(Time)

然后我得到以下内容:

 Var.Time = c_char_p(Time)
 TypeError: bytes or integer address expected instead of str instance

我是这里的新生。 谢谢你回答我的问题。

1 个答案:

答案 0 :(得分:2)

在Python中,字符串用引号括起来:

 string = "this is a string"

当你需要字节时,这就是你所拥有的。 字节的定义如下:

bytes = b'this is a bytes object'

bytes = b"This is another bytes object"

看起来你需要使用字节 - 所以把'b'放在引号之前。