VB6 has 3 undocumented functions. VarPtr, StrPtr, and ObjPtr.StrPtr returns object of a string, VarPtr returns address of any other variable and ObjPtr returns address of an object. In VB.Net these 3 functions are abosolete but their functionality is still avaliable.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Public 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)
Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return ret
End Function
Report Bad Submission
Your Vote
Other User Comments
9/4/2002 7:03:01 PM:
thnx i needed this (If this comment was disrespectful, please report it.)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Private Declare Function GetProp Lib "user32" Alias "GetPropA"(ByVal hwnd As Integer, ByVal lpString As String) As Integer
Private objProcClass As New cProc
If (GetProp(hwnd, hwnd & "#" & iMsg & "#" & iC) = ObjPtr(objProcClass)) Then ... ... ... End If ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
can you help me figure this out? (If this comment was disrespectful, please report it.)
9/7/2002 2:58:46 AM:
basicaly that checks "##" property of the virtual property bag associated with the window handle for a certain value and then checks if that value is the address of some user object (If this comment was disrespectful, please report it.)
Great code! 5 globes from here! But I need to use all of the good ol' undocumented functions; VarPtr, StrPtr, and ObjPtr. Have any of you redone these?!? Then please post them to help us all :-) Best regards! David! (If this comment was disrespectful, please report it.)
9/13/2002 4:00:13 PM:
varptr tat i posted handles everything, strings, objects, variables, so it repalces all 3 vb6 functions varptr, strptr and objptr (If this comment was disrespectful, please report it.)
The only time I've ever had to use VarPtr is when sending a message to a textbox to have the Windows XP pop up balloon point to it. When I moved to VB.net I couldn't figure out how to get VarPtr's functionality in VB.net. Obviously I found it :-) 5 globes from me. (If this comment was disrespectful, please report it.)
Best I can figure, a GCHandle is not the actual pointer to the object(sadly). It's a handle to a pointer to an object...or something, but...if you call GC.Free() the handle that this "VarPtr" function returns is invalid and no use to anyone. so comment out the "GC.Free()" and to get the object back do ~~~~~~ Dim GC As GCHandle = New GCHandle(New IntPtr(VarPtrReturnValue)) : Dim TheObject = GC.Target ~~~~~~ (If this comment was disrespectful, please report it.)
Opps...the code in that is wrong, and now I can't get VB.NET to open to work out the correction...LOL...So just ignore most of that... using the "op_Explicit" function and not "Pinning" an object is ... better... cause it works on all Objects...not just the "Pinnable" ones...(Forms and userdefined class are apparently not pinnable)... just don't "Free" it till you're done with it... (If this comment was disrespectful, please report it.)
1/16/2003 4:31:06 AM:
I have tried using this when using the TTM_SETTITLE Api call for balloon tooltips however only the first characater appears. Are you aware of any issues with strings. I have tries declaring the string in different ways (public, const). The project is a COM dll, could this affect it ? (If this comment was disrespectful, please report it.)
7/2/2003 4:29:59 PM:
Thats because the text is stored as unicode, which in high byte/low byte order, makes the second byte 'zero', (a.k.a. end-of-string). Use the System.Text classes to build an ASCII string instead. (If this comment was disrespectful, please report it.)
7/8/2003 11:56:06 AM:
When I try to build it (VB .NET 2003) I get the error message:An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: Object contains non-primitive or non-blittable data.
(If this comment was disrespectful, please report it.)
7/8/2003 12:06:24 PM:
Try for example: VarPtr(me) in a form.
I'm trying to do a memory redirect: Private Property Let VirtualTableEntry(ByVal FarPointer As Long)
how about the other to functions? (If this comment was disrespectful, please report it.)
1/9/2004 6:26:14 AM:
I've got the same error message (An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Object contains non-primitive or non-blittable data.) But I don't know exactly how to use the code that's posted in the message below. Maybe someone can help me? Thanks a lot! (If this comment was disrespectful, please report it.)
(If this comment was disrespectful, please report it.)
8/27/2004 2:13:27 AM:
I am trying ot get the address of an object of user defined class type. Is there any way to pin user-defined class? Please come back. (If this comment was disrespectful, please report it.)
Hey god good code. I searched a long time to get just this "grr-xyz" pointer from my Array of Structures. And now it works without the Marshal equiptment! Thanks !!!
Timo (If this comment was disrespectful, please report it.)
If you follow the logic of this function, once you have the pointer to where it WAS in memory, at which point you can then pass the pointer to a 3rd Party DLL. Normally this wouldn't cause a problem, but there is always the possibility that after you unpin the variable, it could move in memory before it gets passed to the DLL, or even move in memory while the DLL is trying to work with it. Anyway, because of this, there is always the minute chance for some random effects of using this function. (If this comment was disrespectful, please report it.)
Controls (to include forms and textboxes), have a "Handle" property. This returns a pointer to that Control which can then be passed on. So Function(Control.Handle) would pass the Control's point to the Function. (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)