Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:creating_com_library [2012/05/14 15:59]
smayr
swdev:dotnet:creating_com_library [2012/05/16 12:40] (current)
smayr [Creating COM Library]
Line 1: Line 1:
 = Creating COM Library = = Creating COM Library =
  
-Source: [[http://stackoverflow.com/questions/3360160/how-do-i-create-an-activex-com-in-c]] +  Create new Class Library project (Eg: MyLib). Start VS2010 as administrator in order to register types and assemblies.
- +
-  * Start VS2010 as administrator. +
-  Open a Class Library project (Eg: MyLib).+
   * Interface:   * Interface:
-    * Add a new interface to the project (see example below). +    * Add to the file:<code csharp>using System.Runtime.InteropServices;</code> 
-    * Add a using ''System.Runtime.InteropServices;'' to the file. +    * Add a new interface to the project:<code csharp>public interface IMyLauncher 
-    * Add the attributes ''InterfaceType'', ''Guid'' to the interface. +
-    * You can generate a Guid using Tools > Generate GUID.+   void launch(); 
 +}</code> 
 +    * Add the attributes ''InterfaceType'', ''Guid'' to the interface. Generate a Guid using Tools > Generate GUID. <code csharp> 
 +[InterfaceType(ComInterfaceType.InterfaceIsDual)] 
 +[Guid("F207154F-C7E9-42B7-92BA-D6C4DA4D0A55")]</code>
   * Class:   * Class:
-    * Add a class that implement the interface. +    * Add a class that implement the interface.<code csharp> 
-    * Add the attributes ''ClassInterface'', ''Guid'', ''ProgId'' to the interface.+public class MyLauncher: IMyLauncher 
 +
 +    public void launch() 
 +    { 
 +    } 
 +}</code> 
 +    * Add the attributes ''ClassInterface'', ''Guid'', ''ProgId'' to the interface. Generate a Guid using Tools > Generate GUID. <code csharp> 
 +[ClassInterface(ClassInterfaceType.None)] 
 +[Guid("0BE40A98-FCAC-432D-B0B6-999B42A28E48")] 
 +[ProgId("MyLib.MyLauncher")]</code>
     * ''ProgId'' convention is ''{namespace}.{class}''     * ''ProgId'' convention is ''{namespace}.{class}''
   * Activate COM and register it:   * Activate COM and register it:
-    * In Project Properties (Alt-Tab) > Application > Assembly Information, check "Make Assembly COM-Visible". Alternatively, In the folder MyLib > Properties > AssemblyInfo, set ''ComVisible'' to true. +    * In Project Properties (Alt-Enter) > Application > Assembly Information, check "Make Assembly COM-Visible". Alternatively, In the folder MyLib > Properties > AssemblyInfo, set ''ComVisible'' to true. 
-    * In Project Properties (Alt-Tab) > Build > Output, and check "Register for COM interop".+    * In Project Properties (Alt-Enter) > Build > Output, and check "Register for COM interop".
     * Build the project. This will register the COM object.     * Build the project. This will register the COM object.
     * Alternatively, manually register COM object:<code>C:\>"%Windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe" /codebase MyLib.dll</code>     * Alternatively, manually register COM object:<code>C:\>"%Windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe" /codebase MyLib.dll</code>
  
 Now the COM object is available to be called by its ''ProgID''. Now the COM object is available to be called by its ''ProgID''.
 +== Example ==
 Example class for COM: Example class for COM:
 <code csharp> <code csharp>
Line 64: Line 74:
 Hello, World, from a COM object Hello, World, from a COM object
 </code> </code>
 +
 +== References ==
 +
 +  * [[http://stackoverflow.com/questions/3360160/how-do-i-create-an-activex-com-in-c]]
 +  * [[http://www.codeproject.com/Articles/12673/Calling-Managed-NET-C-COM-Objects-from-Unmanaged-C]]