Solved

VB Reading Memory Values (like PEEK) in QBasic

Posted on 2001-09-08
20
275 Views
Last Modified: 2011-04-14
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!

0
Comment
Question by:stewartbrown
  • 6
  • 6
  • 4
  • +2
20 Comments
 
LVL 27

Accepted Solution

by:
Ark earned 259 total points
Comment Utility
'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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
Hearing...
0
 

Author Comment

by:stewartbrown
Comment Utility
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
 

Author Comment

by:stewartbrown
Comment Utility
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
 

Author Comment

by:stewartbrown
Comment Utility
Richie_Simonetti, what do you mean "Hearing..."? :)

btw sorry for the double post... dunno how that happened..
0
 
LVL 3

Expert Comment

by:andysalih
Comment Utility
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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
 

Author Comment

by:stewartbrown
Comment Utility
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
 
LVL 27

Expert Comment

by:Ark
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
To avoid double posted use link "Reload question" (at top of page) instead refresh button of your browser.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
0
 
LVL 2

Expert Comment

by:Paullkha
Comment Utility
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
 
LVL 2

Expert Comment

by:Paullkha
Comment Utility
this link is better:


read/write string,byte,long, etc.


http://www.vb2themax.com/ItemBank.asp?PageID=CodeBank&Cat=1600#CAT
0
 

Author Comment

by:stewartbrown
Comment Utility
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
Comment Utility
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
 
LVL 2

Expert Comment

by:Paullkha
Comment Utility
Sure, sounds fine. Thanks!
0
 

Author Comment

by:stewartbrown
Comment Utility
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now