0

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:

#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:

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:

$ 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.

1 Answer 1

1

Both Windows and Linux should use cdll which results in the C calling convention. The Python code at present calls your Windows DLL using stdcall but it's really a cdecl function. The difference between cdll and windll is just the calling convention used.

That error message is a textbook calling convention mismatch error, for what it's worth.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, thanks. I somehow assumed that on windows I should use windll. Stupid me. Using "libfoo = cdll.LoadLibrary('foo')" for the win32 part works flawlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.