|
-
Jan 9th, 2007, 09:23 PM
#1
Thread Starter
Member
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:
Dim trigger As Boolean
Dim looper As Boolean
Dim counter As Integer
Private Sub Command1_Click()
Wait 4
trigger = False
looper = False
counter = 9022 'store value
Do While trigger = False
If looper = False Then
counter = counter + 1 ' now is 9023
Me.Print counter ' show me
looper = True
Else
counter = counter - 1 ' next value back to original
Me.Print counter ' show me
looper = False
End If
Wait 5
Form1.Cls
Loop
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:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal bytes As Long)
Dim intValue As Long
Private Sub Command1_Click()
Wait 4
Dim address As Long
address = 1369624 'decimal form
intValue = 4444 ' new value
CopyMemory address, intValue, 4
End Sub
I have tried..
VB Code:
CopyMemory byval address, intValue, 4
but all I get is a crash.
Any suggestions would be great! TY^^
-
Jan 10th, 2007, 11:16 AM
#2
Addicted Member
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).
-
Jan 11th, 2007, 09:29 PM
#3
Thread Starter
Member
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. :-)
-
Jan 12th, 2007, 05:26 AM
#4
Addicted Member
Re: CopyMemory API
This copies from one variable to another.
VB Code:
Private Sub Command1_Click()
Dim lngMyVariable As Long
Dim lngNewValue As Long
lngMyVariable = 12345
Debug.Print lngNewValue 'Outputs 0
Call CopyMemory(lngNewValue, lngMyVariable, 4)
Debug.Print lngNewValue 'Outputs 12345
End Sub
-
Jan 12th, 2007, 06:44 AM
#5
Thread Starter
Member
Re: CopyMemory API
Now if I could only get that to work with a location in memory!
-
Jan 12th, 2007, 09:32 AM
#6
Addicted Member
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:
'<CSCM>
'---------------------------------------------------------------------
'-- Name: Peek
'-- By: <SGD> on 04/19/2006
'-- Desc: Allows you to examine the contents of memory at any address
'-- for any number of bytes and returns this as a string of bytes.
'---------------------------------------------------------------------
'-- Note: To return STRING data, use PeekStr instead.
'---------------------------------------------------------------------
'-- Revd: <SGD> on 04/19/2006
'---------------------------------------------------------------------
'-- Parm: lngAddr (Long) = The VARPTR of the variable to read, or memory address
'-- lngBytes (Long) = The number of bytes to read.
'-- Typically, this value is defined as:
'-- Byte = 1
'-- Boolean = 2 (Booleans are implemented as Integers)
'-- Integer = 2
'-- Long = 4
'-- Single = 4
'-- Currency= 8 '-- Use CVC (return string) * 10,000 to get real value
'-- Double = 8 '-- Use CVC (return string) * 10,000 to get real value (CVD does NOT work)
'-- Date = 8
'-- Variant = 16
'---------------------------------------------------------------------
'</CSCM>
Public Function Peek(ByVal lngAddr As Long, _
ByVal lngBytes As Long) As String
Peek = Space$(lngBytes)
CopyMemory ByVal Peek, ByVal lngAddr, ByVal lngBytes
End Function
Public Sub PokeLng(ByVal lngAddr As Long, _
ByVal lngValue As Long)
Call CopyMemory(ByVal lngAddr, lngValue, 4&)
End Sub
Last edited by sigid; Jan 12th, 2007 at 09:36 AM.
-
Jan 12th, 2007, 12:20 PM
#7
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|