Win 32 Programming Lesson 15 Practical Windows Memory

  • Slides: 12
Download presentation
Win 32 Programming Lesson 15: Practical Windows Memory (If you can read this you

Win 32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)

Where are we? o o We’ve covered theory of Windows memory, but not the

Where are we? o o We’ve covered theory of Windows memory, but not the details Let’s delve down into the details…

System Information o o There a lot of system information calls in Win 32

System Information o o There a lot of system information calls in Win 32 We’ll focus on Get. System. Info(lp. SYSTEM_INFO);

System_Info o typedef struct _SYSTEM_INFO { union { DWORD dw. Oem. Id; struct {

System_Info o typedef struct _SYSTEM_INFO { union { DWORD dw. Oem. Id; struct { WORD w. Processor. Architecture; WORD w. Reserved; }; }; DWORD dw. Page. Size; LPVOID lp. Minimum. Application. Address; LPVOID lp. Maximum. Application. Address; DWORD_PTR dw. Active. Processor. Mask; DWORD dw. Number. Of. Processors; DWORD dw. Processor. Type; DWORD dw. Allocation. Granularity; WORD w. Processor. Level; WORD w. Processor. Revision; } SYSTEM_INFO;

Which means what? o o o dw. Page. Size: The CPU’s page size lp.

Which means what? o o o dw. Page. Size: The CPU’s page size lp. Minimum. Application. Address: The lowest usable address of process space lp. Maximum. Application. Address: The highest usable address of process space dw. Allocation. Granularity: The granularity of reserved memory regions The rest doesn’t apply directly to memory

Example: Using the Call o Very straightforward – see Demo

Example: Using the Call o Very straightforward – see Demo

Global. Memory. Status o o Simple call, to find out the state of memory

Global. Memory. Status o o Simple call, to find out the state of memory Returns a MEMORY_STATUS structure with members: n typedef struct _MEMORYSTATUS { DWORD dw. Length; DWORD dw. Memory. Load; SIZE_T dw. Total. Phys; SIZE_T dw. Avail. Phys; SIZE_T dw. Total. Page. File; SIZE_T dw. Avail. Page. File; SIZE_T dw. Total. Virtual; SIZE_T dw. Avail. Virtual; } MEMORYSTATUS, *LPMEMORYSTATUS;

Caveat Emptor o o Doesn’t work well with numbers larger than 4 G… use

Caveat Emptor o o Doesn’t work well with numbers larger than 4 G… use Global. Memory. Status. Ex instead (see MSDN) But easy to use… see my Demo. NET app

Determining the State of an Address o Nice simple function to use: n n

Determining the State of an Address o Nice simple function to use: n n DWORD Virtual. Query( LPCVOID pv. Address, PMEMORY_BASIC_INFORMATION pmbi, DWORD dw. Length); Can also do it inter-process: DWORD Virtual. Query. Ex… add HANDLE h. Process as the first parm…

MEMORY_BASIC_INFORMATION o typedef struct _MEMORY_BASIC_INFORMATION { PVOID Base. Address; PVOID Allocation. Base; DWORD Allocation.

MEMORY_BASIC_INFORMATION o typedef struct _MEMORY_BASIC_INFORMATION { PVOID Base. Address; PVOID Allocation. Base; DWORD Allocation. Protect; SIZE_T Region. Size; DWORD State; DWORD Protect; DWORD Type; } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;

Values o o o o Base. Address: The same as the pv. Address, but

Values o o o o Base. Address: The same as the pv. Address, but rounded down to the nearest Page. Boundary Allocation. Base: The start of this region Allocation. Protect: The protection attribute originally assigned to the region Region. Size: The size in bytes of the region State: The state (MEM_FREE, MEM_RESERVE, MEM_COMMIT) for all adjoining pages Protect: The protection attribute of all adjoining pages Type: Where is the information stored? (MEM_IMAGE, MEM_MAPPED, MEM_PRIVATE) See MSDN for more information

*Big* Example o Let’s look at a program which prints out quite a lot

*Big* Example o Let’s look at a program which prints out quite a lot of memory information…