gpt4 book ai didi

c++ - VC++ 中的 _com_ptr_t 赋值

转载 作者:搜寻专家 更新时间:2023-10-31 01:07:04 25 4
gpt4 key购买 nike

我有以下代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com