如何将 C# 中的 StringBuilder 参数传递给 python 中的 ctypes,这不是正常的 ctypes 支持的数据类型
How to pass the StringBuilder parameter in C# to ctypes in python, which is not the normal ctypes supported datatype
我正在做这个项目,有一个 c# 项目使用这样的 dll:
public string GetMachineKey()
{
StringBuilder buff = new StringBuilder();
buff.Length = 128;
ZwCommDll.GetCPUMachineKey(buff, 128);
string mk = buff.ToString();
return mk;
}
我想在 python 中使用 ctypes 进行类似的操作。
但我对 StringBuilder 数据类型感到很困惑。
非常感谢您的帮助。
使用 ctypes.create_unicode_buffer()
为 API 生成可写文本字符串缓冲区 (wchar_t*)。使用 ctypes.create_string_buffer()
作为可写字节字符串缓冲区 (char*)。如果函数采用 char*
:
,则类似以下内容应该有效
>>> import ctypes
>>> buff = ctypes.create_string_buffer(128)
>>> ZwCommDll.GetCPUMachineKey(buff,128)
>>> buff.value
b'<returned string>'
我正在做这个项目,有一个 c# 项目使用这样的 dll:
public string GetMachineKey()
{
StringBuilder buff = new StringBuilder();
buff.Length = 128;
ZwCommDll.GetCPUMachineKey(buff, 128);
string mk = buff.ToString();
return mk;
}
我想在 python 中使用 ctypes 进行类似的操作。
但我对 StringBuilder 数据类型感到很困惑。
非常感谢您的帮助。
使用 ctypes.create_unicode_buffer()
为 API 生成可写文本字符串缓冲区 (wchar_t*)。使用 ctypes.create_string_buffer()
作为可写字节字符串缓冲区 (char*)。如果函数采用 char*
:
>>> import ctypes
>>> buff = ctypes.create_string_buffer(128)
>>> ZwCommDll.GetCPUMachineKey(buff,128)
>>> buff.value
b'<returned string>'