Re: argc argv filters; pointer to array of strings; point to pointer to pointer

Mark McMahon <[email protected]> Mon, 10 Oct 2011 03:15:26 +0000
Newsgroups gmane.comp.python.ctypes
Message-ID <1FE3E26CC0B45741B13A8157598BB99C079006@005-TK5MPN1-013.MGDADSK.autodesk.com>
Hi,

My guess is that you need to use ctypes.byref()

I simplified the C code (and made it MS specific - cause that's what I have handy. But it should be simple to strip out the declspec stuff)....

============== blah.c ====================
#include <string.h>

__declspec(dllexport) int filters_command_line(char ***pargv) {

    // test1
    printf("%s\n", pargv[0][0]);
    

    // test2
    strcpy(pargv[0][0], "test");
    
    return 0;
}
==================== end =========================

================== blahtest.py =======================
from ctypes import *
import ctypes
fc = cdll.blah.filters_command_line

# test with no variables
fc(ctypes.byref(ctypes.pointer(c_char_p("test"))))

# test using a variable - but hardcoding all the pointer/byref stuff
val = create_string_buffer("ooo", 10)
fc(ctypes.byref(ctypes.pointer(ctypes.pointer(val))))

# finally try the complete one
array_of_arrays = ((c_char_p * 20) * 20)()
array_of_arrays[0][0] = c_char_p('aaaaaa')
fc(ctypes.byref(ctypes.pointer(array_of_arrays)))

# outputs "test"
print array_of_arrays[0][0]
==================== end =========================

I hope this helps,
  Mark



-----Original Message-----
From: Thomas Stover [mailto:[email protected]] 
Sent: Wednesday, August 17, 2011 5:09 AM
To: [email protected]
Subject: [ctypes-users] argc argv filters; pointer to array of strings; point to pointer to pointer

Often C libraries will have an initialization function that is to be passed the program's command line. Various possible parameters will be read by the library, and then removed from the command line so that the rest of the program wont think they are erroneous parameters. 

I've been trying to use such functions in ctypes. The following is an example function in C. These functions take pointers so that the new values can be filled in in the process. This example is further stripped down so as to not even manipulate the command line, just show it.

===example4.c==========================================
#include <string.h>

int filters_command_line(int *pargc, char ***pargv) {  int i;  int argc = *pargc;  char **argv = *pargv;

 for(i=0; i<argc; i++)
 {
  printf("argv[%d] = \"%s\"\n", i, argv[i]);  }

 return 0;
}
=====================================================
gcc -g -fpic -shared -o example4.so example4.c


===== two different attempts at the problem========


import sys
from ctypes import *

cdll.LoadLibrary("example4.so")
ctype_object = CDLL("example4.so")

def FiltersCommandLine(CommandLine):
	Length = len(CommandLine)
	argc = c_int(Length)

	filters_command_line = ctype_object.filters_command_line
	filters_command_line.restype = c_int
	filters_command_line.argtypes = [c_int, POINTER(POINTER(c_char_p) *Length)]

	argv = (POINTER(c_char_p) * Length) ()
	
	for i in range(Length):
		argv[i] = cast(pointer(create_string_buffer(CommandLine[i])),
POINTER(c_char_p))

	pargv = pointer(argv)

	filters_command_line(argc, pargv)


FiltersCommandLine(sys.argv)


=======================================================
import sys
from ctypes import *

cdll.LoadLibrary("example4.so")
ctype_object = CDLL("example4.so")

filters_command_line = ctype_object.filters_command_line filters_command_line.restype = c_int filters_command_line.argtypes = [c_int, c_void_p]


def FiltersCommandLine(CommandLine):
	Length = len(CommandLine)
	argc = c_int(Length)

	array = (c_void_p * Length) ()
	
	for i in range(Length):
		array[i] = cast(create_string_buffer(CommandLine[i]), c_void_p)

	pointer = c_void_p(addressof(array))

	filters_command_line(argc, pointer)


FiltersCommandLine(sys.argv)


===================================================

There has to be a way!

--
www.thomasstover.com
FLYNN LIVES!

------------------------------------------------------------------------------
Get a FREE DOWNLOAD! and learn more about uberSVN rich system, user administration capabilities and model configuration. Take the hassle out of deploying and managing Subversion and the tools developers use with it. http://p.sf.net/sfu/wandisco-d2d-2
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1