1

I know I fixed this same problem about 6 years ago...but I can't quite remember what the trick is.

I have a .NET class. It is COM visible (but not COM registered). It is returned as the result of a call to a COM registered class. So my VB6 code ends up with

Dim instance as Variant
' call .NET exposed tlb to set instance with a COM visible class
Dim wrapper as New ComWrapper  ' this is a .NET class COM exposed and registered
Set instance = wrapper.MyClassInstance ' MyClassInstance is an instance of COM visible, but not COM registered MyClass defined below
instance.DoIt 1

The relevant class is MyClass

public class MyClass
{
    public void DoIt(int id) { ... }
}

Call instance.DoIt 1 throws an exception "Object required". If I remember correctly, it has something to do with the fact that the integer 1 needs to be boxed or unboxed or something, which VB6 doesn't do automatically for you...but I can't remember quite how to fix it... If the method DoIt has no arguments, things work fine...

Anyone know how to fix this one?

Thanks.

2 Answers 2

2

It will be a boxed short, VB6 integers are 16 bits. Also, the default argument passing in VB6 is ByRef. Declaring the argument as object ought to work. It has been too long ago, but I think the syntax is wrong. It should be either

instance.DoIt 1

or

Call instance.DoIt(1)

Note the parentheses. There's little reason to do this late-bound. As long as your class is [ComVisible] then it will be present in the type library and you can just declare the VB6 variable type to allow the compiler to check your code and generate the most optimal call. Registering the class won't be necessary.

Dim instance As MyClass
Sign up to request clarification or add additional context in comments.

7 Comments

MyClass is in a separate dll and I don't want to register multiple type libraries for communication. Having to register one type library is messy enough.
You do not want to register a type library. In VB6 use Project + References, Browse to add the .tlb
As far as I'm aware, that DOES add the requirement that the dll be registered as with a tlb via regasm. This second assembly is a .NET COM Visible, non-regasm'd class library.
No, it doesn't. The only thing that needs to be registered is the class factory that creates the objects that you expect COM to create for you. ComWrapper in your case. Why are my comments so completely out of sync with your assumptions? No idea.
Yes, only the class factory that creates the objects you expect COM to create for you needs to be registered in the COM registry. NO, you can't add a reference to a .NET class library unless it's been exported to a type library (even if it's not COM registered in the registry). Try it.
|
0

Try late binding to instance by declaring it as Object.

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.