我有以下代码:
int _tmain(int argc, _TCHAR* argv[])
{
// Initialize COM.
HRESULT hr = CoInitialize(NULL);
// Create the interface pointer.
ICalculatorPtr pICalc(__uuidof(ManagedClass));
long lResult = 0;
// Call the Add method.
pICalc->Add(5, 10, &lResult);
wprintf(L"The result is %d\n", lResult);
// Uninitialize COM.
CoUninitialize();
return 0;
}
我想首先将 pICalc
声明为全局变量,然后在 _tmain
函数内分配一些值。我怎样才能做到这一点?我想,像这样:
ICalculatorPtr pICalc;
//...
int _tmain(int argc, _TCHAR* argv[])
{
//...
pICalc = __uuidof(ManagedClass);
}
但这会抛出:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const _GUID' (or there is no acceptable conversion)
提前致谢。
解决方案:
ICalculatorPtr pICalc = NULL;
//...
int _tmain(int argc, _TCHAR* argv[])
{
//...
pICalc = new ICalculatorPtr(__uuidof(ManagedClass));
}
最佳答案
您建议的解决方案会泄漏内存。做到这一点
ICalculatorPtr pICalc;
pICalc.CreateInstance(__uuidof(ManagedClass));
关于c++ - VC++中_com_ptr_t赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19849884/