Getting the current and maximum Windows registry size C program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged.

Need to add netapi32.lib (netapi32.dll) and pdh.lib to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' and 'pdh.lib' (in separate line or separated by a space) in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Getting the current and maximum registry size programmatically using C language

To show: More on various Windows registry C function usage

 

 

// ************ cplus.cpp ******************

// Determines the current and maximum registry size

// For Win XP, run on Win Xp...Change accordingly

// but it is useless here, Win Xp doesn't have quota limit :o)

// Just for fun...

// #define _WIN32_WINNT 0x0502 // Windows Server 2003 family

// For Win Xp, change accordingly...

#define _WIN32_WINNT 0x0501

// #define _WIN32_WINNT 0x0500 // Windows 2000

// #define _WIN32_WINNT 0x0400 // Windows NT 4.0

// #define _WIN32_WINDOWS 0x0500 // Windows ME

// #define _WIN32_WINDOWS 0x0410 // Windows 98

// #define _WIN32_WINDOWS 0x0400 // Windows 95

 

// This is wide character/UNICODE based program

#ifndef UNICODE

#define UNICODE

#endif

 

// The header files

#include <windows.h>

#include <stdio.h>

#include <pdh.h>

 

// And don't forget to include the pdh.lib into this project...

 

// Function prototype

PDH_STATUS GetRegistrySize(LPTSTR szMachineName, LPDWORD lpdwCurrentSize, LPDWORD lpdwMaximumSize);

// Entry point for the program. This function demonstrates how to

// use the GetRegistrySize() function implemented below.

// It will use the first argument, if present, as the name of the

// computer whose registry you wish to determine. If unspecified, it will use the local computer.

 

int wmain(int argc, TCHAR *argv[])

{

LPTSTR szMachineName = NULL;

PDH_STATUS pdhStatus = 0;

DWORD dwCurrent = 0;

DWORD dwMaximum = 0;

 

// Allow a computer name to be specified on the command line.

if(argc > 1)

szMachineName = argv[1];

wprintf(TEXT("Usage: %s <computer_name> else default to local computer.\n\n"), argv[0]);

// Get the registry size.

pdhStatus = GetRegistrySize(szMachineName, &dwCurrent, &dwMaximum);

// Print the results.

if(pdhStatus == ERROR_SUCCESS)

{

printf("GetRegistrySize() is OK.\n");

wprintf(TEXT("\nCurrent registry size: %ld bytes\n"), dwCurrent);

wprintf(TEXT("Maximum registry size: %ld bytes\n"), dwMaximum);

}

else

{

// If the operation failed, print the PDH error message.

LPTSTR szMessage = NULL;

FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE,

GetModuleHandle(TEXT("PDH.DLL")), pdhStatus,

MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), szMessage, 0, NULL);

wprintf(TEXT("GetRegistrySize() failed: %s"), szMessage);

LocalFree(szMessage);

}

return 0;

}

 

// Retrieves the current and maximum registry size. It gets this

// information from the raw counter values for the "% Registry Quota

// In Use" performance counter within the System object.

// PARAMETERS:

// szMachineName - Null-terminated string that specifies the

// name of the computer whose registry you wish to query.

// If this parameter is NULL, the local computer is used.

// lpdwCurrentSize - Receives the current registry size.

// lpdwMaximumSize - Receives the maximum registry size.

// RETURN VALUE:

// ERROR_SUCCESS if successful. Otherwise, the function

// returns a PDH error code. These error codes can be

// found in PDHMSG.H. A textual error message can be

// retrieved from PDH.DLL using the FormatMessage function.

 

//******************************************************************

PDH_STATUS GetRegistrySize(LPTSTR szMachineName, LPDWORD lpdwCurrentSize, LPDWORD lpdwMaximumSize)

{

PDH_STATUS pdhResult = 0;

TCHAR szCounterPath[1024];

DWORD dwPathSize = 1024;

PDH_COUNTER_PATH_ELEMENTS pe;

PDH_RAW_COUNTER pdhRawValues;

HQUERY hQuery = NULL;

HCOUNTER hCounter = NULL;

DWORD dwType = 0;

 

// Open PDH query

pdhResult = PdhOpenQuery(NULL, 0, &hQuery);

if(pdhResult != ERROR_SUCCESS)

// Just return the error...

return pdhResult;

else

printf("PdhOpenQuery() is OK.\n");

 

// Create counter path

pe.szMachineName = szMachineName;

pe.szObjectName = TEXT("System");

pe.szInstanceName = NULL;

pe.szParentInstance = NULL;

pe.dwInstanceIndex = 1;

pe.szCounterName = TEXT("% Registry Quota In Use");

 

pdhResult = PdhMakeCounterPath(&pe, szCounterPath, &dwPathSize, 0);

if(pdhResult != ERROR_SUCCESS)

{

printf("PdhMakeCounterPath() is not OK.\n");

exit(1);

}

else

printf("PdhMakeCounterPath() is OK.\n");

 

// Add the counter to the query

pdhResult = PdhAddCounter(hQuery, szCounterPath, 0, &hCounter);

if(pdhResult != ERROR_SUCCESS)

// Here we just exit, better to retrieve and return the error code...

// same for the following...

exit(1);

else

printf("PdhAddCounter() is OK.\n");

 

// Run the query to collect the performance data

pdhResult = PdhCollectQueryData(hQuery);

if(pdhResult != ERROR_SUCCESS)

exit(1);

else

printf("PdhCollectQueryData() is OK.\n");

 

// Retrieve the raw counter data:

// The dividend (FirstValue) is the current registry size

// The divisor (SecondValue) is the maximum registry size

ZeroMemory(&pdhRawValues, sizeof(pdhRawValues));

 

pdhResult = PdhGetRawCounterValue(hCounter, &dwType, &pdhRawValues);

if(pdhResult != ERROR_SUCCESS)

exit (1);

else

printf("PdhGetRawCounterValue() is OK\n");

// Store the sizes in variables.

if(lpdwCurrentSize)

*lpdwCurrentSize = (DWORD) pdhRawValues.FirstValue;

if(lpdwMaximumSize)

*lpdwMaximumSize = (DWORD) pdhRawValues.SecondValue;

// Close the query

PdhCloseQuery(hQuery);

return 0;

}

 

Output example:

 

F:\vc2005project\cplus\d.e.b.u.g>cplus

Usage: cplus <computer_name> else default to local computer.

PdhOpenQuery() is OK.

PdhMakeCounterPath() is OK.

PdhAddCounter() is OK.

PdhCollectQueryData() is OK.

PdhGetRawCounterValue() is OK

GetRegistrySize() is OK.

Current registry size: 4814028 bytes

Maximum registry size: 58720256 bytes

F:\vc2005project\cplus\d.e.b.u.g>cplus mypersonal

Usage: cplus <computer_name> else default to local computer.

PdhOpenQuery() is OK.

PdhMakeCounterPath() is OK.

PdhAddCounter() is OK.

PdhCollectQueryData() is OK.

PdhGetRawCounterValue() is OK

GetRegistrySize() is OK.

Current registry size: 4814028 bytes

Maximum registry size: 58720256 bytes

F:\vc2005project\cplus\d.e.b.u.g>

 

 

C and C++ Programming Resources | C & C++ Code Example Index