简体   繁体   English

如何通过python ctypes将二进制缓冲区传递给ac函数

[英]How to pass a binary buffer via python ctypes to a c function

I'd like to wrap the following c function bar() in python 2.x and invoke it via the ctypes module, both on Linux and Windows: 我想将以下c函数bar()包装在python 2.x中,并在Linux和Windows上通过ctypes模块调用它:

#include <stdio.h>

#ifdef FOO_DLL
#define FOO_API __declspec(dllexport)
#else
#define FOO_API extern
#endif

FOO_API unsigned int bar(const void* data, unsigned int len) 
{
   printf("%p (%d bytes)\n", data, len);
   return len;
}

To compile the shared library I use scons: 要编译共享库,我使用scons:

import sys

env = Environment()
if sys.platform == 'win32':
  env.Append(CFLAGS=['-DFOO_DLL'])

env.SharedLibrary('foo', ['foo.c'])

Finally I load the respective shared lib and invoke the function: 最后,我加载各自的共享库并调用该函数:

import sys
import struct
from ctypes import *

if sys.platform == 'win32':
  libfoo = windll.LoadLibrary('foo')
else:
  libfoo = cdll.LoadLibrary('./libfoo.so')

data = struct.pack('BBB', 0, 1, 2)
libfoo.bar(data, len(data))

To my surprise this seems to work flawlessly under Linux, but on Windows the code throws an error that makes me suspects that this isn't the proper way to do things: 令我惊讶的是,这似乎在Linux下可以完美地工作,但是在Windows上,代码引发了一个错误,使我怀疑这不是正确的处理方式:

$ python foo.py
00B520C4 (3 bytes)
Traceback (most recent call last):
  File "foo.py", line 11, in <module>
    libfoo.bar(data, len(data))
ValueError: Procedure probably called with too many arguments (8 bytes in excess)

What would be the right approach? 什么是正确的方法?

On Linux (Ubuntu 10.10 64bit) I'm using Python 2.6.6, gcc 4.4.5 and on Windows (XP 32bit, in a VirtualBox) Python 2.7.1 and VS2010 Express. 在Linux(Ubuntu 10.10 64位)上,我使用的是Python 2.6.6,gcc 4.4.5,在Windows(XP 32位,在VirtualBox中)使用的是Python 2.7.1和VS2010 Express。

Both Windows and Linux should use cdll which results in the C calling convention. Windows和Linux都应使用cdll ,这将导致C调用约定。 The Python code at present calls your Windows DLL using stdcall but it's really a cdecl function. 目前,Python代码使用stdcall调用Windows DLL,但这实际上是一个cdecl函数。 The difference between cdll and windll is just the calling convention used. cdllwindll之间的区别只是所使用的调用约定。

That error message is a textbook calling convention mismatch error, for what it's worth. 该错误消息是一本教科书,称其约定不匹配错误,这是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python ctypes中将“文件”传递给需要“ Buffer”的cpp函数? - How to pass a 'file' to cpp function that expects 'Buffer' in Python ctypes? 如何通过ctypes将void *数组传递给C函数? - How do I pass void* array to a C function via ctypes? 通过ctypes将C数组传递给python - Pass C array to python via ctypes 如何将python 2d矩阵传递给C函数ctypes - How to pass python 2d matrix to C function ctypes 在 Python 中,如何使用 ctypes 将回调数组作为参数传递给 C 函数? - In Python, how to pass a callback array as arguments to a C function using ctypes? 如何使用ctypes,python将字符串参数传递给C ++函数 - How to pass string parameters to C++ function using ctypes, python 如何使用ctypes将python列表传递给C函数(dll) - How to pass a python list to C function (dll) using ctypes 如何使用 ctypes.create_string_buffer 将其作为 char * 参数传递给 C function? - How do I use ctypes.create_string_buffer to pass it as a char * argument for a C function? 如何通过 cZtype 将 python function 的字符串缓冲区传递给 Z0D61F8370CAD1D412F80B84D1D412F80B84D143E1257Z Z72664DC0959F3B0C0C04891F8CF7 - How to pass a string buffer from python function to C Api via ctype 如何通过ctypes将列表(非空)从Python传递到C ++? - How to pass (non-empty) list of lists from Python to C++ via ctypes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM