User Tools

Site Tools


gchandle

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Last revisionBoth sides next revision
gchandle [2018/09/06 19:43] wolfgangriedmanngchandle [2018/09/07 04:23] wolfgangriedmann
Line 1: Line 1:
 ====== GCHandle: cast an object to a number ====== ====== GCHandle: cast an object to a number ======
  
-Please see: https://www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object+When working with callback functions and the Windows API, sometimes it is important to map an object to a numeric variable. 
 +In VO, often the object was casted to an int variable. This was possible since in VO and Win32 a pointer has the same width as an int, but it could lead to problems if the object was moved by the garbage collector. Therefore this is dangerous and could lead to problems if not used correctly. 
 +In X#, this is not more possible, as a pointer can also be 64 bit wide (when the program runs in x64 mode), cannot be casted to an int. Please see below for a thread on the X# forum. 
 + 
 +Fortunately in the .NET Framework there is another possibilitythe System.Runtime.InteropServices.GCHandle structure: [[https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.gchandle|https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.gchandle]] 
 + 
 +Basically, you can get a handle from an object and lock it in memory so the garbage collector will not move it around: <code visualfoxpro>local oHandle as GCHandle 
 +oHandle := GCHandle.Alloc( oMyObject )<code> 
 +and then get a pointer to it: <code visualfoxpro>local ptrObject as IntPtr 
 +ptrObject := GCHandle.ToIntPtr( oHandle )<code> 
 +To return then the object from the IntPtr (that is 32 bit wide in x86 mode, and 64 bit in x64 mode), you can use this code: <code visualfoxpro>local oHandle as GCHandle 
 +local oObject as MyObject 
 +oHandle := GCHandle.FromIntPtr( ptrObject ) 
 +oObject := (MyObject) GCHandle.Target<code> 
 +And since you have locked this object in memory, it is very important to release it also with 
 +<code visualfoxpro>oHandle:Free()</code> 
 +There is also a sample by Microsoft: [[https://msdn.microsoft.com/en-us/library/44ey4b32(v=vs.110).aspx|https://msdn.microsoft.com/en-us/library/44ey4b32(v=vs.110).aspx]] 
 + 
 +For explanations by the development team, please see: [[https://www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object|https://www.xsharp.info/forum/public-product/832-2-0-0-2-cannot-cast-type-int-to-object]]
gchandle.txt · Last modified: 2018/09/07 04:23 by wolfgangriedmann