How to pass String BYREF to C function?
"Lynton Grice" <[email protected]>
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
Hi there,
I have the following example C function that basically take 2 arguments and
"halves the first string" and puts that result in the second argument.
int executeUserExit(char *buffer, char **userExitResultBuffer){
int total_len;
total_len = strlen(buffer) / 2;
*userExitResultBuffer = (char*) malloc(total_len);
strncpy(*userExitResultBuffer, buffer, total_len);
return 0;
}
I have call this from C code happily, but am having trouble from Python.
I created a DLL from the code above
import ctypes as c
import os
if __name__ == "__main__":
if os.name=='nt':
userExit = c.cdll.LoadLibrary("libUserExits.dll")
executeUserExit = userExit.executeUserExit
executeUserExit.argtypes = [c.c_char_p, c.c_char_p]
executeUserExit.restype = c.c_int
stream = c.c_char_p()
stream.value = "hello world"
exitResult = c.c_char_p()
result = executeUserExit(c.pointer(stream), c.byref(exitResult))
print result
print "Finished"
I am not too sure how the BYREF and POINTER stuff should work in the above
code?
I keep getting the error:
"ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: wrong
type"
I have been reading online but cannot find any nice example to follow, can
someone please give me some advise on this?
Your help is greatly appreciated ;-)
Lynton
------------------------------------------------------------------------------
This SF.net email is sponsored by
Make an app they can't live without
Enter the BlackBerry Developer Challenge
http://p.sf.net/sfu/RIM-dev2dev
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users