Python ctypes in_dll string assignment

Posted by ackdesha on Stack Overflow See other posts from Stack Overflow or by ackdesha
Published on 2010-05-19T17:31:42Z Indexed on 2010/05/19 18:20 UTC
Read the original article Hit count: 200

Filed under:
|

I could use some help assigning to a global C variable in DLL using ctypes.

The following is an example of what I'm trying:

test.c contains the following

    #include <stdio.h>

    char name[60];

    void test(void) {
      printf("Name is %s\n", name);
    }

On windows (cygwin) I build a DLL (Test.dll) as follows:

gcc -g -c -Wall test.c
gcc -Wall -mrtd -mno-cygwin -shared -W1,--add-stdcall-alias -o Test.dll test.o

When trying to modify the name variable and then calling the C test function using the ctypes interface I get the following...

>>> from ctypes import *
>>> dll = windll.Test
>>> dll
<WinDLL 'Test', handle ... at ...>
>>> f = c_char_p.in_dll(dll, 'name')
>>> f
c_char_p(None)
>>> f.value = 'foo'
>>> f
c_char_p('foo')
>>> dll.test()
Name is Name is 48+?
13

Why does the test function print garbage in this case?

© Stack Overflow or respective owner

Related posts about python

Related posts about ctypes