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:45]
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
- +  * Interface: 
-  Start VS2010 as administrator. +    * Add to the file:<code csharp>using System.Runtime.InteropServices;</code> 
-  * Open a Class Library project (Eg: MyProject). +    * Add a new interface to the project:<code csharp>public interface IMyLauncher 
-  * Add a new interface to the project (see example below)+
-  * Add using ''System.Runtime.InteropServices;'' to the file. +   void launch(); 
-  * Add the attributes ''InterfaceType'', ''Guid'' to the interface. +}</code> 
-  * You can generate a Guid using Tools > Generate GUID. +    * Add the attributes ''InterfaceType'', ''Guid'' to the interface. Generate a Guid using Tools > Generate GUID. <code csharp> 
-  * Add a class that implement the interface. +[InterfaceType(ComInterfaceType.InterfaceIsDual)] 
-  * Add the attributes ''ClassInterface'', ''Guid'', ''ProgId'' to the interface. +[Guid("F207154F-C7E9-42B7-92BA-D6C4DA4D0A55")]</code> 
-  * ''ProgId'' convention is ''{namespace}.{class}'' +  * Class: 
-  * Under the prperties folder in the project in the asseblyInfo file set ''ComVisible'' to true. +    * Add a class that implement the interface.<code csharp> 
-  * In the project properties menuin the build tab mark "Register for COM interop"+public class MyLauncher: IMyLauncher 
-  * Build the project.+
 +    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}'' 
 +  * Activate COM and register it: 
 +    * 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-Enter) > Build > Outputand check "Register for COM interop"
 +    * 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>
  
 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 27: Line 41:
 using System.Runtime.InteropServices; using System.Runtime.InteropServices;
  
-namespace Launcher+namespace MyLib
 { {
  
Line 36: Line 50:
     }     }
  
-    [ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("Launcher.MyLauncher")]+    [ClassInterface(ClassInterfaceType.None), Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYY"), ProgId("MyLib.MyLauncher")]
     public class MyLauncher: IMyLauncher     public class MyLauncher: IMyLauncher
     {     {
Line 53: Line 67:
 Example how to call COM object using VB script: Example how to call COM object using VB script:
 <code vb> <code vb>
-set obj = createObject("PSLauncher.PSMyLauncher") obj.launch()+set obj = createObject("PSMyLib.PSMyLauncher") obj.launch()
 </code> </code>
  
Line 60: 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]]