[gambit-list] FFI **Argv

Marc Feeley feeley at iro.umontreal.ca
Tue Apr 16 07:19:40 EDT 2013


On 2013-04-16, at 6:53 AM, <sales.creditscore at creditscore.co.nz> wrote:

> Hi,
> 
> I'm trying to call a C foreign function which uses command-line arguments...
> 
> ;
> ;Isectd echo worker in Scheme
> ;
> (c-declare "#include \"isdio.h\"")
> 
> (define isdLogInit (c-lambda (int (pointer (pointer char)#f)) void "isdLogInit"))
> (isdLogInit (-(length (command-line))1) (command-line))
> 
> The FFI C function isdLogInit function is:
> 
> Xport void isdLogInit(int argc, char **argv)
> {
> 	int i;
> 	char isdLogFileName[1024];
> 
> 	sprintf(isdLogFileName, "/isect/logs/%s.log", argv[1]);
> 
> 	isdLogFile = fopen(isdLogFileName, "a");
> 	if (isdLogFile == NULL)
> 		isdLogFile = stderr;
> 
> 	isdLog("\nSTARTING\n");
> 	for (i = 0; i < argc; i++)
> 		isdLog("%s ", argv[i]);
> }
> 
> It compiles OK, but when I run it, I get the message (Argument 2) Can't convert to C pointer.  That's because (command-line) is a list of strings.  Seems I can't cast to the required char **argv using Scheme.  How do I capture the command-line as the correct type.  Sorry, if this is straightforward.  I'm not an academic or a professional programmer, just an electrician who likes to play with things to see how they work.  If I can understand Scheme then I might eventually be able to contribute to GnuCash which I use in my business every day. 

In C the type char* usually represents a string, but it can also represent a generic pointer to memory.  You have to tell the Gambit Scheme compiler what your intention is, so that it knows how to convert Scheme strings to that type.  So instead of (pointer char) you have to use nonnull-char-string .  Your array of strings is also terminated by NULL, so you need to say that too.  This is the type nonnull-char-string-list .  Use that instead of (pointer (pointer char) #f) .

Marc




More information about the Gambit-list mailing list