PageRenderTime 54ms CodeModel.GetById 35ms app.highlight 14ms 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
 18#pragma region Includes and Imports
 19#include <stdio.h>
 20#include <tchar.h>
 21
 22#import "CSRegFreeCOMServer.tlb" no_namespace named_guids
 23#pragma endregion
 24
 25
 26int _tmain(int argc, _TCHAR* argv[])
 27{
 28	HRESULT hr;
 29
 30	// Initializes the COM library on the current thread and identifies the
 31	// concurrency model as single-thread apartment (STA). 
 32	CoInitializeEx(0, COINIT_APARTMENTTHREADED);
 33
 34
 35	/////////////////////////////////////////////////////////////////////////
 36	// Create the CSRegFreeCOMServer.SimpleObject COM object using 
 37	// the #import directive and smart pointers.
 38	// 
 39
 40	// Create an instance of the component.
 41	// NOTE: Make sure that registration-free components are created with 
 42	// their CLSID and not the ProgId.
 43	ISimpleObjectPtr spSimpleObj;
 44	hr = spSimpleObj.CreateInstance(__uuidof(SimpleObject));
 45	if (FAILED(hr))
 46	{
 47		_tprintf(_T(
 48			"ISimpleObjectPtr::CreateInstance failed w/err 0x%08lx\n"
 49			), hr);
 50		return hr;
 51	}
 52
 53
 54	/////////////////////////////////////////////////////////////////////////
 55	// Consume the properties and the methods of the COM object.
 56	// 
 57
 58	try
 59	{
 60		// Set the property: FloatProperty.
 61		{
 62			_tprintf(_T("Set FloatProperty = %.2f\n"), 1.2f);
 63			spSimpleObj->FloatProperty = 1.2f;
 64		}
 65
 66		// Get the property: FloatProperty.
 67		{
 68			_tprintf(_T("Get FloatProperty = %.2f\n"),
 69				spSimpleObj->FloatProperty);
 70		}
 71
 72		// Call the method: HelloWorld, that returns a BSTR.
 73		{
 74			// the _bstr_t object and the underlying BSTR will be cleared 
 75			// automatically in the destructor when the object is out of 
 76			// the scope.
 77			_bstr_t bstrResult = spSimpleObj->HelloWorld();
 78			_tprintf(_T("Call HelloWorld => %s\n"), (LPCTSTR)bstrResult);
 79		}
 80
 81		// Call the method: GetProcessThreadID, that outputs two DWORDs.
 82		{
 83			_tprintf(_T("The client process and thread: %ld, %ld\n"),
 84				GetCurrentProcessId(), GetCurrentThreadId());
 85			
 86			DWORD dwProcessId, dwThreadId;
 87			spSimpleObj->GetProcessThreadID(&dwProcessId, &dwThreadId);
 88			_tprintf(_T("Call GetProcessThreadID => %ld, %ld\n"), 
 89				dwProcessId, dwThreadId);
 90		}
 91
 92		_putts(_T(""));
 93	}
 94	catch (_com_error &err)
 95	{
 96		_tprintf(_T("The server throws the error: %s\n"), err.ErrorMessage());
 97		_tprintf(_T("Description: %s\n"), (LPCTSTR) err.Description());
 98	}
 99
100
101	/////////////////////////////////////////////////////////////////////////
102	// Release the COM object.
103	// 
104
105	// Releasing the references is not necessary for the smart pointers
106	// spSimpleObj.Release();
107
108	// Uninitialize COM for this thread
109	CoUninitialize();
110	
111	return 0;
112}