Results 1 to 7 of 7

Thread: FillMemory/ZeroMemory

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804

    Question 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:
    1. Option Explicit
    2.  
    3. Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
    4. Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
    5.  
    6. Private Sub Command1_Click()
    7. Dim s$
    8. s = "8888888888888888888888888"
    9. ZeroMemory ByVal StrPtr(s), Len(s)
    10. Me.Print "s apres zeromemory: " & s
    11. 'this does work why, I want 25 zeros?
    12.  
    13. s = "8888888888888888888888888"
    14. FillMemory ByVal StrPtr(s), LenB(s), Asc("a")
    15. Me.Print "s apres fillmemory: " & s
    16. End Sub

  2. #2
    jim mcnamara
    Guest
    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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804
    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:
    1. Private Declare Sub ZeroMemory Lib "kernel32.dll" Alias "RtlZeroMemory" (Destination As Any, ByVal Length As Long)
    2. Private Declare Sub FillMemory Lib "kernel32.dll" Alias "RtlFillMemory" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
    3.  
    4. Private Sub Command1_Click()
    5. Dim s$
    6. s = "8888888888888888888888888"
    7. ZeroMemory ByVal s, LenB(s)
    8. Me.Print "s apres zeromemory: " & s
    9. 'this still doesn't work why? I want 25 zeros?
    10.  
    11. s = "8888888888888888888888888"
    12. FillMemory ByVal s, LenB(s), Asc("a")
    13. Me.Print "s apres fillmemory: " & s
    14. 'Now this does work, why does this work and zero memory fail?
    15. 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.

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253
    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.

    ?

  5. #5
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728

    Smile 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]

  6. #6
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Hmmm

    I have to ask why you'd want to do this anyway?

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Sydney Australia
    Posts
    804

    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
  •  



Click Here to Expand Forum to Full Width