Ejemplo 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);
        }
Ejemplo 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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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"));
            }
        }