5

I created a C# COM accessible dll that I want to consume in VB6 I was able to consume in VB6 my COM object with a hard reference to the TLB. What I am trying to do now is to remove this reference and load it dynamically I am creating it as follows:

Dim keylok As Object
Set keylok = CreateObject("MyClassLib.MyObject")

I get the Run-time error 424 "Object Required" once I hit the second line. But when I create it as follows:

Dim keylok As MyObject
Set keylok = CreateObject("MyClassLib.MyObject")

It works fine. I am not sure why would that make a difference. Anyway I cannot use the second one because I would still need to have the physical reference.

I tried also as a sort of debugging to write to file in my COM object constructor to if it really gets called. And yes it does, I'm even able to call other methods in my COM object sucessfully inside the constructor.

I was even able to load dynamically and consume it from another C# app using:

dynamic myObj = Activator.CreateInstance(Type.GetTypeFromProgID("MyClassLib.MyObject"));

Did any one encounter something like that before?

7
  • 4
    Read support.microsoft.com/kb/245115 , then give deep thought to how late binding works differently from early; in particular, can you confirm a valid IDispatch for your object? Commented Apr 18, 2012 at 17:12
  • I was able to late bind it in C#. So, I would assume that my component generally is capable of being late bound. And actually I got my constructor called too. Commented Apr 18, 2012 at 18:13
  • Some extra information: My C# dll was built in VS 2010 (.NET 4.0) Commented Apr 18, 2012 at 18:35
  • I still wonder if that Activator.CreateInstance(T) is working via the CCW generated from the typelib rather than IDispatch. For the giggles, try the project at codeproject.com/Articles/74528/C-COM-Late-Binding-Event , and if that works, compare/contrast that with what you're doing in your project. Commented Apr 18, 2012 at 19:09
  • You did register both the .dll using regasm and the .tlb file using regsrvr32 in both scenarios, didnt't you? Commented Apr 18, 2012 at 21:27

2 Answers 2

3

I found the solution with the help of @rskar input. So, I thought I'm gonna answer my question, in case any one faces the same problem.

My object didn't impelement IDsipatch. So all I had to do it to decorate my C# COM interface with InterfaceType(ComInterfaceType.InterfaceIsDual) So it implements both IUnknown and IDispatch. Originally it was decorated with InterfaceType(ComInterfaceType.InterfaceIsIUnknown)

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are going to require the .tlb anyway. COM objects need to be capable of being marshalled as the .Net hosting runs on a different thread to the VB6 runtime. The default marshalling uses information from the typelibrary to do this. IDIspatch has 4 methods and 2 of these are to do with accessing type information. So possibly if you removed the .tlb, when you create the IDispatch COM attempts to call up the ITypeInfo from this and dies failing to load the registered typelibrary. If you eliminate the .tlb you will become unable to be marshalled and likely you would have to provide a custom marshaller for your interface.

2 Comments

There is no "net hosting" that runs on a different thread. ComVisible classes are created and called on the main VB6 STA thread. VB6 doesn't otherwise support threading in a reliable way so no marshaling ever needs to occur. Nor does Regasm do anything to register interfaces.
Thanks for your response. The solution was really simple. The problem was that my C# COM interface didn't impelement IDispatch. Please check my last comment on my post.

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.