Using a C function with command line parameters

Jacob Carlborg doob at me.com
Mon Jul 4 23:48:13 PDT 2011


On 2011-07-04 18:59, Steven Schveighoffer wrote:
> 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.

Hehe. Wonder what I was thinking. Anyway, the platforms usually do have 
an API for that, second try:

Mac OS X:
extern(C) char*** _NSGetArgv();
extern(C) int* _NSGetArgc();

Linux:
I read somewhere you could read from /proc/<pid>/cmdline
Where <pid> is the process id.

>> 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

Seems to be that one.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list