argc argv filters; pointer to array of strings; point to pointer to pointer
Thomas Stover <[email protected]> Tue, 16 Aug 2011 16:08:41 -0500
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <[email protected]> |
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