views:

41

answers:

2

I tried to access a COM method by using a code like obj.Do("text") while Do is a method in obj which takes a ref string as its input (obj is a .COM object, written in VB6). However it always throw a COMException type mismatch. I tried passing obj.Do(ref a) while a is a string variable but it didn't work either.

The VB code looks like this

Function Generate(sDestinationFile As String)
    ....
Exit Function

Do you know what causes this and how should I work around it?

+1  A: 

What does the VB6 cls look like?

For example, something like this seems to work as a quick test.

VB6 cls named stringMe.cls:

Dim someString As String

Function AddString(ByRef someString)
    AddString = "Hello " & someString
End Function

I compiled this as an ActiveX DLL.

In C#, I added a reference to the DLL and coded:

static void Main(string[] args)
{
    StringMe sm = new StringMe();
    object inVar = "world!";
    string returnVar = sm.AddString(ref inVar).ToString();
    System.Console.WriteLine(returnVar);
}
Mark
edited to show how the code looks like
Louis Rhys
@Louis, I've tried a few combinations of passing byref or not, using different function signatures in the VB6 code, etc.. everyone works as expected and I can't reproduce your error. I'm leaning towards what @MarkJ says, that the exception is some place in the VB6. I'm out of ideas.
Mark
maybe you're right.. Big thanks for the effort, I'm giving you an upvote
Louis Rhys
A: 

With your VB6 component, make sure you have a Binary Compatibility reference DLL that you put aside so that on each compile it generates the same DispID's for the dll, otherwise the Interop for the .Net project will not be referencing the correct methods.

Just remember that when you recompile your VB6 component after you add methods etc, you will need to generate a new Interop for your .Net project.

Use the Commandline parameters into tlbimp to insure that you have a consistent Interop, rather than the default one that it generated when you choose Add Reference to the Com Component.

Paul Farry