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