Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Win32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)"— Presentation transcript:

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

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

3 System Information  There are a lot of system information calls in Win32lot  We’ll focus on GetSystemInfo(lpSYSTEM_INFO);

4 System_Info  typedef struct _SYSTEM_INFO { union { DWORD dwOemId; struct { WORD wProcessorArchitecture; WORD wReserved; }; }; DWORD dwPageSize; LPVOID lpMinimumApplicationAddress; LPVOID lpMaximumApplicationAddress; DWORD_PTR dwActiveProcessorMask; DWORD dwNumberOfProcessors; DWORD dwProcessorType; DWORD dwAllocationGranularity; WORD wProcessorLevel; WORD wProcessorRevision; } SYSTEM_INFO;

5 Which means what?  dwPageSize: The CPU’s page size  lpMinimumApplicationAddress: The lowest usable address of process space  lpMaximumApplicationAddress: The highest usable address of process space  dwAllocationGranularity: The granularity of reserved memory regions  The rest doesn’t apply directly to memory

6 Example: Using the Call  Very straightforward – see Demo

7 GlobalMemoryStatus  Simple call, to find out the state of memory  Returns a MEMORY_STATUS structure with members: typedef struct _MEMORYSTATUS { DWORD dwLength; DWORD dwMemoryLoad; SIZE_T dwTotalPhys; SIZE_T dwAvailPhys; SIZE_T dwTotalPageFile; SIZE_T dwAvailPageFile; SIZE_T dwTotalVirtual; SIZE_T dwAvailVirtual; } MEMORYSTATUS, *LPMEMORYSTATUS;

8 Caveat Emptor  Doesn’t work well with numbers larger than 4G… use GlobalMemoryStatusEx instead (see MSDN)  But easy to use… see my Demo.NET app

9 Determining the State of an Address  Nice simple function to use: DWORD VirtualQuery( LPCVOID pvAddress, PMEMORY_BASIC_INFORMATION pmbi, DWORD dwLength); Can also do it inter-process: DWORD VirtualQueryEx… add HANDLE hProcess as the first parm…

10 MEMORY_BASIC_INFORMATION  typedef struct _MEMORY_BASIC_INFORMATION { PVOID BaseAddress; PVOID AllocationBase; DWORD AllocationProtect; SIZE_T RegionSize; DWORD State; DWORD Protect; DWORD Type; } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;

11 Values  BaseAddress: The same as the pvAddress, but rounded down to the nearest PageBoundary  AllocationBase: The start of this region  AllocationProtect: The protection attribute originally assigned to the region  RegionSize: 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 informationMSDN

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


Download ppt "Win32 Programming Lesson 15: Practical Windows Memory (If you can read this you have good vision)"

Similar presentations


Ads by Google