0

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
  • Could the error be about 'obj', rather than the string argument? Or possibly the argument is recognised and the call is successfully marshalled, but then an exception happens in the vb6 code? Commented Aug 18, 2010 at 12:19

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@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.
maybe you're right.. Big thanks for the effort, I'm giving you an upvote
0

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.

Comments

0

for calling vb6 component from .net you must do like this

var array = new string[20];
var x = new System.Runtime.InteropServices.VariantWrapper(array as object);
var x2 = x as object;
comComponent.callFunction(ref x2);

this work for me other solution retrun type mismatch error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.