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();
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);
myUserControlInCPP = (GCHandle::ToIntPtr(GCHandle::Alloc(tempUserControlObj))).ToPointer();
}
But this also results in non-NULL value at C# side, but NULL pointer at C++ side.