VB Reading Memory Values (like PEEK) in QBasic

I need to find out how I can read memory values like i used to with the PEEK function in QBasic.. Only in VisualBasic...

I'm willing to give Good points to someone who can give me some good info (including some examples etc)

Thanks!

stewartbrownAsked:
Who is Participating?
 
ArkConnect With a Mentor Commented:
'Change lib name to your vb version
Private Declare Sub GetMem1 Lib "msvbvm50.dll" (ByVal MemAddress As Long, var As Byte)
Private Declare Sub GetMem2 Lib "msvbvm50.dll" (ByVal MemAddress As Long, var As Integer)
Private Declare Sub GetMem4 Lib "msvbvm50.dll" (ByVal MemAddress As Long, var As Long)

'=====Using======
Public Function Peek(ByVal MemAddress As Long, ByVal nBytes As Long) As Variant
    If nBytes < 1 Then Exit Function
    Dim i As Long
    Dim x() As Byte
    Redim x(1 To nBytes)
    For i = 1 To nBytes
        Call GetMem1(MemAddress + i, x(i))
    Next i
    Peek = x
End Function

Cheers
0
 
Richie_SimonettiIT OperationsCommented:
Hearing...
0
 
stewartbrownAuthor Commented:
Ok, i'm putting the points up another 100, but i need you to write some more code.. showing it actually returning values (some examples)  i kinda want to see it work.

just have a msgbox show a couple return values..

(can you list a few common values one can track in windows?)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
stewartbrownAuthor Commented:
Ok, i'm putting the points up another 100, but i need you to write some more code.. showing it actually returning values (some examples)  i kinda want to see it work.

just have a msgbox show a couple return values..

(can you list a few common values one can track in windows?)
0
 
stewartbrownAuthor Commented:
Richie_Simonetti, what do you mean "Hearing..."? :)

btw sorry for the double post... dunno how that happened..
0
 
andysalihCommented:
hearing means listening,

the user is interested in your question but may not know the answer so he is taking note on comments being passed.

comprenday
0
 
ArkCommented:
Private Declare Sub GetMem4 Lib "msvbvm50.dll" (ByVal MemAddress As Long, var As Long)

Private Sub Command1_Click()
   Dim x As Long, y As Long, ptrMem As Long
   x = 55
   ptrMem = VarPtr(x)
'Read value from ptrMem address intp y variable:
   GetMem4 ptrMem, y
   Debug.Print y
End Sub

Cheers
0
 
ArkCommented:
BTW, let me to explain a bit more:
Win32 memory management isn't same as under DOS. DOS mode have global memory space and you can acces any memory address with PEEK QB function. But under Win32 every process have its own memory space and it isn't so simple to read memory belong to another process (to tell the truth, memory management under Win32 is more complicated and is diffrent for w95 and NT. If you're interesting with memory programming, read Jeffry Richter's "Programming application for MS Windows" book.) and often cause GPF when trying access protected memory addresses. There are a lot of memory functions in API, choose one which suit your needs.

Cheers
0
 
stewartbrownAuthor Commented:
Yes, i had kinda figured that memory(handling) in Windows would be quite a bit different than in the good ol' day's of DOS...

Anyways, my main purpose for trying this is to set up a reader program for information that is placed in memory by a piece of hardware. The hardware takes a reading and then stores that reading in a particular location... This in itself hasn't been acheived yet.. but I'm working under the assumption that it will work when done.

Another Question (i'll add another 50 points)

Are you aware of a common memory block that windows doesn't "protect" so to speak, one that can be used freely for storage of such values?

thanks again.. sorry for dragging this out so long...
0
 
ArkCommented:
Hi
Memory mangement in win95/98/ME and NT/2000/XP is different. Hardware drivers write its data at so called "kernel memory" area. win95 map this data int shareable area, so all processes can read (and even write!) into these addresses. To increase security, NT system totally protect these addresses and you even can not read data from them from win32 processes. There is some hacks with VirtualProtectEx and Read/WriteThreadContext API which allow access low level programming (ring0) from win32 process, but basically you have to write 16-bit vxd for this purpose.

For example, take a look on my code at http://www.freevbcode.com/ShowCode.Asp?ID=795 
for reading BIOS data directly from memory. Works perfectly under w95/98/ME, but cause GPF on NT system.

Cheers
0
 
Richie_SimonettiIT OperationsCommented:
To avoid double posted use link "Reload question" (at top of page) instead refresh button of your browser.
0
 
Richie_SimonettiIT OperationsCommented:
I don't know where it comes from. Sorry.


How do I do Peek and Poke and other low-level stuff?
VB provides no direct mechanism for this.

On The Microsoft Developer Network Library CD, you?ll find a utility called VBASM which provides full access to all sorts of low-level routines: peek, poke, memory pointer, port i/o and interrupts. You should be able to find vbasm.dll easily by searching for it on the net.

[Deane Gardner <deaneg@ix.netcom.com> quotes Microsoft]

There are several 3rd party packages which provide this functionality. Also, this often comes up in regards to the communication ports and you can many times do what you want with the MsComm control (which comes with the professional/enterprise editions of VB).

0
 
Richie_SimonettiIT OperationsCommented:
0
 
PaullkhaCommented:
This is from this link:

http://www.vb2themax.com/Item.asp?PageID=CodeBank&Cat=1600&ID=123



Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As _
    Any, source As Any, ByVal bytes As Long)

' read a byte from memory

Function PeekB(ByVal address As Long) As Byte
    CopyMemory PeekB, ByVal address, 1
End Function

' read an integer from memory

Function PeekI(ByVal address As Long) As Integer
    CopyMemory PeekI, ByVal address, 2
End Function

' read a Long value from memory

Function PeekL(ByVal address As Long) As Long
    CopyMemory PeekL, ByVal address, 4
End Function
0
 
PaullkhaCommented:
this link is better:


read/write string,byte,long, etc.


http://www.vb2themax.com/ItemBank.asp?PageID=CodeBank&Cat=1600#CAT
0
 
stewartbrownAuthor Commented:
Ok, I think you've all been very helpful in finding out information on this subject..


I'm going to award the points for this question to Ark, but i'll compose two additional questions for Richie_Simonetti and Paullkha to answer for 100 points each.

Please drop a line here telling me you understand(Richie & Paullkha) so that the questions can be answered sooner rather than later (or not at all) and when you've each got your points.. i'll award you yours Ark,

Thanks
0
 
Richie_SimonettiIT OperationsCommented:
You award the points at your best knowledge and if they solve your problem.
It's OK to me if my comments deserve them.
You are a nice person, i think.
Cheers
0
 
PaullkhaCommented:
Sure, sounds fine. Thanks!
0
 
stewartbrownAuthor Commented:
This was just a spur of the moment type Question, i actually might not try and use the code for many months.. And i wouldn't want you to wait that long.. :)


your points will be "posted" in the Lounge part of the EE area.. just to save cluttering up the VB area..

http://www.experts-exchange.com/jsp/qList.jsp?ta=lounge

Thanks again for all your help.

richie = http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=lounge&qid=20181725

paulkha = http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=lounge&qid=20181724
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.