Create a COM Object with C#?

C

Craig

This article describes a way to create a COM object with VB.Net:

http://www.codeguru.com/vb/gen/vb_general/miscellaneous/article.php/c8269/

I used the instructions under Solution 1 as a guide for creating a component
with C# that could be accessed as a COM object from VB or C++ code developed
under Visual Studio 6.0. After building the project I used regasm to
register the DLL (.Net assembly). Sure enough, that enabled me to add a
reference to the component in VB60. But in the VB Object Browser I cannot
see the Concat method/interface that the object implements. In fact, I
cannot see any methods or properties. What am I missing here? I really
need to be able to write some C# code that is accessible from code that is
purely .Net ignorant. I'm assuming a COM interface is my best option for
accomplishing this but am open to other/better suggestions.
 
C

Craig

Thanks, that definitely got me going in the right direction. I am trying to
build the "COM Interop Part 2 Sample" from:

http://msdn.microsoft.com/library/en-us/csref/html/vcwlkcominteroppart2cservertutorial.asp

Actually, building it and registering it with regasm works fine. I can add
a reference to this COM server in the VB 6.0 IDE. From the VB Object
Browser I can see the PrintHi() method in the IManagedInterface class. But
this line of VB 6.0 code:

Set myObj = New CSharpServer.InterfaceImplementation

gets this error message during the VB compile:

"Run-time error 80070002. File or assembly name CSharpServer, or one of
its dependencies was not found."

Surely I am almost there. What's the last step?

Craig
 
M

Mattias Sjögren

gets this error message during the VB compile:
"Run-time error 80070002. File or assembly name CSharpServer, or one of
its dependencies was not found."

Surely I am almost there. What's the last step?

The assembly must be located in a place where the runtime can find it.
In practice this means either install it into the GAC, in the native
application directory (the VB directory while running in the VB6 IDE)
or be registered with Regasm's /codebase switch.



Mattias
 
C

Craig

Ok, I have this working well enough now that I can invoke my C#-sourced COM
object from VB 6.0. Now, I want to be able to do CoCreateInstance() and
QueryInterface() from VS 6.0 C++. Is there a tool that will produce C++
source code that maps the interface so I can instantiate the object, get a
pointer to the interface (which will soon be just one of several exposed by
the object) and invoke methods from VS 6.0 C++? I'm pretty lame when it
comes to COM and .Net but I think I'm asking how to accomplish early binding
in my VS 6.0 C++ code.

Craig
 
M

Mattias Sjögren

Is there a tool that will produce C++
source code that maps the interface so I can instantiate the object, get a
pointer to the interface (which will soon be just one of several exposed by
the object) and invoke methods from VS 6.0 C++? I'm pretty lame when it
comes to COM and .Net but I think I'm asking how to accomplish early binding
in my VS 6.0 C++ code.

Tlbexp.exe (or Regasm.exe with its /tlb switch) can produce a type
library, which you then can use with the C++ #import preprocessor
directive.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top