This is an old revision of the document!


Creating COM Library

Source: http://stackoverflow.com/questions/3360160/how-do-i-create-an-activex-com-in-c

  • Start VS2010 as administrator.
  • Open a Class Library project (Eg: MyProject).
  • Add a new interface to the project (see example below).
  • Add a using System.Runtime.InteropServices; to the file.
  • Add the attributes InterfaceType, Guid to the interface.
  • You can generate a Guid using Tools > Generate GUID.
  • Add a class that implement the interface.
  • Add the attributes ClassInterface, Guid, ProgId to the interface.
  • ProgId convention is {namespace}.{class}
  • Under the prperties folder in the project in the asseblyInfo file set ComVisible to true.
  • In the project properties menu, in the build tab mark “Register for COM interop”.
  • Build the project.

Now the COM object is available to be called by its ProgID.

Example class for COM:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Runtime.InteropServices;
 
namespace Launcher
{
 
    [InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
    public interface IMyLauncher
    {
        void launch();
    }
 
    [ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("Launcher.MyLauncher")]
    public class MyLauncher: IMyLauncher
    {
        private string path = null;
 
        public void launch()
        {
            Console.WriteLine("Hello, World, from a COM object");
 
        }
 
    }
}

Example how to call COM object using VB script:

set obj = createObject("PSLauncher.PSMyLauncher") obj.launch()

Output:

Hello, World, from a COM object