C语言 使用 ctypes 将 python 字符串对象转换为 c char*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27127413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:34:46  来源:igfitidea点击:

Converting python string object to c char* using ctypes

cstringpython-3.xctypes

提问by LittleOne

I am trying to send 2 strings from Python (3.2) to C using ctypes. This is a small part of my project on my Raspberry Pi. To test if the C function received the strings correctly, I place one of them in a text file.

我正在尝试使用 ctypes 将 2 个字符串从 Python (3.2) 发送到 C。这是我的 Raspberry Pi 项目的一小部分。为了测试 C 函数是否正确接收了字符串,我将其中一个放在文本文件中。

Python code

Python代码

string1 = "my string 1"
string2 = "my string 2"

# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')

# send strings to c function
my_c_function(ctypes.create_string_buffer(b_string1),
              ctypes.create_string_buffer(b_string2))

C code

代码

void my_c_function(const char* str1, const char* str2)
{
    // Test if string is correct
    FILE *fp = fopen("//home//pi//Desktop//out.txt", "w");
    if (fp != NULL)
    {
        fputs(str1, fp);
        fclose(fp);
    }

    // Do something with strings..
}

The problem

问题

Only the first letter of the string appears in the text file.

只有字符串的第一个字母出现在文本文件中。

I've tried many ways to convert the Python string object with ctypes.

我尝试了很多方法来使用 ctypes 转换 Python 字符串对象。

  • ctypes.c_char_p
  • ctypes.c_wchar_p
  • ctypes.create_string_buffer
  • ctypes.c_char_p
  • ctypes.c_wchar_p
  • ctypes.create_string_buffer

With these conversions I keep getting the error "wrong type" or "bytes or integer address expected instead of str instance".

通过这些转换,我不断收到错误“错误类型”或“预期的字节或整数地址而不是 str 实例”。

I hope someone can tell me where it goes wrong. Thanks in advance.

我希望有人能告诉我哪里出了问题。提前致谢。

回答by LittleOne

Thanks to Eryksun the solution:

感谢 Eryksun 的解决方案:

Python code

Python代码

string1 = "my string 1"
string2 = "my string 2"

# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')

# send strings to c function
my_c_function.argtypes = [ctypes.c_char_p, ctypes.char_p]
my_c_function(b_string1, b_string2)

回答by Aksel

I think you just need to use c_char_p() instead of create_string_buffer().

我认为您只需要使用 c_char_p() 而不是 create_string_buffer()。

string1 = "my string 1"
string2 = "my string 2"

# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')

# send strings to c function
my_c_function(ctypes.c_char_p(b_string1),
              ctypes.c_char_p(b_string2))

If you need mutable strings then use create_string_buffer() and cast those to c_char_p using ctypes.cast().

如果您需要可变字符串,则使用 create_string_buffer() 并使用 ctypes.cast() 将它们转换为 c_char_p。

回答by Aksel

Have you considered using SWIG? I haven't tried it myself but here's what it would look like, without changing your C source:

您是否考虑过使用SWIG?我自己还没有尝试过,但在不更改您的 C 源代码的情况下,它会是什么样子:

/*mymodule.i*/

%module mymodule
extern void my_c_function(const char* str1, const char* str2);

This would make your Python source as simple as (skipping compilation):

这将使您的 Python 源代码像(跳过编译)一样简单:

import mymodule

string1 = "my string 1"
string2 = "my string 2"
my_c_function(string1, string2)

Note I'm not certain .encode('utf-8')is necessary if your source file is already UTF-8.

注意.encode('utf-8')如果您的源文件已经是 UTF-8,我不确定是否有必要。