VC++中的_com_ptr_t赋值 [英] _com_ptr_t assignment in VC++

查看:19
本文介绍了VC++中的_com_ptr_t赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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 函数中分配一些值.我怎样才能做到这一点?我想,像这样:

I want to first declare pICalc as a global variable and later assign some value inside the _tmain function. How can I do that? I suppose, like this:

ICalculatorPtr pICalc;
//...
int _tmain(int argc, _TCHAR* argv[])
{
    //...
    pICalc = __uuidof(ManagedClass);
}

但这会抛出:

错误 C2679:二进制=":未找到采用const _GUID"类型的右侧操作数的运算符(或没有可接受的转换)

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));
}

推荐答案

您建议的解决方案会泄漏内存.做起来

Your suggested solution leaks memory. Make it

ICalculatorPtr pICalc;
pICalc.CreateInstance(__uuidof(ManagedClass));

这篇关于VC++中的_com_ptr_t赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