Difference between revisions of "Pointers and CopyMemory"

From HashVB
Jump to: navigation, search
m (Reverted edit of 1146364882, changed back to last version by Wakjah)
 
Line 67: Line 67:
  
 
That's just about it.  Pointers can do fun and funky things but they usually don't have that much application in VB, except in the special cases described above.  Obviously there are PLENTY more applications, and if anyone feels the requirement to include them, then please feel free to do so.  I haven't done so because I don't see it as necessary and can't be bothered.
 
That's just about it.  Pointers can do fun and funky things but they usually don't have that much application in VB, except in the special cases described above.  Obviously there are PLENTY more applications, and if anyone feels the requirement to include them, then please feel free to do so.  I haven't done so because I don't see it as necessary and can't be bothered.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<div  style="display:none">
 
[We are delicate. We do not delete your content.]
 
[l_sp2006]
 
http://top20man.in.ua/black-eyed-peas-mp3 black eyed peas mp3]
 
[http://top20man.in.ua/madonna-mp3 madonna mp3]
 
[http://top20man.in.ua/eminem-mp3 eminem mp3]
 
[http://ringtonemaker.blogs.eurosport.com/ ringtone maker]
 
[http://top20man.in.ua/godsmack-awake godsmack awake]
 
[http://top20man.in.ua/godsmack-voodoo godsmack voodoo]
 
[http://top20man.in.ua/sean-paul-temperature sean paul temperature]
 
[http://top20man.in.ua/sean-paul-we-be-burnin sean paul we be burnin]
 
[http://top20man.in.ua/bad-day-daniel-powter bad day daniel powter]
 
[http://top20man.in.ua/system-of-a-down-mp3 system of a down mp3]
 
[http://top20man.in.ua/sean-paul-mp3 sean paul mp3]
 
[http://top20man.in.ua/metallica-mp3 metallica mp3]
 
[http://top20man.in.ua/shakira-mp3 shakira mp3]
 
[http://top20man.in.ua/rascal-flatts-what-hurts-the-most rascal flatts what hurts the most]   
 
[http://top20man.in.ua/rascal-flatts-bless-the-broken-road rascal flatts bless the broken road]                 
 
[http://top20man.in.ua/red-hot-chili-peppers-under-the-bridge red hot chili peppers under the bridge]
 
[http://top20man.in.ua/james-blunt-wisemen james blunt wisemen]
 
[http://top20man.in.ua/bad-day-daniel-powter bad day daniel powter]
 
[http://top20man.in.ua/godsmack-mp3 godsmack mp3]
 
[http://blog.yukonho.com/index.php?blog=44 Godsmack Awake]
 
[http://blog.yukonho.com/index.php?blog=45 godsmack voodoo]
 
[http://blog.yukonho.com/index.php?blog=46 sean paul temperature]
 
[http://blog.yukonho.com/index.php?blog=47 Sean Paul We Be Burnin]
 
[http://blog.yukonho.com/index.php?blog=48 natasha bedingfield unwritten]
 
[http://blog.yukonho.com/index.php?blog=49 50 cent mp3]
 
[http://blog.yukonho.com/index.php?blog=50 Bad Day Daniel Powter]
 
[http://blog.yukonho.com/index.php?blog=51 Daniel Powter mp3]
 
[http://blog.yukonho.com/index.php?blog=52 Goodbye My Lover James Blunt]
 
[http://blog.yukonho.com/index.php?blog=53 System Of A Down mp3]
 
[http://blog.yukonho.com/index.php?blog=54 Sean Paul mp3]
 
[http://blog.yukonho.com/index.php?blog=55 Metallica mp3]
 
[http://blog.yukonho.com/index.php?blog=56 Shakira mp3]
 
[http://blog.yukonho.com/index.php?blog=57 Black Eyed Peas mp3]
 
[http://blog.yukonho.com/index.php?blog=58 Madonna mp3]
 
[http://blog.yukonho.com/index.php?blog=59 eminem mp3]
 
[http://blog.yukonho.com/index.php?blog=60 Fall Out Boy Grand Theft Autumn]
 
[http://blog.yukonho.com/index.php?blog=61 Jack Johnson mp3]
 
[http://blog.yukonho.com/index.php?blog=62 oscar dresses]
 
[http://blog.yukonho.com/index.php?blog=63 mother of the bride dresses]
 
[http://blog.yukonho.com/index.php?blog=64 cocktail dresses]
 
[http://blog.yukonho.com/index.php?blog=65 Flower Girl Dresses]
 
[http://blog.yukonho.com/index.php?blog=66 Formal prom Dresses]
 
[http://blog.yukonho.com/index.php?blog=67 Plus Size Prom Dresses]
 
[http://blog.yukonho.com/index.php?blog=68 Free Verizon Ringtone]
 
[http://top20man.in.ua/godsmack-i-stand-alone godsmack i stand alone]
 
[http://top20man.in.ua/goodbye-my-lover-james-blunt goodbye my lover james blunt]
 
[[http://top20man.in.ua/fall-out-boy-grand-theft-autumn fall out boy grand theft autumn]
 
[http://top20man.in.ua/jack-johnson-mp3 jack johnson mp3]
 
[http://top20man.in.ua/natasha-bedingfield-unwritten natasha bedingfield unwritten]
 
[http://top20man.in.ua/50-cent-mp3 50 cent mp3]
 
[http://blogs.wwwcoder.com/cleo/ nextel ringtone]
 
[http://top20man.in.ua/bad-day-daniel-powter bad day daniel powter]
 
[http://top20man.in.ua/daniel-powter-mp3 daniel powter mp3]
 
[http://verizonringtone.forumco.com/ verizon ringtone]
 
[http://uscellularringtone.forumco.com US Cellular Ringtone]
 
[http://novogate.com/board/5907/222695-1.html free sprint ringtone]
 
[http://4898.rapidforum.com verizon ringtone]
 
[http://blogs.heraldextra.com/verizonringtone/ verizon ringtone]
 
[http://blog.investing.com/bcbgshoes/ bcbg shoes]
 
[http://blog.yukonho.com/index.php?blog=40 free sprint ringtones]
 
[http://blog.yukonho.com/index.php?blog=41 cheap prom dresses]
 
[http://blog.yukonho.com/index.php?blog=42 sexy prom dresses]
 
[http://www.buddyprofile.com/viewprofile.php?username=waterfordcrystal waterford crystal]
 
[http://www.buddyprofile.com/viewprofile.php?username=swarovskicrystal swarovski crystal bead]
 
[http://www.buddyprofile.com/viewprofile.php?username=mesotheliomalawsuits mesothelioma lawsuits]
 
[http://www.buddyprofile.com/viewprofile.php?username=mesotheliomasymptoms mesothelioma symptoms]
 
[http://www.buddyprofile.com/viewprofile.php?username=mesotheliomadiag mesothelioma diagnosis]
 
[http://blog.yukonho.com/index.php?blog=69 Cingular Ringtone]
 
[http://blog.yukonho.com/index.php?blog=70 free kyocera ringtones]
 
[http://blog.yukonho.com/index.php?blog=71 Free Nextel Ringtone]
 
[http://blog.yukonho.com/index.php?blog=73 informal wedding dresses]
 
[http://blog.yukonho.com/index.php?blog=74 bridal gowns]
 
[http://blog.yukonho.com/index.php?blog=75 Discount Bridal Gowns]
 
[http://blog.yukonho.com/index.php?blog=76 Plus Size Swimwear]
 
[http://blog.yukonho.com/index.php?blog=77 no fax payday loans]
 
[http://blog.yukonho.com/index.php?blog=78 countrywide home loans]
 
[http://blog.yukonho.com/index.php?blog=79 fast cash loan guaranteed]
 
[http://www.totalvideogames.com/blog/naturalizershoes/ Naturalizer Shoes]
 
[http://www.totalvideogames.com/blog/freekyocerarington/ Free Kyocera Ringtone]
 
[http://www.surfbirds.com/blog/formalpromdresses/ formal prom dresses]
 
[http://www.missoula.com/blog/sexypromdresses/ Sexy Prom Dresses]
 
[http://www.justachat.com/blog/?w=naturalizershoes Naturalizer Shoes][http://www.westwoodbapt.org/blog/towelwarmer/ towel warmer]
 
[http://www.toutelapoesie.com/blog/aerobed/ Aero Bed]
 
 
 
</div>
 

Latest revision as of 20:09, 1 May 2006

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

If you're totally new to the concept of pointers, don't bother with this article. Having said that...

Pointers in VB?

Yes, that's right, it IS possible to use pointers in VB, although the fucnctions to do so are not documented, and at times it can be rather, erm, dodgy, so to speak.

3 Types of Pointers

VB defines 3 different types of pointers for different variable types, and those are Strings, Objects, and any other kind of variable (other than strings or objects). Their corresponding functions are as follows:


  • StrPtr
  • ObjPtr
  • VarPtr


As aforementioned, these functions are NOT documented. Perhaps MS just 'forgot' to include them... *cough* Anyway, I will begin with VarPtr, since it is relatively simple to understand.

VarPtr

This function will return the pointer to any kind of variable (hence its one parameter is defined as:

 Ptr As Any

The main use for this is in callbacks/use with APIs in general. I shan't go into detail with this, other than to say that it is the equivalent of *myVar in C, and can be used with CopyMemory, but MUST be passed ByVal, otherwise your app jsut crashes.

StrPtr

This function perhaps requires a little more explanation. Since Version 4, VB has not used the standard C-type null-terminated char*/wchar* (or LPSTR/LPWSTR), but instead has used bstr, an entirely (well almost) different construct. In terms of what's going on in the memory, this means that 4 bytes before the actual start of the string data is stored an integer value that contains the length of the string. The string is also null-terminated to provide compatibility with C, the usefulness of which we will see in just a second. This necessitates an extra function to return the real position of the start of the string in memory - i.e. StrPtr().

As a demonstration of this, you might try the following code:

   Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _
       (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
   
   Private Sub Form_Load()
       Dim s As String
       Dim sLen As Long
       
       s = "test"
       CopyMemory sLen, ByVal StrPtr(s) - 4, 4
       Debug.Print sLen
   End Sub

If you look at the Immediate window, you will see that '8' is the output of this. Obviously this is twice the actual length of the string, which would make no sense if VB weren't based around Unicode. Thankfully it is, so this is not utter nonsense-gibberish. Anyway...

Where this comes in handy is when you're writing your own API DLLs in a C language. There is usually some difficulty passing variables of unicode string types between C and VB. This solves the problem. If you have a function like this in C++:

 void __stdcall foo(wchar *lpstring) { OutputDebugStringW((LPSTR)lpstring); }

Then you can quite easily pass this string to the DLL using StrPtr. The function would be declared in VB as follows:

 Declare Sub foo Lib "myLib.dll" (lpstring As Long)

And pass a string to it like this:

 Dim myString As String
 
 myString = "test"
 foo ByVal StrPtr(myString)

This should trace 'test' in your output in the C++ debugger of your choice. Note that I have used wchar and OutputDebugStringW in the C++ code. Usually when you call an API, VB will convert strings to ANSI for you, however, it will not do this when you use StrPtr, which allows you to pass a Unicode string in its full glory over to your C functions.

ObjPtr

ObjPtr does something else useful. It will give you the pointer to your object reference. Note: a pointer is NOT the same as a reference. The first 4 bytes after your object pointer will be the object reference. This knowledge allows you to do something incredibly useful: get callbacks from modules into class modules. For an example of how to do this, see Modularised Subclassing using interfaces and CopyMemory.


That's just about it. Pointers can do fun and funky things but they usually don't have that much application in VB, except in the special cases described above. Obviously there are PLENTY more applications, and if anyone feels the requirement to include them, then please feel free to do so. I haven't done so because I don't see it as necessary and can't be bothered.