Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible that C# has problems calling VB6 methods that have optional parameters?

I added 3 optional boolean parameters to a method found within a VB6 DLL. The class that houses it is MultiUse (public), and the method itself is Private. The class implements a specific interface from a TLB, allowing for public calls to this method.

After adding the 3 optional parameters on the VB6 side, I modified related C# code so that it specified the 3 optional parameters. It built fine... however, when I try to run that code, it fails with the following error message:

Method not found: 'Boolean MyTLBName.IMyClassName.MyMethod(System.Object, System.String, Boolean, Boolean, Int32, Int32 ByRef, System.Object, System.Object, System.Object, Boolean, Boolean, Boolean)'.

Notice how all 3 boolean parameters are shown in the error message? Looks fine to me... I know I specified those 3 booleans when calling the method from C#.

Suspicious, I checked out the MyTLBName.IMyClassName interface in OLEView, and saw this:

[id(0x60030000)]

HRESULT MyMethod(
                //Cut out the other parameters - they are working fine.
                [in, optional, defaultvalue(-1)] VARIANT_BOOL blnMyFirstOptionalBoolean, 
                [in, optional, defaultvalue(-1)] VARIANT_BOOL blnMySecondOptionalBoolean, 
                [in, optional, defaultvalue(-1)] VARIANT_BOOL blnMyThirdOptionalBoolean, 
                [out, retval] VARIANT_BOOL* __MIDL_0324);

Again, the 3 optional parameters are visible, and look fine.

Seems to me like it should work... but maybe I'm missing something.

Is there any way I can get this working without having to create another version of "MyMethod" in the TLB? (With a different name, and those 3 parameters as required rather than optional)

like image 726
Matt Refghi Avatar asked Nov 06 '09 15:11

Matt Refghi


1 Answers

Currently, C# does have problems calling VB6 functions with optional parameters. I fought very hard with this one on a project where I absolutely needed to use the VB6 component provided by an external vendor.

I found that the easiest solution was to create a class in VB to call those functions, since VB.Net DOES support optional parameters, and then call that VB6 class from C#.

In the class written in VB.Net, I followed the proper method overloading to provide several methods to call that one VB6 function.

Example - say the VB6 function has three parameters, the first one required, and the second two optional, I'd create three functions in the VB.Net component.

Public Sub CallComponent(byVal myParameter as Integer)
  ....call component with one parm
End Sub

Public Sub CallComponent(byVal myParameter as Integer, byVal parm2 as  String,)
  ....call component with two parms
End Sub

etc...

And it looks like optional parameters are coming in 4.0: http://geekswithblogs.net/michelotti/archive/2009/02/05/c-4.0-optional-parameters.aspx

like image 83
David Avatar answered Oct 20 '22 19:10

David