Previous Thread
Next Thread
Print Thread
Rate Thread
Joined: Mar 2002
Posts: 1,273
DollarDNS Owner
OP Offline
DollarDNS Owner
Joined: Mar 2002
Posts: 1,273
Some people out there say that you can't manipulate pointers in VB. You can't blame them really, the 3 built-in functions that makes pointers in VB possible are largely undocumented. But without them, VB's power would be greatly diminished.

Besides those 3 functions, there is one other API function which is a VB guru's best friend and makes those pointers more then just pretty to look at. Those three, and that one API will be discussed in this lesson. StrPtr() VarPtr() ObjPtr() and CopyMemory() API.

First, let's go over a few definition:

Pointer = The numerical address of a block of memory.
Allocate = To set aside a block of memory for use
Variable = The name, supplied for programmatic convenience, for a block of allocated memory. When compiled, these names are resolved to POINTERS.
ByVal Operator = To make sure the VALUE of a variable and not the POINTER to the variable is passed to a function.

CopyMemory(pDest As Any, pSrc As Any, ByVal ByteLen As Long) API Sub
All this very powerful function does is copy a block of memory from one location to another. It's pretty basic, you supply it with the destination pointer (pDest), the source pointer (pSrc), and the number of bytes to copy (ByteLen). In the declaraion you see how it says pDest and pSrc As Any? That means, you can supply any variable you want to the function. At compile time the variable will be replaced with the variable pointer. See that 'ByVal' beside ByteLen? That's to make sure the VALUE of Var3 is passed to the function rather than the Var3 POINTER. Without the ByVal, even though Var3 may contain the value of 3. The pointer may be something like 1606088. See the diagram below:

Code
Var1 = "Jim"
Var2 = ""
Var3 = Len(Var1) + 1  '+1 = null terminator
CopyMemory Var1, Var2, Var3

Var1's Pointer is: 1579328
Var2's Pointer is: 1644472
Var3's Pointer is: 1606088

After you compile, it will look like this:

With ByVal:
CopyMemory 1579328, 1644472, 4
(copy 4 bytes from 1579328 to 1644472)

Without ByVal
CopyMemory 1579328, 1644472, 1606088
(copy 1606088 bytes from 1579328 to 1644472)
As you can see, we want the VALUE, and NOT the POINTER of the number of bytes to copy. However, in the case of the pDest and pSrc, we want the POINTER, and NOT the VALUE. In case you're curious, watch what happens if we stuck a ByVal in front of pDest and pSrc in the declaration.

Code
CopyMemory 74, 0, 4
(copy 3 bytes from 74 to 0)
See what happened? Since I used ByVal, I returned the VALUE of Var1 and Var2 instead of their POINTERS. The first byte in Var1 is 'J' and the ASCII value of 'J' is 74. Var2 is a blank variable so it's first byte is NULL, or 0. You do realize, that if you tried to do that, VB would crash. wink So be careful, if you don't know your pointers very well, VB will be crashing like a mofo.

VarPtr() Function
This function returns the Variable Pointer to a given variable. Usually this function isn't very necessary cause it is the same value as your variable when compiled.

'Return the pointers to Var1 and Var2
Ex 1: CopyMemory Var1, Var2, Var3

'Return the value of the 2 variables (crash alert!)
Ex 2: CopyMemory ByVal Var1, ByVal Var2, Var3

'Return the VALUE of the variable pointer
Ex 3: CopyMemory ByVal VarPtr(Var1), ByVal VarPtr(Var2), Var3

Now see, the value of the variable pointer iiiiiiiiiiis... the pointer itself. Duh. Example 1 and Example 3 does the same thing. So VarPtr() isn't very useful in this case.

StrPtr() Function
This function returns the String Pointer within a string variable. You see, I lied earlier when I said the VALUE of Var1 starts with 'J' or 74. I was just making a point. Visual Basic stores some internally useful information at the variable pointer like what datatype it is and how many bytes is stored and such. The REAL string pointer can be returned with this StrPtr() function.

CopyMemory Var1, Var2, Var3

Remember that? If Var1 and Var2 are string variables then we're in trouble! You'll be writing over the Var1's variable information which VB requires to keep track of it's variables! Talk about a major boom! Instead, you'd want this:

CopyMemory ByVal StrPtr(Var1), ByVal StrPtr(Var2), Var3

This way we'll be overwriting Var1's STRING with Var2's STRING. We get the exact memory location of those strings with StrPtr(). Ya follow? Now you still gotta be careful. Don't try to write a big string to a small string. Like in the example above with writing Jim to a blank variable. Not a good idea. VB still thinks that Var2 is still blank cause the information located at the variable pointer hasn't changed. But you can do this with no crashes.

Var1 = "123"
Var2 = "456"
CopyMemory ByVal StrPtr(Var1), ByVal StrPtr(Var2), 3

Now, Var1 is equal to "456".

