PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CppExeCOMServer/CppExeCOMServer.cpp

#
C++ | 131 lines | 85 code | 19 blank | 27 comment | 18 complexity | 086a165eff718cbb5200b8dae4252748 MD5 | raw file
  1/****************************** Module Header ******************************\
  2Module Name:  CppExeCOMServer.cpp
  3Project:      CppExeCOMServer
  4Copyright (c) Microsoft Corporation.
  5
  6
  7
  8This source is subject to the Microsoft Public License.
  9See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 10All other rights reserved.
 11
 12THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 13EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 14WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 15\***************************************************************************/
 16
 17#include <windows.h>
 18#include <strsafe.h>
 19#include "CppExeCOMServer_i.c"      // For component GUIDs
 20#include "Reg.h"
 21
 22
 23STDAPI ExeRegisterServer(void);
 24STDAPI ExeUnregisterServer(void);
 25
 26
 27
 28int APIENTRY wWinMain(HINSTANCE hInstance,
 29                      HINSTANCE hPrevInstance,
 30                      LPWSTR lpCmdLine,
 31                      int nCmdShow)
 32{
 33    HRESULT hr; 
 34
 35    if (*lpCmdLine == L'-' || *lpCmdLine == L'/')
 36    {
 37        if (_wcsicmp(L"regserver", lpCmdLine + 1) == 0)
 38        {
 39            // Register the COM server.
 40            hr = ExeRegisterServer();
 41            if (FAILED(hr))
 42            {
 43            }
 44        }
 45        else if (_wcsicmp(L"unregserver", lpCmdLine + 1) == 0)
 46        {
 47            // Unregister the COM server.
 48            hr = ExeUnregisterServer();
 49            if (FAILED(hr))
 50            {
 51            }
 52        }
 53    }
 54    else
 55    {
 56        // Call PreMessageLoop to initialize the member variables and 
 57        // register the class factories.
 58        hr = PreMessageLoop();
 59        
 60        if (SUCCEEDED(hr))
 61        {
 62            __try
 63            {
 64                // Run the message loop.
 65                RunMessageLoop();
 66            }
 67            __finally
 68            {
 69                // Call PostMessageLoop to revoke the registration.
 70                hr = PostMessageLoop();
 71            }
 72        }
 73    }
 74
 75	return hr;
 76}
 77
 78
 79// Register the COM server.
 80STDAPI ExeRegisterServer(void)
 81{
 82    HRESULT hr;
 83
 84    wchar_t szModule[MAX_PATH];
 85    if (GetModuleFileName(NULL, szModule, ARRAYSIZE(szModule)) == 0)
 86    {
 87        hr = HRESULT_FROM_WIN32(GetLastError());
 88        return hr;
 89    }
 90
 91    // Register the component.
 92    hr = RegisterLocalServer(szModule, 
 93        CLSID_SimpleObject, 
 94        L"CppExeCOMServer.SimpleObject Class", 
 95        LIBID_CppExeCOMServerLib,
 96        L"CppExeCOMServer.SimpleObject.1", 
 97        L"CppExeCOMServer.SimpleObject");
 98    if (SUCCEEDED(hr))
 99    {
100        // Register the type library.
101        hr = RegisterTypeLib(szModule);
102    }
103
104    return hr;
105}
106
107
108// Unregister the COM server.
109STDAPI ExeUnregisterServer(void)
110{
111    HRESULT hr = S_OK;
112
113    wchar_t szModule[MAX_PATH];
114    if (GetModuleFileName(NULL, szModule, ARRAYSIZE(szModule)) == 0)
115    {
116        hr = HRESULT_FROM_WIN32(GetLastError());
117        return hr;
118    }
119
120    // Unregister the component.
121    hr = UnregisterLocalServer(CLSID_SimpleObject, 
122        L"CppExeCOMServer.SimpleObject.1", 
123        L"CppExeCOMServer.SimpleObject");
124    if (SUCCEEDED(hr))
125    {
126        // Unregister the type library.
127        hr = UnregisterTypeLib(szModule);
128    }
129
130    return hr;
131}