VarPtr in .NET

July 9, 2006

When you do a call to external function inside a native dll through .NET code; one problem which is faced by programmers is marshalling the data type and use of VarPtr. Heres the code which provides the functionality as VarPtr does.

//C# code

static int VarPtr(object o)
System.Runtime.InteropServices.GCHandle GC =
int ret = GC.AddrOfPinnedObject().ToInt32();

return ret;

” VB code

Public Shared Function VarPtr(ByVal o as Object) As Integer
Dim GC as System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Return GC.AddrOfPinnedObject.ToInt32
End Function



Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: