1 out of 16 rated this helpful - Rate this topic

Example COM Class (C# Programming Guide)

The following is an example of a class that you would expose as a COM object. After this code has been placed in a .cs file and added to your project, set the Register for COM Interop property to True. For more information, see How to: Register a Component for COM Interop.

Exposing Visual C# objects to COM requires declaring a class interface, an events interface if it is required, and the class itself. Class members must follow these rules to be visible to COM:

  • The class must be public.

  • Properties, methods, and events must be public.

  • Properties and methods must be declared on the class interface.

  • Events must be declared in the event interface.

Other public members in the class that are not declared in these interfaces will not be visible to COM, but they will be visible to other .NET Framework objects.

To expose properties and methods to COM, you must declare them on the class interface and mark them with a DispId attribute, and implement them in the class. The order in which the members are declared in the interface is the order used for the COM vtable.

To expose events from your class, you must declare them on the events interface and mark them with a DispId attribute. The class should not implement this interface.

The class implements the class interface; it can implement more than one interface, but the first implementation will be the default class interface. Implement the methods and properties exposed to COM here. They must be marked public and must match the declarations in the class interface. Also, declare the events raised by the class here. They must be marked public and must match the declarations in the events interface.


using System.Runtime.InteropServices;

namespace project_name
{
    [Guid("EAA4976A-45C3-4BC5-BC0B-E474F4C3C83F")]
    public interface ComClass1Interface
    {
    }

    [Guid("7BD20046-DF8C-44A6-8F6B-687FAA26FA71"), 
        InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ComClass1Events 
    {
    }

    [Guid("0D53A3E8-E51A-49C7-944E-E72A2064F938"),
        ClassInterface(ClassInterfaceType.None),
        ComSourceInterfaces(typeof(ComClass1Events))]
    public class ComClass1 : ComClass1Interface
    {
    }
}


Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Don't forget to set your assembly to be COM Visible
Go to the project settings (Alt+Enter) $0In the Application tab, click the Assembly Information button$0 $0Check the box that says "Make assembly COM-Visible"$0
Problem using C# Com Class from VB6. (Solved)
I solved my problem.
I modified the AssemblyInfo.cs file.
I changed the line: 
    [assembly: ComVisible(false)]
by:
    [assembly: ComVisible(true)]
I have rebuilt the project.
And now I can use my C# Com Class from my VB6 project.

I didnt write this... this is bugged
Translation to VB.Net of above C# code http://www.developerfusion.com/tools/convert/csharp-to-vb/
oh

http://msdn.microsoft.com/en-us/library/aa645712%28v=VS.71%29.aspx
Problem using C# Com Class from VB6.
I'm agree with the last comment. We need more information.

I have followed the steps of this article to make my Com Class in C#.
But it doesn't works.
I add the reference of the .tlb gererated with the C# project to a VB6 project,
and then, in the code I can't see the C# Com Class.
Windows UAC gets involved with this turned on
It should be mentioned that if this is checked, the UAC in Windows 7 and Vista will require that Visual Studio itself is configured to "run as administrator", unless there are some tricks I don't know about. This is a bit of a pain, since then double clicking on a solution file no longer starts Visual Studio and you have to confirm every manual start of it.
Article is a bit weak
This article is extremely sparse.  No Mention is made of the GUIDs, there are no example properties or methods, and no example of how to properly use the DispId that each method should have.  Please flesh out this article a bit more.
Advertisement