Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Visual Basic 2005 Basics
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 31st, 2008, 04:11 AM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default ReadProcessMemory returns nothing

Hi,

I have a function that gathers info through readprocessmemory, converts it to ASCII and puts it in a textbox, atleast, that's what I want it to do, but it returns nothing and results in an error.

Code:
Unable to cast object of type 'System.Byte' to type 'System.Byte[]'.
Part of the code:

Code:
        Dim hwnd, idprocess, hprocess As Integer
        Dim mapnamebyte As Object
        hwnd = FindWindow(vbNullString, appname)
        If hwnd < 0 Or hwnd > 0 Then
            GetWindowThreadProcessId(hwnd, idprocess)
            hprocess = OpenProcess(PROCESS_VM_READ, False, idprocess)
            ReadProcessMemory(hprocess, address, mapnamebyte, 16, vbNullString)
            Dim string = System.Text.Encoding.UTF8.GetString(mapnamebyte, vbNullString, 16)
            txt_mapname.Text = string
            CloseHandle(hwnd)
        Else
            txt_mapname.Text = "Could not get mapname"
        End If
I've tried to Dim mapnamebyte as Byte, Object or nothing, but it just doesnt return anything in the textbox.
What am I doing wrong ?

Thanks in advance,
evolution445
 
Old September 2nd, 2008, 06:44 AM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone has an idea of what I'm doing wrong ?

 
Old September 2nd, 2008, 12:29 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Apparently you have dimmed mapnamebyte (which, if it were me, I would naem MapNameByte for readability) as a byte, but whichever line you are getting the error on is trying to treat it as an array of bytes.

Try Dim MapNameByte() As Byte, using parens to establish it as an array.

Or repost, specifying which line is throwing this error.
 
Old September 3rd, 2008, 08:00 AM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've changed it to "MapNameByte()", and now this is my error:

Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'.

The error is cast on this line at the moment:

Code:
ReadProcessMemory(hprocess, &H6B4E25, MapNameByte(), 16, vbNullString)
Also, do I have to do something with "lpNumberOfBytesWritten", which I currently set to "vbNullString" ?

 
Old September 3rd, 2008, 02:43 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I didn't say to add parens to the Function call, but to the Dim statement. So try:
Code:
    ...

    Dim MapNameByte() As Byte

    ...

    ReadProcessMemory(hprocess, &H6B4E25, MapNameByte, 16, vbNullString)
 
Old September 3rd, 2008, 02:54 PM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It still gives the error, without the parents inside the function.

I do remember, before I didn't had this error. Instead, there was a different error on the convert line. It wouldn't want to convert the bytes to ASCII. I then mixed up the variables a little, and now I'm stuck..
 
Old September 5th, 2008, 08:48 AM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Anyone ?
 
Old September 12th, 2008, 08:23 AM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was hoping to have finished my program a few weeks ago. It appears no-one everywhere has the knowledge of how 'Readprocessmemory' works ?
 
Old September 12th, 2008, 01:52 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

MapNameByte is the area to which the results of the function will be written. So, noturally, there needs to be a suitably sized area set asside to receive those data.

So, I would:
Code:
    Dim hWnd As Integer = FindWindow(vbNullString, AppName)
    Dim idProc, hProc   As Integer
    Dim MapNameByte(16) As Byte

    If hwnd <> 0 Then

        GetWindowThreadProcessId(hWnd, idProc)
        hProc = OpenProcess(PROCESS_VM_READ, False, idProc)
        ReadProcessMemory(hProc, addr, MapNameByte, 16, vbNullString)
        ' Don’t use a VB Type as a Var Name.
        ' And where is the declaration of the type? “... As ...”
        Dim sString = System.Text.Encoding.UTF8.GetString(MapNameByte, _
                                                          vbNullString, 16) 
        txt_MapName.Text = sString
        CloseHandle(hWnd)

    Else

        txt_mapname.Text = "Could not get MapName"

    End If
    I am not familiar with the GetString() function, so I’ll just presume you have it right. After all, your question is on a different topic...

What I’ve done (aside from making the var names shorter, and easier to read) is set aside 17 bytes as an array (or 16; I forget how the initializing argument works...). The name of that array is actually a pointer to the memory, when used in an API fuction.
 
Old September 12th, 2008, 02:20 PM
Authorized User
 
Join Date: Aug 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The declaration of the type ? You mean 'vbNullString' ? As far as I know it stands for 'nothing'. I did that cause I didnt have a clue what to do with 'lpNumberOfBytesWritten'. Is that the function that you have to 'set aside' so it can receive the info ?






Similar Threads
Thread Thread Starter Forum Replies Last Post
for-each carriage returns TiLaser XSLT 4 April 28th, 2008 07:31 PM
Not all code returns a value Doom C# 4 February 11th, 2008 03:48 PM
using() in a method that returns.... belphoebes_friend General .NET 1 April 12th, 2007 04:41 PM
remove returns redruff Classic ASP Basics 13 January 9th, 2007 11:56 PM
context-item returns...? maxtoroq XSLT 2 October 22nd, 2006 09:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.