Exposing .NET Class as a COM Class

Hello,

I am having problems with exposing a single C# class as a COM Class.

I have a project which compiles to an .exe. I have a C# class which has an interface. I wish to expose this class and only this class as a COM class.

Can you help with an example? Or some steps on how to do this, without creating another seperate project.

I can create a type library and a DLL and I can register these. I can create a class wrapper in VS 6.0 but calling createdispatch fails with not registered. Then I read something about stong names but this gives another problem. Shouldn't it be easier than this!!!!?

[608 byte] By [DarrenStones] at [2008-2-4]
# 1

You should mark any classes that you do not want exposed to COM with the ComVisible(false) attribute.

Here is an example of a class that will be exposed to COM:

public interface IFoo
{
int Add(int x, int y);
}

[ClassInterface(ClassInterfaceType.None)]
public class Foo : IFoo
{
public in Add(int x, int y)
{
return x+y;
}
}

Make sure that you run regasm.exe on the managed dll...this registers it and allows COM components to create instances of the com visible classes in the managed dll ('regasm /tlb Managed.dll').

YasirAlvi at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 2
What about an exe. I can also expose an COM Interface in an exe file.
Same procedure as with a dll assembly?

thx in advance
Martin

maddin123 at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 3
I do all this and it doesn't work. Visual Studio .NET reports the library is registered correctly, but COM doesn't like it. Neither does regsvr32.
DarrenStones at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 4
Yes, the procedure is the same for an exe.

DarrenStones, does the regasm step give any errors/warnings when you do it outside of VS? If regsvr32 is failing then you might be missing some dependencies...you'll have to debug and figure out why regsvr32 is failing.

YasirAlvi at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....
# 5
Yes. If I copy it to the directory in which the client exists it works.

Thanks for the responses.

DarrenStones at 2007-9-9 > top of Msdn Tech,.NET Development,64-Bit .NET Framework Development....

.NET Development

Site Classified