1. 程式人生 > >C++呼叫COM

C++呼叫COM

import “.\ATLSimple1.dll” no_namespace

int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);

// CLSID clsid;
// HRESULT hr = CLSIDFromProgID(OLESTR(“ATLSimple1.Func”), &clsid);
// CComQIPtr pFunc;
// hr = pFunc.CoCreateInstance(clsid);
// long lResult(0L);
// pFunc->Add(100, 200, &lResult);
// BSTR strResult;
// pFunc->StringCat(OLESTR(“愛你哈哈”), OLESTR(“helloworld”), &strResult);
// SysFreeString(strResult);
// pFunc.Release();
HRESULT hr;
IFunc* pFunc = NULL;

// IUnknown* pUnknown = NULL;
// hr = CoCreateInstance(__uuidof(Func), NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
// hr = pUnknown->QueryInterface(__uuidof(IFunc), (void**)&pFunc);
// pUnknown->Release();

hr = CoCreateInstance(__uuidof(Func), nullptr, CLSCTX_INPROC_SERVER, __uuidof(IFunc), (void**)&pFunc);
long lResult(0);
pFunc->Add(100, 200, &lResult);
BSTR strResult;
pFunc->StringCat(OLESTR("愛你哈哈"), OLESTR("hellworld"), &strResult);
SysFreeString(strResult);

pFunc->Release();

CoUninitialize();
return 0;

}