Click here to Skip to main content
13,094,732 members (70,538 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 12 Sep 2009

Grant Access to your Assembly from COM Objects

, 14 Sep 2009
Rate this:
Please Sign up or sign in to vote.
Make your class library COM visible using few simple steps

This is a very common occasion where we need to expose a .NET assembly to COM applications, so that the COM application can communicate with .NET assemblies. Here I am going to create a .NET Assembly and expose it from COM.

Your COM Exposed .NET Class

Let us create the class which I need to expose to COM. For simplicity, I made this very simple with only 2 functions which gets one string and another which sets a string. You can easily create a more complex class and test it in your application. Let us see the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ComVisibleObject
    public interface IComVisibleObject
        string MyString { get; set; }
        string GetMessage();
        void SetMessage(string value);

    public class ComVisibleObjectExample : IComVisibleObject
        private string mystring = "I am cool";

        #region IComVisibleObject Members

        public string GetMessage()
            return this.mystring;

        public void SetMessage(string value)
            this.mystring = value;

        public string MyString
            get { return this.mystring; }
            set { this.mystring = value; }


Here I have created an Interface called IComVisibleObject which might be used to create the class. Please note, it is not mandatory to create the interface. Even I didn't expose the Interface to COM.

To Uniquely Identify each class, we placed GuidAttribute in each of them. This is essential when working with COM. The class ComVisibleObjectEx is exposed to COM using ComVisibleAttribute set to true.


  1. After your class is created in Class Library, Right click on Project - > Go to Properties and select Register for COM interop.


  2. Again under the signing Tab, select Sign the Assembly. In the Choose key file combobox, select New.


  3. Choose Filename, Username and password. The Strong name key will be created automatically.


  4. Now Build the project. Say the DLL produced is ComVisibleObject.dll. Use GacUtil to put it in Global Assembly Cache. Use this:
    gacutil -i ComVisibleObject.dll

    If it is successful, it will confirm "Assembly successfully added to the cache”.

  5. To add it from COM applications, you need to register the assembly using regasm tool. Use
    regasm ComVisibleObject.dll

    After registration is successful, it will say "‘Types registered successfully".


Your COM Application

Now let us create your COM application. For simplicity, I use .vbs files. You can easily create an original VB application to test this as well. Let us look at the VBS file.

Option Explicit
Dim obj
Dim defaultText,newText
Set obj = WScript.CreateObject("ComVisibleObject.ComVisibleObjectExample")
MsgBox("Created the object.")
defaultText = obj.GetMessage()
MsgBox("Default text : " & defaultText)
newText = InputBox("Enter new Text")
newText = obj.GetMessage()
MsgBox("New text is " & newText) 

Here in the code we created an object of ComVisibleObject.ComVisibleObjectExample, and called the functions GetMessage and SetMessage. InputBox takes the input from the User and it sets to the object. Messageboxes show you the output.

Save the file as Example.vbs.

Note: You can also make a full fledged VB application to test this, and I am sure it will work just like this.

To Run

Open Command prompt and type cscript Example.vbs or directly double click on the file.


It is very easy to connect your .NET assembly to a COM environment. Hope you like this post. Please comment to enrich this further.
The sample application can be found here.


  • 1st version: September 13, 2009


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhishek Sur
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.

You can also mail him directly to

Want a Coder like him for your project?
Drop him a mail to

Visit His Blog

Dotnet Tricks and Tips

Dont forget to vote or share your comments about his Writing

You may also be interested in...


Comments and Discussions

QuestionCom Visible a dot net dll, and access in vb6 Pin
Avneesh Sharma jaani3-Mar-12 2:19
memberAvneesh Sharma jaani3-Mar-12 2:19 
GeneralNot use the gacutil or regasm Pin
AcidAndroid27-Sep-09 10:33
memberAcidAndroid27-Sep-09 10:33 
GeneralRe: Not use the gacutil or regasm Pin
Abhishek Sur28-Sep-09 6:26
memberAbhishek Sur28-Sep-09 6:26 
GeneralMy vote of 1 Pin
kASTRATOR15-Sep-09 20:12
memberkASTRATOR15-Sep-09 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 14 Sep 2009
Article Copyright 2009 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid