PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CppRegFreeCOMClient/CppRegFreeCOMClient.cpp

#
C++ | 112 lines | 50 code | 21 blank | 41 comment | 1 complexity | 448ff55cd43ba98ff0fcaa0f2400e6a7 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: CppRegFreeCOMClient.cpp
  3. * Project: CppRegFreeCOMClient
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. *
  7. *
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  14. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  16. \***************************************************************************/
  17. #pragma region Includes and Imports
  18. #include <stdio.h>
  19. #include <tchar.h>
  20. #import "CSRegFreeCOMServer.tlb" no_namespace named_guids
  21. #pragma endregion
  22. int _tmain(int argc, _TCHAR* argv[])
  23. {
  24. HRESULT hr;
  25. // Initializes the COM library on the current thread and identifies the
  26. // concurrency model as single-thread apartment (STA).
  27. CoInitializeEx(0, COINIT_APARTMENTTHREADED);
  28. /////////////////////////////////////////////////////////////////////////
  29. // Create the CSRegFreeCOMServer.SimpleObject COM object using
  30. // the #import directive and smart pointers.
  31. //
  32. // Create an instance of the component.
  33. // NOTE: Make sure that registration-free components are created with
  34. // their CLSID and not the ProgId.
  35. ISimpleObjectPtr spSimpleObj;
  36. hr = spSimpleObj.CreateInstance(__uuidof(SimpleObject));
  37. if (FAILED(hr))
  38. {
  39. _tprintf(_T(
  40. "ISimpleObjectPtr::CreateInstance failed w/err 0x%08lx\n"
  41. ), hr);
  42. return hr;
  43. }
  44. /////////////////////////////////////////////////////////////////////////
  45. // Consume the properties and the methods of the COM object.
  46. //
  47. try
  48. {
  49. // Set the property: FloatProperty.
  50. {
  51. _tprintf(_T("Set FloatProperty = %.2f\n"), 1.2f);
  52. spSimpleObj->FloatProperty = 1.2f;
  53. }
  54. // Get the property: FloatProperty.
  55. {
  56. _tprintf(_T("Get FloatProperty = %.2f\n"),
  57. spSimpleObj->FloatProperty);
  58. }
  59. // Call the method: HelloWorld, that returns a BSTR.
  60. {
  61. // the _bstr_t object and the underlying BSTR will be cleared
  62. // automatically in the destructor when the object is out of
  63. // the scope.
  64. _bstr_t bstrResult = spSimpleObj->HelloWorld();
  65. _tprintf(_T("Call HelloWorld => %s\n"), (LPCTSTR)bstrResult);
  66. }
  67. // Call the method: GetProcessThreadID, that outputs two DWORDs.
  68. {
  69. _tprintf(_T("The client process and thread: %ld, %ld\n"),
  70. GetCurrentProcessId(), GetCurrentThreadId());
  71. DWORD dwProcessId, dwThreadId;
  72. spSimpleObj->GetProcessThreadID(&dwProcessId, &dwThreadId);
  73. _tprintf(_T("Call GetProcessThreadID => %ld, %ld\n"),
  74. dwProcessId, dwThreadId);
  75. }
  76. _putts(_T(""));
  77. }
  78. catch (_com_error &err)
  79. {
  80. _tprintf(_T("The server throws the error: %s\n"), err.ErrorMessage());
  81. _tprintf(_T("Description: %s\n"), (LPCTSTR) err.Description());
  82. }
  83. /////////////////////////////////////////////////////////////////////////
  84. // Release the COM object.
  85. //
  86. // Releasing the references is not necessary for the smart pointers
  87. // spSimpleObj.Release();
  88. // Uninitialize COM for this thread
  89. CoUninitialize();
  90. return 0;
  91. }