Posted on 2005-04-14
I have the following :
int MyInt = 100;
GCHandle gch = GCHandle.Alloc( MyInt, GCHandleType.Pinned );
If i print GCHandle.Target i get 100. Now, if i do :
Myint = 200;
And i print GCHandle.Target i still get 100. Is this normal ?
I thought GCHandle gives me a stable pointer to the object (MyInt).
But it seems as if it copies MyInt somewhere and returns a pointer to the copy.
I am calling a function in an unmanaged DLL that requires a pointer to an int.
This pointer must remain stable for the lifetime of my app since it's value is updated
thru other calls to the DLL. This means that I have to do MyInt = GCHandle.Target to
get the new value every time I call a DLL function that modifies the pointer value.
Is this the only way of getting a stable pointer (no GC and no moving) ?