Skip to main content
 首页 » 编程设计

python之如何使用ctypes在特定地址显示字符串

2025年05月04日19mate10pro

我正在处理一个 C dll,其中一个函数返回一个指向字符串的指针。我想打印这个指针的内容。

目前我有这个:

>>> str_p = dll.myfunction(42) 
>>> print str_p 
1371678884 

我尝试了 repr(str_p)、repr(str_p.value),但似乎 str_p 被视为一个整数。

当然 print str(str_p) 也会打印 1371678884

请您参考如下方法:

将函数的 restype 设置为 ctypes.c_char_p .

所以这对你有用:

>>> dll.myfunction.restype = c_char_p 
>>> str_p = dll.myfunction(42) 
>>> print str_p 
The Content Of Your String