Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have the following questions to ask:
How to cast/marshal a LPDISPATCH to System::Object^ ?

LPDISPATCH pSetup)
System::Object^ cSetup = nullptr;
cSetup = (casting / marshalling) pSetup;

Do I have to use the namespace System::Runtime::InteropServices ?
Does somebody have already done such a marshalling or casting ?

Thank you very much in advance.
Best regards.
MiQi.

What I have tried:

I have tried static_cast but get the following compilation error:
C++
BOOL CMyClass::CreateThroughputFile(LPDISPATCH pSetup)
{
	System::Object^ cSetup = nullptr;

	if (static_cast<iremoteoperations^>(_Remoting.GetInterface()) == nullptr)
		return FALSE;

	cSetup = static_cast<system::object^>(pSetup);

returns:
Severity Code Description Project File Line Suppression State
Error C2682 cannot use 'safe_cast' to convert from 'LPDISPATCH' to 'System::Object ^'

return _Remoting.GetInterface()->CreateThroughputFile(cSetup);

}
Posted
Updated 9-Dec-16 2:06am
v2

In C#, I use the System.:Runtime::InteropServices::Marshal class to do this, and it works well. I would imagine in C++, it would be something like

C++
cSetup = Marshal::GetObjectFromIUnknown(IntPtr(pSetup));


This will properly marshal the pointer into the .NET interop, and set the refcount accordingly.
 
Share this answer
 
v2
At first you better use dynamic_cast for type checking.

But I think casting a COM object interface to a GC object is not correct. You should handle the original (COM) object as LPDISPATCH and dont cast around. ;-)
 
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