Using a C function with command line parameters
Steven Schveighoffer
schveiguy at yahoo.com
Mon Jul 4 07:39:07 PDT 2011
On Mon, 04 Jul 2011 10:31:58 -0400, Jonathan Sternberg
<jonathansternberg at gmail.com> wrote:
> glut has the function:
>
> void glutInit( int* pargc, char** argv );
>
> In order to use it. Since D has an ABI compatible with C, I should be
> able to
> write a D file with extern (C) on the glut functions. How would I wrap
> this
> function to be used with D arrays? Such as:
>
> int main(string[] args)
> {
> glutInit( /* I don't know what to do here */ );
> return 0;
> }
>
> Thanks.
Convert all the args to zero-terminated strings. One thing to look at,
does glutInit actually modify any of the strings or does it just remove
them from the argv array? Because I would expect the argv argument to be
const(char)** if it doesn't modify. That would allow you to pass in the
strings without having to duplicate them.
Some untested code:
auto argarr = new immutable(char)*[args.length];
foreach(i, a; args)
argarr[i] = (a.dup ~ '\0').ptr;
int argc = argarr.length;
glutInit(&argc, argarr.ptr);
// now, need to refix args to ignore arguments already consumed by glutInit
...
-Steve
More information about the Digitalmars-d-learn
mailing list