Results 1 to 7 of 7

Thread: CopyMemory API

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    CopyMemory API

    I have been learning about memory. Slowly it is coming together but I am still have problems. I made a simple app that stores a value in a variable. A loop then starts and in that loop I use Bool to switch back and forth adding 1 to variable and then changing it back by subtract 1. I made it continue to switch so I could find its location in memory. I found it with no problems.

    Its location:

    Hex 0014E618

    Dec 1369624

    Just so you can check me if needed here is simple app:

    VB Code:
    1. Dim trigger As Boolean
    2. Dim looper As Boolean
    3. Dim counter As Integer
    4.  
    5.  
    6. Private Sub Command1_Click()
    7. Wait 4
    8. trigger = False
    9. looper = False
    10. counter = 9022  'store value
    11.   Do While trigger = False
    12.     If looper = False Then
    13.      counter = counter + 1  ' now is 9023
    14.      Me.Print counter   ' show me
    15.      looper = True
    16.     Else
    17.      counter = counter - 1   ' next value back to original
    18.      Me.Print counter  ' show me
    19.      looper = False
    20.     End If
    21. Wait 5
    22. Form1.Cls
    23.   Loop
    24. End Sub


    Now I know the location of this variable in memory. I try to use CopyMemory to change the value like so...

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)
    4. Dim intValue As Long
    5.  
    6. Private Sub Command1_Click()
    7.  
    8.    Wait 4
    9.    Dim address As Long
    10.    address = 1369624   'decimal form
    11.    intValue = 4444  ' new value
    12.    CopyMemory address, intValue, 4
    13.  
    14. End Sub


    I have tried..

    VB Code:
    1. CopyMemory byval address, intValue, 4

    but all I get is a crash.


    Any suggestions would be great! TY^^

  2. #2
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: CopyMemory API

    The answer is easy. You can't do this - at least not in this way...

    VB may, at its sole discretion, move variables around in memory during program execution. You need to get the address of the variable IMMEDIATELY before trying to use it. The VARPTR function works fine for a numeric variable. Modify your code so that ADDRESS is made = VARPTR (varname) immediately before each usage, and see if that works.

    And - you use "intValue" as a variable name, but if this is REALLY an INTEGER, then the number of bytes to copy is 2, not 4. Use 4 only if intValue is a LONG (and I do NOT mean a variant of type LONG, I mean a real LONG).

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    Re: CopyMemory API

    "You need to get the address of the variable IMMEDIATELY before trying to use it."

    I have been. I get the location each time. This seems like a tough topic. Anyone care to step in?

    I have a location in memory in hex form. I know its value and I know what value I want it to be. Please point me in the right direction.


    "Dim intValue As Long"

    For the record.. it is Long. Just forgot to chage name. :-)

  4. #4
    Addicted Member
    Join Date
    Mar 2006
    Posts
    178

    Re: CopyMemory API

    This copies from one variable to another.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim lngMyVariable As Long
    3. Dim lngNewValue As Long
    4.  
    5. lngMyVariable = 12345
    6. Debug.Print lngNewValue    'Outputs 0
    7.  
    8. Call CopyMemory(lngNewValue, lngMyVariable, 4)
    9. Debug.Print lngNewValue    'Outputs 12345
    10.  
    11. End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    Re: CopyMemory API

    Now if I could only get that to work with a location in memory!

  6. #6
    Addicted Member sigid's Avatar
    Join Date
    May 2006
    Location
    Massachusetts, USA
    Posts
    182

    Re: CopyMemory API

    This is code I use regularly - maybe it will help you out to some extent:
    (and as a historical footnote, PEEK and its complement, POKE, used to be intrinsic commands in DOS BASIC - this function merely recreates the PEEK fun ctionality)

    VB Code:
    1. '<CSCM>
    2. '---------------------------------------------------------------------
    3. '-- Name:   Peek
    4. '--   By:   <SGD> on 04/19/2006
    5. '-- Desc:   Allows you to examine the contents of memory at any address
    6. '--     for any number of bytes and returns this as a string of bytes.
    7. '---------------------------------------------------------------------
    8. '-- Note:   To return STRING data, use PeekStr instead.
    9. '---------------------------------------------------------------------
    10. '-- Revd:   <SGD> on 04/19/2006
    11. '---------------------------------------------------------------------
    12. '-- Parm:   lngAddr (Long)      = The VARPTR of the variable to read, or memory address
    13. '--         lngBytes (Long)     = The number of bytes to read.
    14. '--             Typically, this value is defined as:
    15. '--                 Byte    = 1
    16. '--                 Boolean = 2     (Booleans are implemented as Integers)
    17. '--                 Integer = 2
    18. '--                 Long    = 4
    19. '--                 Single  = 4
    20. '--                 Currency= 8     '-- Use CVC (return string) * 10,000 to get real value
    21. '--                 Double  = 8     '-- Use CVC (return string) * 10,000 to get real value (CVD does NOT work)
    22. '--                 Date    = 8
    23. '--                 Variant = 16
    24. '---------------------------------------------------------------------
    25. '</CSCM>
    26. Public Function Peek(ByVal lngAddr As Long, _
    27.                      ByVal lngBytes As Long) As String
    28.     Peek = Space$(lngBytes)
    29.     CopyMemory ByVal Peek, ByVal lngAddr, ByVal lngBytes
    30. End Function
    31.  
    32. Public Sub PokeLng(ByVal lngAddr As Long, _
    33.                    ByVal lngValue As Long)
    34.     Call CopyMemory(ByVal lngAddr, lngValue, 4&)
    35. End Sub
    Last edited by sigid; Jan 12th, 2007 at 09:36 AM.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2006
    Posts
    36

    Re: CopyMemory API

    I can copy and change variables within my app with no problem. The part I am having a problem with is changing a value that I located in memory. I found this ref but can't get it to work.

    When the source or the destination is a memory locationyou should pass a 32-bit address by value, for example:

    ' Copy the contents of a Long variable to the memory location
    ' pointed to by the "address" variable
    Dim address As Long
    ...
    CopyMemory ByVal address, lngValue, 4

    location in memory is &H14E618

    current value 9022

    During my search I found a program that finds locations of values in memory. Its called artmoney. I used it on my app and changed the value stored at that location. I am sure there is a way to do the same thing using api.
    Last edited by christiangibbs; Jan 12th, 2007 at 12:25 PM.

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