Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top