2

我有以下代码:

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

但这会引发:

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

提前致谢。

解决方案:

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

1 回答 1

4

您建议的解决方案会泄漏内存。做了

ICalculatorPtr pICalc;
pICalc.CreateInstance(__uuidof(ManagedClass));
于 2013-11-08T05:47:17.743 回答