Exemplo n.º 1
0
        /// <summary>
        /// PreMessageLoop is responsible for registering the COM class
        /// factories for the COM classes to be exposed from the server, and
        /// initializing the key member variables of the COM server (e.g.
        /// _nMainThreadID and _nLockCnt).
        /// </summary>
        private void PreMessageLoop()
        {
            //
            // Register the COM class factories.
            //

            Guid clsidSimpleObj = new Guid(SimpleObject.ClassId);

            // Register the SimpleObject class object
            int hResult = COMNative.CoRegisterClassObject(
                ref clsidSimpleObj,                 // CLSID to be registered
                new SimpleObjectClassFactory(),     // Class factory
                CLSCTX.LOCAL_SERVER,                // Context to run
                REGCLS.MULTIPLEUSE | REGCLS.SUSPENDED,
                out _cookieSimpleObj);

            if (hResult != 0)
            {
                throw new ApplicationException(
                          "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X"));
            }

            // Register other class objects
            // ...

            // Inform the SCM about all the registered classes, and begins
            // letting activation requests into the server process.
            hResult = COMNative.CoResumeClassObjects();
            if (hResult != 0)
            {
                // Revoke the registration of SimpleObject on failure
                if (_cookieSimpleObj != 0)
                {
                    COMNative.CoRevokeClassObject(_cookieSimpleObj);
                }

                // Revoke the registration of other classes
                // ...

                throw new ApplicationException(
                          "CoResumeClassObjects failed w/err 0x" + hResult.ToString("X"));
            }

            //
            // Initialize member variables.
            //

            // Records the ID of the thread that runs the COM server so that
            // the server knows where to post the WM_QUIT message to exit the
            // message loop.
            _nMainThreadID = NativeMethod.GetCurrentThreadId();

            // Records the count of the active COM objects in the server.
            // When _nLockCnt drops to zero, the server can be shut down.
            _nLockCnt = 0;

            // Start the GC timer to trigger GC every 5 seconds.
            _gcTimer = new Timer(new TimerCallback(GarbageCollect), null,
                                 5000, 5000);
        }
Exemplo n.º 2
0
        private void PreMessageLoop()
        {
            Guid clsidSimpleObj = new Guid(CacheConnector.ClassId);

            int hResult = COMNative.CoRegisterClassObject(
                ref clsidSimpleObj,
                new CacheConnectorClassFactory(),
                CLSCTX.LOCAL_SERVER,
                REGCLS.MULTIPLEUSE | REGCLS.SUSPENDED,
                out _cookieSimpleObj);

            if (hResult != 0)
            {
                throw new ApplicationException(
                          "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X"));
            }

            hResult = COMNative.CoResumeClassObjects();
            if (hResult != 0)
            {
                if (_cookieSimpleObj != 0)
                {
                    COMNative.CoRevokeClassObject(_cookieSimpleObj);
                }

                throw new ApplicationException(
                          "CoResumeClassObjects failed w/err 0x" + hResult.ToString("X"));
            }

            _nMainThreadID = NativeMethod.GetCurrentThreadId();
            _nLockCnt      = 0;
            _gcTimer       = new Timer(new TimerCallback(GarbageCollect), null,
                                       5000, 5000);
        }
Exemplo n.º 3
0
        /// <summary>
        /// PostMessageLoop is called to revoke the registration of the COM
        /// classes exposed from the server, and perform the cleanups.
        /// </summary>
        private void PostMessageLoop()
        {
            //
            // Revoke the registration of the COM classes.
            //

            // Revoke the registration of SimpleObject
            if (_cookieSimpleObj != 0)
            {
                COMNative.CoRevokeClassObject(_cookieSimpleObj);
            }

            // Revoke the registration of other classes
            // ...

            //
            // Perform the cleanup.
            //

            // Dispose the GC timer.
            if (_gcTimer != null)
            {
                _gcTimer.Dispose();
            }

            // Wait for any threads to finish.
            Thread.Sleep(1000);
        }
Exemplo n.º 4
0
        /// <summary>
        /// PostMessageLoop被用于撤销对服务器可见的COM类并且执行清理。
        /// </summary>
        private void PostMessageLoop()
        {
            /////////////////////////////////////////////////////////////////
            // 撤销COM类的注册

            // 撤销CSSimpleObject的注册
            if (_cookieSimpleObj != 0)
            {
                COMNative.CoRevokeClassObject(_cookieSimpleObj);
            }

            // 撤销其他类
            // ...


            /////////////////////////////////////////////////////////////////
            // 执行清理

            // 关闭GC计时器
            if (_gcTimer != null)
            {
                _gcTimer.Dispose();
            }

            //等待其他进程关闭
            Thread.Sleep(1000);
        }
