Important alert: (current site time 7/15/2013 7:30:20 AM EDT)
 

article

undocumented varptr in vb7

Email
Submitted on: 8/9/2002 6:02:19 PM
By: Izek_S 
Level: Intermediate
User Rating: By 9 Users
Compatibility: VB.NET
Views: 36848
(About the author)
 
     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...   
  1. 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.
  2. 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.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. 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
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

9/4/2002 7:03:01 PM

thnx i needed this
(If this comment was disrespectful, please report it.)

 
9/6/2002 10:52:12 PMkhairil anwar

how about this?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.)

 
9/13/2002 10:22:46 AMDavid Bue Pedersen

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.)

 
10/9/2002 8:05:39 PMtdtomlins

Great code but how do you reverse it by getting the Item from the location of the pointer??
(If this comment was disrespectful, please report it.)

 
11/19/2002 11:36:27 PMAndrew Christiansen

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.)

 
12/17/2002 5:02:40 PMSyko9000

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.)

 
12/17/2002 5:37:26 PMSyko9000

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)

CopyMemory PtrToMyself, ByVal ObjPtr(Me), 4 'get pointer to object (Me)
CopyMemory ByVal PtrToMyself + &H1C + VTIndex * 4, FarPointer, 4 'put VT entry

End Property
(If this comment was disrespectful, please report it.)

 
7/24/2003 10:14:05 PMUnruled Boy

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.)

 
5/13/2004 2:13:06 AMJustin Yates

Great code

(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.)

 
5/14/2005 7:30:05 AMsk_plus

when i use the function i get this
Object contains non-primitive or non-blittable data
(If this comment was disrespectful, please report it.)

 
3/22/2007 11:34:33 AMTimo Böhme

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.)

 
8/29/2007 7:10:11 AMrcw722

Anyone got any idea how to do this for Windows Forms or TextBox objects?

I am getting the error as well. :o(

(If this comment was disrespectful, please report it.)

 
10/2/2007 3:21:54 PMDavid

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.)

 
10/2/2007 3:25:46 PMDavid

To row722:

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.)
 

To post feedback, first please login.