Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

functions with optional arguments in Windows DLLs

by spurperl (Priest)
on Dec 12, 2007 at 15:57 UTC ( #656658=perlquestion: print w/ replies, xml ) Need Help??
spurperl has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I use the Win32::API module in some projects to access C-written DLLs on Windows machines (both system DLLs like the Win32 API, and custom written C code wrapped in DLLs). Lately, I ran into functions that accept optional arguments (supposedly they have some default value which isn't documented). The desired behavior is the one that happens when I pass nothing to the function.

Is there a way to express optional arguments with Win32::API ?

Thanks in advance

Comment on functions with optional arguments in Windows DLLs
Re: functions with optional arguments in Windows DLLs
by BrowserUk (Pope) on Dec 12, 2007 at 16:33 UTC

    I've never heard of "optional" arguments to a C routine. A variable number as with int printf( char *t, ... ), but not optional.

    If you're going to access a vararg function using Win32::API, you would need to do something hinky like create one API mapping for each number of args you are likely to use and then call the appropriate variant:

    use Win32::API; $function2 = Win32::API->new( 'mydll', 'sum_integers', 'II', 'I', ); $function3 = Win32::API->new( 'mydll', 'sum_integers', 'III', 'I', ); $function4 = Win32::API->new( 'mydll', 'sum_integers', 'IIII', 'I', ); $function5 = Win32::API->new( 'mydll', 'sum_integers', 'IIIII', 'I', ); $function6 = Win32::API->new( 'mydll', 'sum_integers', 'IIIIII', 'I', );

    Not pretty, but it ought to at least work.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Indeed, you're right about optional arguments in C. So I guess the DLL I'm looking at (rmchart) wasn't written in C, but probably some kind of Basic). This is how the particular function I'm interested in is described:
      nResult (LONG) = RMC_Draw2File( ByVal nCtrlId (LONG), ByRef sFileName (ASCIIZ), Optional ByVal nWidth (LONG), Optional ByVal nHeight (LONG), Optional ByVal nJPGQualityLevel (LONG) )
      And judging by the description, the behavior I need happens when the optional args are not provided.

        According to this, optional arguments are 'filled in' to their default values, by the compilers of languages that support them. It also says that the default values should be clearly documented.

        The upshot appears to be that you will have to pass the default values when calling from a language that does not support this behaviour. So, you will need to look up the dfault values and provide them explicitly either every time you call via Win32::API or supply a wrapper function that fills them in.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: functions with optional arguments in Windows DLLs
by GrandFather (Cardinal) on Dec 12, 2007 at 19:40 UTC

    As BrowserUK suggests, C doesn't have Optional arguments. However C++ does (with some constraints). In essence the C++ compiler takes care of it for you by providing the default value at call time if you haven't provided a value yourself. C++ also provides function overloading where different functions with the same name (but different prototypes) may be used. If you are really dealing with sparsely documented C++ then you are in a world of pain.

    Actually, if it is C++ then the situation is even worse because C++ mangles identifiers. The identifier the linker sees is not the identifier that was compiled, and there are no rules for how that mangling is done.

    Arguments that are notionally optional in C generally accept a 0 (NULL) if they are not being provided. Most often such arguments are pointers so you are simply passing a null pointer to the function.


    Perl is environmentally friendly - it saves trees

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://656658]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-09-02 12:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls