Solved

VB Reading Memory Values (like PEEK) in QBasic

Posted on 2001-09-08
20
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 4
  • +2
20 Comments
 
LVL 28

Accepted Solution

by:
Ark earned 259 total points
ID: 6467993
'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
ID: 6468015
Hearing...
0
 

Author Comment

by:stewartbrown
ID: 6468039
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:stewartbrown
ID: 6468055
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
ID: 6468076
Richie_Simonetti, what do you mean "Hearing..."? :)

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

Expert Comment

by:andysalih
ID: 6468178
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 28

Expert Comment

by:Ark
ID: 6468207
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 28

Expert Comment

by:Ark
ID: 6469282
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
ID: 6469471
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 28

Expert Comment

by:Ark
ID: 6469490
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
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6469513
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
ID: 6469517
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
ID: 6469540
0
 
LVL 2

Expert Comment

by:Paullkha
ID: 6472033
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
ID: 6472037
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
ID: 6472979
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
ID: 6473074
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
ID: 6475506
Sure, sounds fine. Thanks!
0
 

Author Comment

by:stewartbrown
ID: 6475661
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

689 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