Advertisement
Cleric-K

COM Register

Mar 8th, 2018
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Runtime.InteropServices.ComTypes;
  4.  
  5. namespace comtest
  6. {
  7.     [ComVisible(true)]
  8.     [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  9.     [Guid("9009311a-c0b2-42a4-8e7c-f42091d71594")]
  10.     public interface ITestEvents {
  11.         void OnEvent();
  12.     }
  13.    
  14.     [ComVisible(true)]
  15.     [ClassInterface(ClassInterfaceType.AutoDispatch)]
  16.     [ComSourceInterfaces(typeof(ITestEvents))]
  17.     public class ComClass {
  18.         public event Action OnEvent;
  19.        
  20.         public int Test() {
  21.             return 100;
  22.         }
  23.     }
  24.        
  25.     class Program
  26.     {
  27.         [DllImport("ole32.dll")]
  28.         static extern int GetRunningObjectTable(uint reserved, out System.Runtime.InteropServices.ComTypes.IRunningObjectTable pprot);
  29.        
  30.         [DllImport("ole32.dll")]
  31.         static extern int CreateFileMoniker([MarshalAs(UnmanagedType.LPWStr)] string lpszPathName, out System.Runtime.InteropServices.ComTypes.IMoniker ppmk);
  32.  
  33.         public static void Main(string[] args)
  34.         {
  35.             IRunningObjectTable rot;
  36.             IMoniker mon;
  37.            
  38.             var obj = new ComClass();
  39.            
  40.             GetRunningObjectTable(0, out rot);
  41.            
  42.             CreateFileMoniker("comTestApp", out mon);
  43.            
  44.             int ROTFLAGS_REGISTRATIONKEEPSALIVE = 1;
  45.             rot.Register(ROTFLAGS_REGISTRATIONKEEPSALIVE, obj, mon);
  46.            
  47.             System.Threading.Thread.Sleep(3600000); // 1 hour
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement