#include "stdafx.h" #include#include #include "MyCOMInterface.h" CComPtr spMyCOMInterface; //declared as global variable void initialize_COM() { HRESULT hr = spMyCOMInterface.CoCreateInstance(CLSID_MyCOMInterface); if (FAILED(hr)) return; //use COM object functions spMyCOMInterface->Function1(); spMyCOMInterface->Function2(); spMyCOMInterface->Function3(); //when the function ends, spMyCOMInterface is released automatically }
#include "stdafx.h" #includeIn this example, CComPtr is used to create a COM object of IMyCOMInterface interface and initialize it with a local variable spMyCOMInterface. After initializing, the COM object's functions are used. When the function ends, spMyCOMInterface is automatically released. CMyComPtr is a part of ATL (Active Template Library) package library provided by Microsoft for C++ programming.#include #include "MyCOMInterface.h" void use_COM() { CComPtr spMyCOMInterface; //declared as local variable HRESULT hr = spMyCOMInterface.CoCreateInstance(CLSID_MyCOMInterface); if (FAILED(hr)) return; //use COM object functions spMyCOMInterface->Function1(); spMyCOMInterface->Function2(); spMyCOMInterface->Function3(); //when the function ends, spMyCOMInterface is released automatically }