CComPtr

CComPtr
 
#include "stdafx.h"
#include <atlcomcli.h>
 
#import "..\..\SimonATLSample\SimonATLSample\Debug\SimonATLSample.tlb" named_guids
 
#include "Debug\SimonATLSample.tlh" //This is the generated tlh file by the imported tlb above
 
 
using namespace SimonATLSampleLib;
 
int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr;
 
    hr = CoInitialize(NULL);
 
    //{ Notice!!! This bracket makes sure that the smart pointer destructor happens before CoUnitialize(), this is very important.
    //CComPtr<ISimonMathUtil> pSimonMathUtil = NULL;
    //hr = pSimonMathUtil.CoCreateInstance(__uuidof(SimonMathUtil), NULL);
 
    //ISimonMathUtil *pISimonMathUtilAnother;
    //hr = pSimonMathUtil.QueryInterface(&pISimonMathUtilAnother);
 
    //long retVal;
    //pSimonMathUtil->Add(1, 2, &retVal);
    // }
 
    // Above one is equal to the one below
 
    ISimonMathUtil *pISimonMathUtil;
    if(SUCCEEDED(hr))
    {
        hr = CoCreateInstance(__uuidof(SimonMathUtil), NULL, CLSCTX_INPROC_SERVER, /*IID_IKegleTest*/__uuidof(ISimonMathUtil), (void**)&pISimonMathUtil);
        if(SUCCEEDED(hr))
        {
            ISimonMathUtil *pISimonMathUtilAnother;
            hr = pISimonMathUtil->QueryInterface(__uuidof(ISimonMathUtil), (void**)&pISimonMathUtilAnother);
 
            long retVal;
            hr = pISimonMathUtilAnother->Add(1, 2, &retVal);           
        }
    }
 
    CoUninitialize();
 
    return 0;
}

你可能感兴趣的:(职场,com,休闲,CComPtr)