Welcome to AddressOf.com Sign in | Join | Help

VB10 vs VB6: VarPtr, StrPtr, ObjPtr

This is part of series of posts categorized under VB10 vs VB6.

As it turns out, I still have a copy of Microsoft Visual Basic 6.0 Programmer's Guide.  This was an excellent resource for learning VB6 and it's what I'll be using as a reference in these posts for comparison.

With that said, I have to point immediately that VarPtr, StrPtr and ObjPtr are *NOT* mentioned in this Programmer's Guide.  These were functions that were "somewhat undocumented" and were only used by developers whom were pushing the limits of VB6 well beyond its original design and/or where they needed to work around the inherent limitations of the IDE, language and or runtime.

Don't get me wrong, they were used in nearly every serious application ever developed in VB6; mainly by people who copied and pasted code they found elsewhere to work around the limitations in VB6 as time and technology marched beyond 1998.  Many of these developers had no fundamental idea of how these procedures they where utilizing actually worked under the hood, they were just trying to get their job done.

When I was upgrading projects from VB6 to VB7, I encountered a ton of these with the highest encountered one being related to mouse/keyboard "hooking".  In every case, I found built in functionality either in VB, Windows Forms or .NET that completely removed the need to utilize these "hacks".

With all that said, I find it interesting that Karl maintains the "Unofficial Documentation for VarPtr, StrPtr, and ObjPtr".  On this document, he states:

"The VarPtr function has been in the BASIC language since long before it turned into QuickBasic and Visual Basic. VarPtr has been in the VB runtime since version 1.0."

According to a post found here, VARPTR is one of the keywords that is specifically mentioned as being "keywords that are either not supported by VB or are being used in different context, or, have become properties/methods of some VB objects (and therefore could not stand as an independent statement)". It's been so long since I've used QB that I can't remember any of specifics one way or the other.

I find it interesting that he points out that VarPtr was in QB; however, fails to mention that it worked a little bit differently... a complaint that he is making with the move to newer versions of VB.  I also suspect that there would have been some changes between its usage in in VB1 though VB4 when you transitioned from 16-bit to 32-bit.  What makes all the more interesting is actually reading his unofficial documentation where he points out that there are differences between VB4, VB5 and VB6 with this functionality and the work arounds you have to do to get it to be "right".  And he complains about the "duct tape" solution provided by VarPtr, et. al. being removed? 

So what can one do when moving to VB10?

I have to point out that many of the hacks that were required in VB6 that leveraged these keywords are no longer needed with specific regards to Windows development.  Additionally, for the times that this is not the case, the Win32 interop story in VB10 is much, much, much better.  Additionally, it's all 100% documented and 100% supported. ;-)

Note: If someone would like to send me an small example of where these keywords were needed, I'd be happy to provide details about why they are no longer needed or how to leverage the Win32 API using VB10 in the cases where built in functionality does not already exist.   It'd be nice to have a specific example on this for future reference.

Verdict:

Given that these functions were for the elite of the elite VB developers; I'm going to go out on a limb here and state that if you could hack your way through things in this manner, leveraging the documented methods in .NET should make things a whole lot simpler.  For those that were just needing the results of these creative coders, a lot of this functionality is "included in the box".  Given those two factors, VB10 wins.

Updates(s):

Bill McCarthy (MVP) writes in: "StrPtr was often needed when calling Unicode API (assuming you pre-allocated the string) ObjPtr was sometimes used with windows subclassing calls: Was fraught with danger. I think Karl Peterson finally wrote an article for VSM a year or two ago admitting that using ObjPtr with subclassing could result in the wrong instance being called, and recommended to folks the use an external library for subclassing (one that ships with XP or later I think). Was kind-of funny to see it only took 10 years to admit it (lol)."

My only reply: :-)

Published Monday, May 30, 2011 12:12 AM by CorySmith
Filed under:

Comments

# VB10 vs VB6 (aka VFred vs Classic VB)

Sunday, May 29, 2011 7:05 PM by corysmith

A couple of weeks ago there was a rumor that was flying around regarding VB6 being made open source.

Anonymous comments are disabled