Click here to Skip to main content
15,748,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a C# WPF User Control DLL.
I want to show this User Control over a C++ MFC UI.
For communication between them I am using C++ COM DLL (CLR enabled for this VS project).

The IDL file has an entry for the COM function like this:
[local] HRESULT getUserControlComponent([in] int userControlKey, [out, retval] void* userControlObj);

In C++ side I am calling this function like this:
int key = 1;
void* myUserControlInCPP;
myCPP_and_CSharpCOMObj->getUserControlComponent(key, myUserControlInCPP);

This function "getUserControlComponent" calls another C# method 'createUserControl_Instance' which eventually calls the C# user control's CTOR like this:

gcroot<MyCoolUserControlNamespace::DummyUserControl^> userControlDummy = gcnew MyCoolUserControlNamespace::DummyUserControl(); //This is the call to the CTOR.

Here the 'userControlDummy' has non-NULL value.

But when, this the control of the code returns in C++ side, the object 'myUserControlInCPP' has NULL value.

Can someone please help me, why I am getting NULL value for the User Control in C++ side?

What I have tried:

Somewhere on internet I found, it is needed to do like this when dealing with such situations:
C# side code:
HRESULT getUserControlComponent(key, myUserControlInCPP)
    System::Object^ tempUserControlObj = createUserControl_Instance(key); //This function calls the CTOR of User Control.

    myUserControlInCPP = (GCHandle::ToIntPtr(GCHandle::Alloc(tempUserControlObj))).ToPointer();


But this also results in non-NULL value at C# side, but NULL pointer at C++ side.
Updated 26-May-23 4:46am

1 solution

I think you need to put an out on the C# method itself so the compiler knows to pass the changed value back to the caller. Without the out, the changes to the parameter variable are only visible within the method.
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