Hi
I have got this problem while developing an AddIn for Outlook. I have a code that is developed in VB 6.0. Now, I want to use that code in my VSTO project. But, as we know .NET dont support pointer, this application got many errors.
I have searched, googled, explored for the code like anything for a week. At last, I got this somewhere in the net.
Now, I am keeping it here for those who are in need of this!
Thanks to Mr.Vaibhav Gaikwad who has provided us this code in his blog.
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 =
System.Runtime.InteropServices.GCHandle.Alloc(o,
System.Runtime.InteropServices.GCHandleType.Pinned);
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