Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] VirtualQueryEx

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Wed Aug 01, 2007 1:51 pm    Post subject: [C#] VirtualQueryEx Reply with quote

Ok, I'm working--or at least TRYING to work with--VirtualQueryEx, and encountering a bit of trouble.

What I'm doing is trying to populate an MBI (or MEMORY_BASIC_INFORMATION), but having a bit of trouble. When I debug and view the values, they're all 0. Here's my code:

Code:

        //the stuff below is found on MSDN

        //DWORD VirtualQueryEx(
        //    HANDLE hProcess,                    //handle to process
        //    LPCVOID lpAddress,                  //base of pages
        //    PMEMORY_BASIC_INFORMATION lpBuffer, //ptr to MBI, info returned
        //    DWORD dwLength                      //size (bytes) of buffer; pointed at by lpBuffer
        //    );

        //typedef struct _MEMORY_BASIC_INFORMATION {
        //    PVOID BaseAddress;       // base of the region of pages
        //    PVOID AllocationBase;    // ptr to the base of a range of pages
        //    DWORD AllocationProtect; // memory protection, can be one of a couple, or 0 for none
        //    DWORD RegionSize;        // size [bytes] of region beginning at BaseAddress, in which all pages have identical attributes
        //    DWORD State;             // state of pages
        //    DWORD Protect;           //
        //    DWORD Type;              //
        //    }


         //Below was not found on MSDN, I made this:

[DllImport("kernel32.dll")]
        public static extern int VirtualQueryEx(IntPtr hProcess, ref object lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);

        public struct MEMORY_BASIC_INFORMATION{
            public int BaseAddress;
            public int AllocationBase;
            public int AllocationProtect;
            public int RegionSize;
            public int State;
            public int Protect;
            public int lType;
    }

//And now for where I call it. Note that this is in a different class, and the
//class that the two above are in is called ProcessMemoryReaderApi


        public System.Collections.Generic.List<string> GetRegions(IntPtr process, uint Start, uint End)
        {
            System.Collections.Generic.List<string> zeRegions = new System.Collections.Generic.List<string>();

            uint CurrentAddy = Start;
            object objAddy = CurrentAddy;

            ProcessMemoryReaderApi.MEMORY_BASIC_INFORMATION mbi = new ProcessMemoryReaderApi.MEMORY_BASIC_INFORMATION();

            while (CurrentAddy <= End)
            {
                ProcessMemoryReaderApi.VirtualQueryEx(process, ref objAddy, ref mbi, mbi.RegionSize);

                string temp = "";
                string allocProtect = mbi.AllocationProtect.ToString();

                //The following cases were taken from MSDN
                switch (allocProtect) //Don't worry about this though, it's just
                { //for the sake of making things easier
                    case "0x10" :
                        allocProtect = "PAGE_EXECUTE";
                        break;
                    case "0x20" :
                        allocProtect = "PAGE_EXECUTE_READ";
                        break;
                    case "0x40" :
                        allocProtect = "PAGE_EXECUTE_READWRITE";
                        break;
                    case "0x80" :
                        allocProtect = "PAGE_EXECUTE_WRITECOPY";
                        break;
                    case "0x01" :
                        allocProtect = "PAGE_NOACCESS";
                        break;
                    case "0x02" :
                        allocProtect = "PAGE_READONLY";
                        break;
                    case "0x04" :
                        allocProtect = "PAGE_READWRITE";
                        break;
                    case "0x08" :
                        allocProtect = "PAGE_WRITECOPY";
                        break;
                    default :
                        allocProtect = mbi.AllocationProtect.ToString();
                        break;
                }

                if (
                   allocProtect == "PAGE_EXECUTE" ||
                   allocProtect == "PAGE_EXECUTE_READ" ||
                   allocProtect == "PAGE_EXECUTE_READWRITE" ||
                   allocProtect == "PAGE_EXECUTE_WRITECOPY"
                   )
                {
                    //Basically, if we can do something with it

                    temp = CurrentAddy.ToString() + " - " + (CurrentAddy + mbi.RegionSize).ToString();

                    zeRegions.Add(temp); //then add it to the list
                }

                CurrentAddy += ((uint)mbi.RegionSize + 1); //Skip over to the
            } //next region

            return zeRegions; //return the list
        }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites