Using a C function with command line parameters

Steven Schveighoffer schveiguy at yahoo.com
Mon Jul 4 09:59:09 PDT 2011


On Mon, 04 Jul 2011 12:51:48 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2011-07-04 16:31, Jonathan Sternberg 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.
>
> It depends on what your needs are. If your not actually using the  
> arguments then you can pass in 0 and null, or 1 and the name of the  
> application.

A typical library that uses standardized arguments (i.e. parameters that  
always mean the same thing across applications that use that library) has  
a parameter pre-processing function such as this which "consume" their  
specific arguments from the command line.  The idea is, you pass the  
command line to those functions, the library processes its specific  
arguments, removing them from the command line array, and then all  
applications using that library have a way to control the library from the  
command line.  I think the first one I remember having this is X and Xt.   
For example, most X-based applications you can pass --display=mysystem:0  
and it uses that display.  The applications simply use a libX function  
that processes those args from the command line and never have to deal  
with it.

> As an alternative, most platforms have an API for getting the arguments  
> passed to an application.
>
> Mac OS X: extern (C) char*** _NSGetEnviron();
> Posix: extern (C) extern char** environ;

Those get the environment variables, not the command line arguments.

> Don't know about Windows.

Windows actually does have a command line fetching function.  Can't  
remember what it is, but if I had to guess I'd say it was GetCommandLine :)

-Steve


More information about the Digitalmars-d-learn mailing list