Exemplo n.º 5
0
        /// <summary>
        /// PreMessageLoop负责注册COM类工厂,并且初始化COM服务器的关键成员变量
        /// (比如_nMainThreadID和_nLockCnt)
        /// </summary>
        private void PreMessageLoop()
        {
            /////////////////////////////////////////////////////////////////
            // 注册COM类工厂
            //

            Guid clsidSimpleObj = new Guid(CSSimpleObject.ClassId);

            // 注册CSSimpleObject类对象
            int hResult = COMNative.CoRegisterClassObject(
                ref clsidSimpleObj,                 // 即将被注册的CLSID
                new CSSimpleObjectClassFactory(),   // 类工厂
                CLSCTX.LOCAL_SERVER,                // 执行代码块
                REGCLS.MULTIPLEUSE | REGCLS.SUSPENDED,
                out _cookieSimpleObj);

            if (hResult != 0)
            {
                throw new ApplicationException(
                          "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X"));
            }

            // 注册其他类对象
            // ...

            // 通知SCM所有的已注册类以及在服务器进程中开始激活请求。
            hResult = COMNative.CoResumeClassObjects();
            if (hResult != 0)
            {
                // 在失败时撤销 CSSimpleObject的注册
                if (_cookieSimpleObj != 0)
                {
                    COMNative.CoRevokeClassObject(_cookieSimpleObj);
                }

                // 撤销其他类的注册
                // ...

                throw new ApplicationException(
                          "CoResumeClassObjects failed w/err 0x" + hResult.ToString("X"));
            }


            /////////////////////////////////////////////////////////////////
            // 初始化成员变量

            // 记录COM服务器当前运行中的线程ID。这样,服务器可以知道哪里可以发送
            // WM_QUIT消息,以便退出消息循环。
            _nMainThreadID = NativeMethod.GetCurrentThreadId();

            // 记录在服务器中激活的COM对象的数目。当_nLockCnt为0时,服务器可以关闭。
            _nLockCnt = 0;

            // 启动GC计时器以触发每5秒一次的GC。
            _gcTimer = new Timer(new TimerCallback(GarbageCollect), null,
                                 5000, 5000);
        }
Exemplo n.º 6
0
        protected override void OnStop()
        {
            // Revoke the registration of SimpleObject on stop
            if (_cookieSimpleObj != 0)
            {
                COMNative.CoRevokeClassObject(_cookieSimpleObj);
            }

            // Revoke the registration of other classes
            // ...
        }
Exemplo n.º 7
0
        private void PostMessageLoop()
        {
            if (_cookieSimpleObj != 0)
            {
                COMNative.CoRevokeClassObject(_cookieSimpleObj);
            }

            if (_gcTimer != null)
            {
                _gcTimer.Dispose();
            }

            Thread.Sleep(1000);
        }
Exemplo n.º 8
0
        /// <summary>
        /// PostMessageLoop is called to revoke the registration of the COM
        /// classes exposed from the server, and perform the cleanups.
        /// </summary>
        private void PostMessageLoop()
        {
            // Revoke the registration of  COM classes
            if (_cookie != 0)
            {
                COMNative.CoRevokeClassObject(_cookie);
            }

            // Dispose the GC timer.
            if (_gcTimer != null)
            {
                _gcTimer.Dispose();
            }

            // Wait for any threads to finish.
            Thread.Sleep(1000);
        }
Exemplo n.º 9
0
        protected override void OnStart(string[] args)
        {
            Guid clsidSimpleObj = new Guid(SimpleObject.ClassId);

            // Register the SimpleObject class object on start
            int hResult = COMNative.CoRegisterClassObject(
                ref clsidSimpleObj,               // CLSID to be registered
                new SimpleObjectClassFactory(),   // Class factory
                CLSCTX.LOCAL_SERVER,              // Context to run
                REGCLS.MULTIPLEUSE | REGCLS.SUSPENDED,
                out _cookieSimpleObj);

            if (hResult != 0)
            {
                throw new ApplicationException(
                          "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X"));
            }

            // Register other class objects
            // ...

            // Inform the SCM about all the registered classes, and begins
            // letting activation requests into the server process.
            hResult = COMNative.CoResumeClassObjects();

            if (hResult != 0)
            {
                // Revoke the registration of SimpleObject
                if (_cookieSimpleObj != 0)
                {
                    COMNative.CoRevokeClassObject(_cookieSimpleObj);
                }

                throw new ApplicationException(
                          "CoResumeClassObjects failed w/err 0x" + hResult.ToString("X"));
            }
        }
Exemplo n.º 10
0
        public COMService()
        {
            InitializeComponent();

            // Initialize COM security
            int hResult = COMNative.CoInitializeSecurity(
                IntPtr.Zero,    // Add your security descriptor here
                -1,
                IntPtr.Zero,
                IntPtr.Zero,
                RPC_C_AUTHN_LEVEL.PKT_PRIVACY,
                RPC_C_IMP_LEVEL.IDENTIFY,
                IntPtr.Zero,
                EOLE_AUTHENTICATION_CAPABILITIES.DISABLE_AAA |
                EOLE_AUTHENTICATION_CAPABILITIES.SECURE_REFS |
                EOLE_AUTHENTICATION_CAPABILITIES.NO_CUSTOM_MARSHAL,
                IntPtr.Zero);

            if (hResult != 0)
            {
                throw new ApplicationException(
                          "CoIntializeSecurity failed w/err 0x" + hResult.ToString("X"));
            }
        }