从非托管C多次调用C#COM对象

问题描述:

所以我看了一下来电从非托管C ++代码管理.NET C#COM对象 [ ]并且能够创建我的表单并毫无问题地调用它。现在我想多次调用表单,允许用户每次都提供输入。 C或C ++它并不重要,它的混合对我的解决方案来说很好。



有什么我想念的吗?我不能简单地将下面的行放入for循环吗?:

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



第一次迭代后,表单永远不会出现。我可能误解了事物的顺序,但我觉得我不应该再次调用CoInitialize或CreateInstance()。下面是我正在使用的简单代码示例。




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





非常感谢能够提供帮助的任何人,或者如果我是正确的方向,请指出我误解COM调用是如何工作的。



编辑:我意识到我的COM调用的返回实际上导致了HRESULT。如果这在第一次返回S_OK时有帮助。进一步的迭代返回看似未初始化的值,例如-2146233079



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.

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

更好地向您引用的文章的作者提出问题。加载文章页面,在最后找到评论和讨论,然后单击添加评论或问题。作者将收到通知并有机会回答您。



对我而言,本文的全部主题及其目的尚不清楚。当您通过某些非托管代码使用COM对象时,无论如何实现此COM对象,在.NET,C#或任何其他方式中,它都应该正确实现。消耗COM对象的技术总是相同的,取决于您的用户代码及其语言。



-SA
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