Click here to Skip to main content
15,887,371 members
Articles / Desktop Programming / Win32

Grant Access to your Assembly from COM Objects

Rate me:
Please Sign up or sign in to vote.
4.80/5 (15 votes)
14 Sep 2009CPOL2 min read 286.5K   27   28   4
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:

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

namespace ComVisibleObject
{
    [Guid("EB7F73B0-AF1F-4595-8CE8-849D3745E862")]
    public interface IComVisibleObject
    {
        string MyString { get; set; }
        string GetMessage();
        void SetMessage(string value);
    }

    [Guid("DF8B1227-68EC-4F76-8C79-D20574C21B56")]
    [ComVisible(true)]
    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; }
        }

        #endregion
    }
}

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.

Steps

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

    3.JPG

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

    1.JPG

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

    2.JPG

  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".

    CoolImage.JPG

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.

VB.NET
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")
obj.SetMessage(newText)
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.

Conclusion

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.

History

  • 1st version: September 13, 2009

License

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


Written By
President
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com 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.
http://bit.ly/EXPERTCookBook

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.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to abhi2434@yahoo.com

Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com

Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Comments and Discussions

 
QuestionCom Visible a dot net dll, and access in vb6 Pin
Avneesh Sharma jaani3-Mar-12 2:19
Avneesh Sharma jaani3-Mar-12 2:19 
GeneralNot use the gacutil or regasm Pin
AcidAndroid27-Sep-09 10:33
AcidAndroid27-Sep-09 10:33 
GeneralRe: Not use the gacutil or regasm Pin
Abhishek Sur28-Sep-09 6:26
professionalAbhishek Sur28-Sep-09 6:26 
GeneralMy vote of 1 Pin
kASTRATOR15-Sep-09 20:12
kASTRATOR15-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.