dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to return new object in COM?

  1. #1
    Join Date
    Oct 2004
    Posts
    481

    How to return new object in COM?

    Hi GURUs,

    I have created a new COM DLL and just wondering how to return a new COM object.

    Here is my code to return a new object.
    Code:
    HRESULT __stdcall CMainTestClass::GetSamTest(IAdd **ppRetVal)
    {
        CAddClass *pAddClass = new CAddClass();
        *ppRetVal = pAddClass;
        return S_OK;
    }
    When I call this function in C#, it gave me this error:
    The runtime has encountered a fatal error. The address of the error was at 0x7f628678, on thread 0x105c. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
    My C# code:
    Code:
        MyTestLib.MainTestClass oMainObject = new MyTestLib.MainTestClass();
        MyTestLib.AddClass addObject = (oMainObject .GetSamTest() as MyTestLib.AddClass );
        MessageBox.Show(addObject.DoTheAddition().ToString());    //it fails here
    Thanks for any help

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

  3. #3
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: How to return new object in COM?

    If you use ATL you can do this through CComObject's CreateInstance :

    Code:
    STDMETHODIMP CMainTestClass::GetSamTest(IAdd **ppRetVal)
    {
    	CComObject<CAddClass>* addObj = NULL;
    	HRESULT hRes = CComObject<CAddClass>::CreateInstance(&addObj);
    	_ASSERTE(SUCCEEDED(hRes));
    
    	// Increment reference count immediately
    	addObj ->AddRef();
    
    	addObj ->QueryInterface( IID_IAdd, (void**) ppRetVal);
    
    	return S_OK;
    }
    Har Har

  4. #4
    Join Date
    Oct 2004
    Posts
    481

    Re: How to return new object in COM?

    Hi PadexArt & Cilu,

    Thanks for your help. You guys have been really helpful

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)