Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,
I want to use an optional parameter of a function (in C/C++), how to do this? Thanks.
Posted

I see from previous solutions (they are quite right) you want to use optional parameters for a call into a dll function. for these functions you should use struct types as parameter:
typedef struct
{
  unsigned int cbSize;
  unsigned int fValid;
  int          param1;
  int          param2;
 // and so on
} DLL_PARAM;
void ExportFunction(DLL_PARAM* param)
{
  if(!param || (sizeof(DLL_PARAM)!=param->cbSize)) return;
  if(1 & param->fValid)
  {
    // param1 is valid
  }
  if(2 & param->fValid)
  {
    // param2 is valid
  }
  // and so on
}

for the caller
void usedll()
{
  DLL_PARAM  param={ sizeof(DLL_PARAM),0, };
  param.param1 = 100; param.fValid |= 1;
  // optional
  // param.param2 = 333; param.fValid |= 2;
  dllfunction(¶m);
}

Good luck.
 
Share this answer
 
Comments
Andrewpeter 20-Aug-11 22:04pm    
Thanks, my 5 for your post.
C++ has following approach.

Function overloading
C++
void func( int required );
void func( int required, int optional );


Default parameters
C++
void func( int required, int optional = 0 );
 
Share this answer
 
Comments
Andrewpeter 18-Aug-11 0:31am    
Thanks, voted.
Prerak Patel 18-Aug-11 1:07am    
OP:Thank Prerak Patel, but if I write a dll file using function overloading and export these functions, how to do in this case?
Prerak Patel 18-Aug-11 1:08am    
It's not clear to me, but did you try making a dll like this? What problem do you face? If the answer has helped you, you should mark it as answer.
Another possiblity is to use the variable argument list for C functions.

See http://msdn.microsoft.com/en-us/library/kb57fad8.aspx[^] for a detailed description of the commands and the usage.

It is used also in the known command printf() where it is possible to have more than one argument given to the function.

The function declaration would look like
int func(int iArg, ...);


Regards
Axel
 
Share this answer
 
Comments
Andrewpeter 20-Aug-11 22:04pm    
My 5. Thanks.
Best Example of function with optional variable is
printf
scanf
sprintf
 
Share this answer
 
Another possibility is this:
C++
void func( int iFirst, int iOptionalSecond = 0);


In reality, this isn't "optional" parameters, but it can be called without the second parameter, in which case it defaults to 0. So this could be called as either of these:
C++
func(5);
func(5, 20);

Hope that helps.
 
Share this answer
 
v2
Hi everyone, thank you for your supports, but I want to use C++ language to create a dll file with optional parameters and use it in VB6, e.g:

C++
int _stdcall f(int x, int option1, int option2) 

and use it in VB6:

VB
Private Declare Function f Lib "abc.dll"(Byval x As Integer, Byval Option1 As Integer, Byval Option2 As Integer)   ' Declare function
Private Sub Form_Load()
MsgBox f(2)
' Or
MsgBox f(2,1)
' Or
MsgBox f(2,1,3)
End Sub 

Hope you help me. Thanks.
 
Share this answer
 
Comments
Philippe Mori 25-Aug-11 19:57pm    
I doubt that you will be able to uses default defined in C++ from VB. I think, you would have to make a wrapper on VB side or uses overloadding. You might be able to duplicate default values in your VB declaration (but if you change the default in C++, you will have to update in in VB also).
yes you can create.
As one of the friend here hav already mentioned the use of default parameters.
you can use that concept.

create a dll as follows:

int func(int a, int b =0; bool b = false);

and while calling these functions in the vb.
if required u pass the parameters else call it with one mandatory parameters as f olows:

1) func(a = 20);
2) func(a= 20,b= 30);
3) func(a= 20,b= 30, true);

but you have to pass the mandatory parameter(parameter 1) in all the cases.


Thanks,
Arun P.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900