Click to See Complete Forum and Search --> : Conversion question: Marshal::PtrToStructure


DennisWoo
November 16th, 2005, 05:23 PM
Hi guys,

I am in the process of converting a VS2003 listview control to the new C++/CLI synatx. Here is the problem I have:

NMHDR *h = dynamic_cast<NMHDR*>( Marshal::PtrToStructure(msg->LParam, __typeof(NMHDR)) );

When I compiled it, it gives me 2 errors:

error C4980: '__typeof' : use of this keyword requires /clr:oldSyntax command line option
error C2440: 'dynamic_cast' : cannot convert from 'System::Object ^' to 'NsCtlListView::CtlListView::NMHDR *'
error C2664: 'void System::Windows::Forms::Control::WndProc(System::Windows::Forms::Message %)' : cannot convert parameter 1 from 'System::Windows::Forms::Message ^' to 'System::Windows::Forms::Message %'

I tried couple of things but I can't solve this problem.

Any suggestions please?

Thanks.

Dennis

NoHero
November 17th, 2005, 02:20 AM
__typeof() only accepts managed classes and structures. NMHDR is a native structure. And PtrToStructure() marshales an unmanaged code block (eg. a pointer to an NMHDR structure) to a managed code block and not vice versa.

DennisWoo
November 17th, 2005, 07:40 AM
This is what bugs me. The same codes works under VS.Net 2003. I understand the first part and that can be easily fixed using typeid. However, it is the second error that i don't know how to address. I was ok in the older version. What will be the equilvalent now? Basically, I am taking something from the managed heap and put it into the unmanaged heap. How is the conversion supposed to do?

TIA!

Dennis

NoHero
November 17th, 2005, 10:27 AM
Sorry for the lack of examples. Alex F has summed it up pretty neatly right here (http://www.codeguru.com/forum/showpost.php?p=1273505&postcount=2) (you forgot to pin it, for example. The location of a managed block on the managed heap, may vary).