Click here to Skip to main content
15,885,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I took a look at the article Calling Managed .NET C# COM Objects from Unmanaged C++ Code[] and was able to create my form and call it without issue. Now I would like to call the form multiple times allowing user to provide input each time. C or C++ it doesn't really matter and a mix of it is fine for my solution.

Is there something I'm missing? Shouldn't I be able to simply place the line below into a for loop?:
pDotNetCOMPtr->ShowCOMDialog ();

The form never appears after the first iteration. It's possible I'm misunderstanding the order of things, but I don't feel I should have to call CoInitialize or CreateInstance() again. Below is the simple code sample I'm using.

int _tmain(int argc, _TCHAR* argv[])
{
    //for(int i = 0; i < 4;i++)
    {
        CoInitializeEx(NULL, COINIT_MULTITHREADED);

        int theResult = -1;
        long result = 0;
        COMTesting::IProgramPtr pDotNetCOMPtr;
        HRESULT hRes = pDotNetCOMPtr.CreateInstance(COMTesting::CLSID_Program);

        for(int i = 0; i < 4;i++)
        {  

            if (hRes == S_OK)
            {
                BSTR MyBstr = SysAllocString(L"Proof I can Pass a String!");
                BSTR myReturnStr;
                result = pDotNetCOMPtr->LaunchDialog(MyBstr, &myReturnStr);
                printf("The Result was %i\r\n", result);
                char *p = _com_util::ConvertBSTRToString(myReturnStr);
                printf(p);
                printf("\r\n"); 
                printf("Press Any Key to Continue\r\n");
                getchar();
                delete [] p;
            }
        }

        CoUninitialize ();
    }
    return 0;
}


Thank you so much for anyone that could provide assistance, or point me in the right direction if I'm misunderstanding how the COM calls are working.

EDIT: I realize that the return of my COM call actually results in a HRESULT. If this helps at all the first time it returns S_OK. Further iterations return what appear to be uninitialized values such as -2146233079
Posted
Updated 5-Dec-13 2:55am
v2

1 solution

Better ask your questions to the author of the article you referenced. Load the article page, locate "Comments and Discussions" at the end and click "Add a Comment or Question". The author will get a notification and a chance to answer you.

To me, the whole subject of this article and its purpose is not clear. When you consume a COM object by some unmanaged code, it does not matter how this COM object is implemented, in .NET, C#, or any other way, it only should be implemented correctly. The technique of consumption of COM object is always the same and depends on your user code and its language.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900