Click to See Complete Forum and Search --> : Another Conversion Question


JoePub
November 17th, 2005, 05:34 AM
Hi All,

I am writing a Managed C++ app that uses RAW Windows API's. Now this particular API (PostQueuedCompletionStatus) uses a completion key value which is of type ULONG_PTR, now it is not used directly by the API, it is only used as a custom value which is then returned from the API call GetQueuedCompletionStatus. Now I want to use the ULONG_PTR as a pointer to a __gc managed class, what is the best approach for this? Can I convert the __gc class to an IntPtr before the PostQueuedCompletionStatus and then back again when using GetQueuedCompletionStatut?

Thanks

Alex F
November 17th, 2005, 09:53 AM
To use pointer to managed class in unmanaged code, you need to pin managed class in the memory. See GCHandle.Alloc Method with Pinned parameter. Having pinned object, you can get it's pointer using GCHandle.AddrOfPinnedObject Method.
In VC++ 8.0 you can try pin_ptr pointer.

JoePub
November 17th, 2005, 12:13 PM
Will that work across different threads then though, I thought pinning only works within the current stack?

Alex F
November 19th, 2005, 01:37 AM
This is not related to stack or threading. __gc managed class instance is allocated on the managed heap. To work with it in unmanaged code it is necessary to pin in in the memory, which prevents GC to move it.