variadic (typesafe) args in DLL

Jarrett Billingsley kb3ctd2 at yahoo.com
Sat Feb 9 08:26:17 PST 2008


"Bjoern" <nanali at nospam-wanadoo.fr> wrote in message 
news:foic4a$1vk7$1 at digitalmars.com...

>> You declare the typesafe variadic param as so:
>>
>> uint BinaryOrPlus(uint[] vals...)
>>
>> then just use vals.
>
> How to iterate over vals  ?

.. Uh, seriously?

It's an array.

> okay let's forget about "typesafe" I can manage this in 4GL code, however 
> following the D guidelines this should work :
>
> export extern (Windows) uint BinaryOrPlus(uint firstarg, ...)
> {
>   uint result = firstarg; //_arguments[0];
>   for (int i = 1; i < _arguments.length; i++)
>   {
>      result |= *cast(uint *)_argptr;
>         _argptr += uint.sizeof;
>   }
>   return result;
> }
>
> In fact it don't work and it is looks dirty. they question here is ; what 
> is the matter with _arguments /// using Tango

extern(Windows) variadic functions don't have an _arguments parameter, only 
extern(D) variadic functions do.

According to the spec, extern(C) (and maybe Windows, it doesn't say) are 
supposed to have an _argptr parameter but it doesn't seem to work :\

So, since you're basically writing a C function, you have to do it the C 
way: with va_start and the like.

In phobos it's in std.c.stdarg, and in Tango, tango.stdc.stdarg.  You'd do 
something like:

export extern(Windows) uint BinOr(uint firstarg, ...)
{
    va_list args;
    va_start(args, firstarg);

    // it's up to you to figure out the end of the list!
    // isn't that fun?

    va_end(args);
}

Lastly -- why _are_ you using this bizarre language anyway? 




More information about the Digitalmars-d-learn mailing list