ObjPtr() Function
This function returns a pointer to an OBJECT. Objects are stored in memory like everything else. And like everything else they have to start somewhere, well, that somewhere is the pointer to the Object. The value that replaces the object name when you compile.

I've only used this pointer once. In my TCP WorkShop project, I had a listbox which displayed all current telnet connections. Also, each telnet session had a window into which you type and recieve text. If I right-click an item in the listbox and say "Close Connection", then I want to close the matching window. I could do things the normal way... I could loop through all the loaded windows and look for the matching one. But this is inefficient, and I could have like 50 telnet sessions going at once. So instead, I saved the window's Object Pointer in the matching listitem. So if I needed to close the window matching a listitem, I could retrieve the pointer again (a LONG value) and using CopyMemory to store it into a Object Variable. I created the object variable from a pointer! Ugh, believe me, that took like 20 crashes for me to get right. Here's the code:

Public Sub GetForm(Pointer As Long) As Form
Dim oForm As Form
'Copy the 4 byte pointer into the object variable temporarily
CopyMemory oForm, Pointer, 4
'Now store the hacked object into a legit VB form object
Set GetForm = oForm
'Now reset the variable so VB doesn't crash
CopyMemory oForm, 0, 4
End Sub

AddressOf Operator
To make sure the POINTER of FunctionA is passed to FunctionB rather than the return value of FunctionA. Functions are no different than variables. Function names resolve to a pointer like anything else. It's just that THIS pointer doesn't point to a chunk of stored data. It points to code which is executed and returns a value. The main thing AddressOf is good for is API. Sometimes you need to tell Windows to call one of your functions if something happens. In that case, you must pass the function pointer to the API function so that it knows where your function location in memory is.

Look at the difference below:

ReturnValue = FunctionA(FunctionB)
If you call FunctionA like that, then FunctionB will be executed first and it's return value will be passed to FunctionA.

ReturnValue = FunctionA(AddressOf FunctionB)
However, in this case FunctionA will just recieve the pointer to FunctionB. A LONG value.

----

If anybody should find that I'm wrong on any point (which is possible), then let me know and I'll thoroughly explore the issue and correct myself.


Domain Registration, Hosting, Management
http://www.dollardns.net
Joined: Mar 2002
Posts: 257
Member
Offline
Member
Joined: Mar 2002
Posts: 257
/me thinks back to the c++ days

nice tut yet again


The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean, "Someone who loves to program and enjoys being clever about it."
--------------------
"Its not a bug, its a feature" (Epic Games)

Link Copied to Clipboard
Member Spotlight
Phatal
Phatal
Houston, TX
Posts: 298
Joined: April 2004
Forum Statistics
Forums41
Topics33,840
Posts68,858
Average Daily Posts1
Members2,176
Most Online3,253
Jan 13th, 2020
Latest Postings
Where and how do you torrent?
by danni75 - 03/01/24 05:58 AM
Animation,
by JohanKaariainen - 08/15/19 01:18 AM
Blackbeard.....
by Gremelin - 10/03/18 07:02 PM
my old account still exists!
by Crime - 08/10/18 02:47 PM
Okay WTF?
by HenryMiring - 09/27/17 01:45 AM
The History Thread...
by Gremelin - 08/11/17 12:11 PM
My friend NEEDS your HELP!
by Lena01 - 07/21/17 12:06 AM
I'm having fun with this guy.
by gabithompson730 - 07/20/17 01:50 AM
I want to upgrade my phone
by gabithompson730 - 07/20/17 01:49 AM
Doom 3
by Cyrez - 09/11/14 08:58 PM
Amazon Gift Card Generator/KeyGen?te
by Gecko666 - 08/22/14 09:21 AM
AIM scene 99-03
by lavos - 09/02/13 08:06 AM
Planetside 2
by Crime - 03/04/13 07:10 AM
Beta Testers Wanted
by Crime - 03/04/13 06:55 AM
Hello Everyone
by Gremelin - 02/12/12 06:01 PM
Tracfone ESN Generator
by Zanvin Green - 01/18/12 01:31 PM
Python 3 issue
by Testing - 12/17/11 09:28 PM
tracfone airtime
by Drache86 - 07/30/11 03:37 AM
Backdoors and the Infinite
by ZeroCoolStar - 07/10/11 03:52 AM
HackThisZIne #12 Releaseed!
by Pipat2 - 04/28/11 09:20 PM
gang wars? l33t-wars?
by Gremelin - 04/28/11 05:56 AM
Consolidate Forums
by diggin2deep - 04/21/11 10:02 AM
LAN Hacking Noob
by Gremelin - 03/12/11 12:42 AM
Top Posters
UGN Security 41,392
Gremelin 7,203
§intå× 3,255
SilentRage 1,273
Ice 1,146
pergesu 1,136
Infinite 1,041
jonconley 955
Girlie 908
unreal 860
Top Likes Received
Ghost 2
Cyrez 1
Girlie 1
unreal 1
Crime 1
Powered by UBB.threads™ PHP Forum Software 7.7.5