Desaware Home
Products    Purchase    Publishing    Articles   Support    Company    Contact    
Licensing System
Professional Services
Universal .NET
Universal COM
.NET Books
COM Books
Visual Basic Programmer's Guide to the Win32 API
Introduction
Contents
Updates
Edition History
Exploring VB6 (series)
Developing COM/ActiveX Components with VB6: A Guide to the Perplexed
Introduction
Outline
What's New from the VB 5.0 edition
Updates
Win32 API Puzzle Book and Tutorial
Introduction
Chapter Outline
Sample Puzzle
Updates
NT Security Programming with Visual Basic 6
Updates
Visual Basic Programmer's Guide to the Windows 16 bit API
Other Books

bluebar
Contact Desaware and order today

bluebar
Sign up for Desaware's Newsletter for the latest news and tech tips.

Note: This page describes a legacy product or book. The page is available for archival purposes and as a courtesy to those who have linked to it, but is no longer being updated or maintained.
 amazon

Win32 API Puzzle Book and Tutorial
$31.96 [Print]

Sample Puzzle

Book Cover
Learn to handle any API call from VB6

From a VB programmer:

"I know you don't usually review code but I've got a one liner I'd like your input on if you please."

Specifically why isn't GetProcessMemoryInfo() working?"

Type PROCESS_MEMORY_COUNTERS
	cb As Long
	PageFaultCount As Long
	PeakWorkingSetSize As Long
	WorkingSetSize As Long
	QuotaPeakPagedPoolUsage As Long
	QuotaPagedPoolUsage As Long
	QuotaPeakNonPagedPoolUsage As Long
	QuotaNonPagedPoolUsage As Long
	PagefileUsage As Long
	PeakPagefileUsage As Long
End Type

Declare Function GetCurrentProcessId Lib _ 
"kernel32" () As Long 
Private Declare Function GetProcessMemoryInfo Lib _ 
"psapi.dll" (ByVal lHandle As Long, lpStructure As _ 
Long, ByVal lSize As Long) As Integer 

Sub Main() 
    Dim lReturn As Long Dim uMemory As PROCESS_MEMORY_COUNTERS 
    lReturn = GetProcessMemoryInfo( _ 
    GetCurrentProcessId(), _ 
    VarPtr(uMemory), Len(uMemory)) ' All values always zero! 
    Debug.Print uMemory.WorkingSetSize 
End Sub

Doesn't seem to work. What gives?

There are two obvious errors in the above code which I pointed out to the reader. He tried them and the program still didn't work. Since I no longer saw an obvious problem, I decided to make his question into this demo puzzle.

The first step was to look at the actual documentation for the GetProcessMemoryInfo function on MSDN.

The function is declared in C as follows:

BOOL GetProcessMemoryInfo( HANDLE Process, 
// handle to the process 
PPROCESS_MEMORY_COUNTERS ppsmemCounters, 
// structure that receives information 
DWORD cb // size of the structure); 

The Process parameter is a handle to a process.

The ppsmemCounters parameter is a pointer to the PROCESS_MEMORY_COUNTERS structure that will be loaded with information about the process.

The cb parameter is the size of the structure.

On success the function will return a non-zero value. Remember - a Win32 BOOL is defined as a 32 bit value, so it should be declared as Long in the VB declaration.

The PROCESS_MEMORY_COUNTERS structure is defined as follows:

typedef struct _PROCESS_MEMORY_COUNTERS { 
	DWORD cb;
	DWORD PageFaultCount; 
	DWORD PeakWorkingSetSize; 
	DWORD WorkingSetSize; 
	DWORD QuotaPeakPagedPoolUsage; 
	DWORD QuotaPagedPoolUsage; 
	DWORD QuotaPeakNonPagedPoolUsage; 
	DWORD QuotaNonPagedPoolUsage; 
	DWORD PagefileUsage; 
	DWORD PeakPagefileUsage;
} PROCESS_MEMORY_COUNTERS;

All of the fields of this structure are DWORD type, which is an unsigned long data type. This type is 32 bits long, so will correspond to the Visual Basic Long data type. The fact that it doesn't matter here whether the variables are signed or unsigned is beyond what I can discuss in this demo puzzle, but is covered in the tutorials.

The meaning of the fields is fairly self explanatory based on their names except for the cb field, which contains the length of the structure. Refer to you online Windows documentation for further details.

Care to try your hand at solving the puzzle? You can download the test form at ftp://ftp.desaware.com/SampleCode/DemoPuzzles/frmPuzzle1.frm

Results:

When you try to run this program, you will see one of two possible results. Either the function will return zero (indicating that it failed) and the fields in the PROCESS_MEMORY_COUNTERS fields will remain zero. Or the function will fail with an error indicating that it cannot find psapi.dll.

Your challenge: Figure out how to make this function work.

Hints:

The book includes an appendix containing hints for each chapter. For this example, allow me to offer you two suggestions:

  1. Take a closer look at the Windows documentation for the GetCurrentProcessId function. And while you're at it, you might want to take a quick look at the GetCurrentProcess function.

  2. Consider what the GetProcessMemoryInfo expects to see as its parameters. Is it getting what it expects?

Ready to look at the solution?

Books
Want to learn even more advanced API techniques?
 
Products    Purchase    Articles    Support    Company    Contact
Copyright© 2012 Desaware, Inc. All Rights Reserved.    Privacy Policy