3

I have made a DLL which exports several functions (with stdcall). I want to have some of them accept parameters or not. So a lazy programmer can just call it without any parameters. I read somewhere on a forum that default parameters don't work in DLL-s. Is my only option is creating 2 functions with different names, like:

procedure DoSomething();
begin
  DoSomethingParams(1, 'Hi');
end;

procedure DoSomethingParams(one: Integer; two: PChar);
begin
//
end; 

? Or maybe there is a more elegant way to achieve this?

Tom
  • 2,962
  • 3
  • 39
  • 69
  • 1
    "Default parameters" are language-dependent concept. Some languages don't have them — how will they see your DLL? AFAIK, procedures with default parameters are just usual procedures, and when you omit parameters at the call site, the compiler inserts them for you. – Joker_vD May 07 '13 at 13:07

1 Answers1

6

Default parameters can be used with DLLs. But the default parameters must be declared when the function is imported rather than when it is exported. That's because default parameters are implemented at the call site. The caller detects that parameters are missing and generates code to supply the missing parameters.

So you can use default parameters when you import the DLL, provided that the language that consumes the DLL supports that.

  • In the DLL code, export the function. You can specify default parameters there if you wish, but it won't have any effect for the consumer of the DLL.
  • In the code that imports the DLL function, declare your default parameters. It is the default values declared at this point that matter.

Since DLLs are typically used to provide language neutral interfaces, and since some languages do not support default parameters, it is rare to use them in DLL interfaces.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Good to know, thanks. But what if my DLL-user is a super lazy person and doesn't want to declare function with parameters? Is my solution the only way? Why is this important for me? I have a DLL that exports a few functions. I want to give a DLL-user option to call all the functions in a similar matter. It most cases it will be fine and results will be great. But if my DLL-user wants, he can specify params to tune results of my DLL. – Tom May 07 '13 at 13:49
  • You know what the options are. I don't think there's much to add to my answer. If you asked a more specific question than in that comment maybe I could clarify. – David Heffernan May 07 '13 at 13:52
  • 1
    Tom, the DLL user must *declare* the function with all the parameters accounted for; omitting parameters from a declaration will lead to any number of problems down the line. However, that user is free to assign default values to the parameters in the declaration so that he or she can be lazy when *calling* the function. If *you* declare the functions, then the users can be super-lazy because they won't have to declare the functions at all. They can just use the declarations you've already provided when you distributed the DLL. As a DLL distributor, you should be doing that anyway. – Rob Kennedy May 07 '13 at 15:10
  • @RobKennedy I will provide a wrapper, but not for all the possible languages out there. – Tom May 07 '13 at 16:58
  • 1
    Of course not, Tom. Bindings for the DLL's native language (Delphi, in this case) and for C are usually sufficient. – Rob Kennedy May 07 '13 at 17:13