cdonts
cdonts

Reputation: 9599

_com_ptr_t assignment in VC++

I have the following code:

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

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

But this throws:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const _GUID' (or there is no acceptable conversion)

Thanks in advance.

Solution:

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

Upvotes: 2

Views: 535

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52471

Your suggested solution leaks memory. Make it

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

Upvotes: 5

Related Questions