|
-
Sep 4th, 2001, 03:40 AM
#1
Thread Starter
Fanatic Member
FillMemory/ZeroMemory
Hi there.
I don't seem to be able to understand how to pass a pointer to a string to the api functions ZeroMemory and FillMemory.
Can someone elucidate what I am doing wrongly.
Thanks in advance.
VB Code:
Option Explicit
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
Private Sub Command1_Click()
Dim s$
s = "8888888888888888888888888"
ZeroMemory ByVal StrPtr(s), Len(s)
Me.Print "s apres zeromemory: " & s
'this does work why, I want 25 zeros?
s = "8888888888888888888888888"
FillMemory ByVal StrPtr(s), LenB(s), Asc("a")
Me.Print "s apres fillmemory: " & s
End Sub
-
Sep 4th, 2001, 06:42 AM
#2
using a byte array instead of unicode strings, fill memory now prints all 'A' 's. Wide (unicode) char strings don't work with these calls. You get wierd results.
Code:
Option Base 0
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
Private Sub Command1_Click()
Dim s(25) As Byte
Init s, 25
FillMemory ByVal VarPtr(s(0)), 25, 65
Me.Print "s apres fillmemory: " & disp(s, 25)
End Sub
Sub Init(ByRef s() As Byte, i As Integer)
Dim j As Integer
For j = 0 To i
s(i) = Asc("8")
Next
End Sub
Function disp(ByRef s() As Byte, i As Integer) As String
Dim j As Integer
Dim tmp
For j = 0 To 25
tmp = tmp & Chr(s(j))
Next
disp = tmp
End Function
-
Sep 5th, 2001, 12:57 AM
#3
Thread Starter
Fanatic Member
Jim thanks for the reply, however I was more interested in working out what I was doing wrong with the pointers and strings.
Passing the string byvalue does work for FillMemory but not for ZeroMemory:
VB Code:
Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
Private Sub Command1_Click()
Dim s$
s = "8888888888888888888888888"
ZeroMemory ByVal s, LenB(s)
Me.Print "s apres zeromemory: " & s
'this still doesn't work why? I want 25 zeros?
s = "8888888888888888888888888"
FillMemory ByVal s, LenB(s), Asc("a")
Me.Print "s apres fillmemory: " & s
'Now this does work, why does this work and zero memory fail?
End Sub
By passing the entire string by value then I can get fill memory to work. Now why didn't this work when I passed a pointer to the exact memory address of the string? What is going on when I pass the string by value that makes it work?
Last edited by JamesM; Sep 5th, 2001 at 02:29 AM.
-
Sep 6th, 2001, 11:13 AM
#4
Frenzied Member
When you pass the string ByVal you are still passing the pointer to the function, but I believe this is the physical address. Passing ByRef will pass the VB address which can, and often is paged, and moved around (you wondered how they get VB so fast?)
Look in to LPSTR, BSTR, and C style strings on the MSDN or any good Dan Appleman book.
All VB Strings are BSTR (which can be Unicode, or otherwise) BSTR are the bread and butter of COM/Automation which VB subscribes (whether you like it or not) so the compiler tries to be as COM compliant as it can.
Have fun.
?
-
Sep 6th, 2001, 11:39 AM
#5
Fanatic Member
Memory...
The ZeroMemory function fills a block of memory with zeros. The function doesn't write values to the memory that will be interpreted by VB as character '0's, but just zeros the memory out.
e.g. Say for example, the area of memory holding the string "ssssssss" looks like this:
00101 00101 00101 00101
00101 00101 00101 00101
After zero memory, it probably looks like this:
00000 00000 00000 00000
00000 00000 00000 00000
VB is atempting to read the same memory area (where the data has been zeroed), and it interprets it as the characters you get displayed.
Basically, zero memory doesn't write the values that can be interpreted as character 0's to memory. For this, use the FillMemory function with "0".
Hope this makes sense and helps.
Laterz
Digital-X-Treme
Contact me on MSN Messenger: [email protected]
[VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
/ (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]
-
Sep 7th, 2001, 03:05 AM
#6
Frenzied Member
Hmmm
I have to ask why you'd want to do this anyway?
-
Sep 7th, 2001, 09:07 AM
#7
Thread Starter
Fanatic Member
Re: Hmmm
Originally posted by yrwyddfa
I have to ask why you'd want to do this anyway?
I would like to know what is going on behind the scenes. I would like to know what is going on with memory and pointers.
Here when I pass a variable by value, fill memory works, but if I pass a pointer to the variable using varptr() it doesn't work, so what is the difference
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|