Re: in_dll string assignment question

"Mark Tolonen" <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
"Ben Stroud" <[email protected]> wrote in message 
news:[email protected]...
>I could use some help assigning to global C variable in DLL using ctypes.
>
> 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
>
> Then 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 4∞┘☺
> 13
>
> Why does the test function print garbage in this case?

"name" is an array, not a pointer.  Try:

    (c_char*60).in_dll(dll,'name').value = "foo"

-Mark



------------------------------------------------------------------------------

_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.