Re: How to pass String BYREF to C function?

"Lynton Grice" <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <[email protected]>
Hi Marco,

 

Thanks for the response, but I still get an error ;-(

 

ctypes.ArgumentError: argument 2: <type 'exceptions.TypeError'>: wrong type

 

I tried this code:

 


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.pointer(c.c_char_p())            

            result = executeUserExit(stream, exitResult)

      

      print "Finished"      

      

 

Do you have any other ideas?

 

Thanks for the help thus far ;-)

 

Lynton

 

From: marco cassisa [mailto:[email protected]] 
Sent: 17 August 2010 09:51 PM
To: Lynton Grice
Subject: Re: [ctypes-users] How to pass String BYREF to C function?

 

Hi 
I am absolutely no pro (just trying to use ctypes in these days), but AFICT
c_char_p is equivalent to char*, while pointer(c_char_p) is equivalent to
char**
So it seems to me that your C-declaration isn't equivalent to your
ctypes'one
Hope not to be wrong...
bye

marco

2010/8/17 Lynton Grice <[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

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