How To Programmatically Determine the Current and Maximum Registry Size

Article translations Article translations
Article ID: 235487 - View products that this article applies to.
This article was previously published under Q235487
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

SUMMARY

On Microsoft Windows NT, Microsoft Windows 2000, and Microsoft Windows XP, it is common for an installation utility to check the current and maximum size of the registry to determine whether there is enough available space for the new data it will be adding. This article demonstrates how to do this programmatically via the performance counter named "% Registry Quota In Use" within the "System" object.

NOTE: It is not necessary to implement this registry size-check on Microsoft Windows 95, Microsoft Windows 98, or Microsoft Windows Millennium Edition (Me) because the Windows 95/98/Me registries do not have a quota limit.

MORE INFORMATION

The sample code in this article uses Performance Data Helper (PDH); it must be linked with Pdh.lib.

PDH is a high-level set of APIs used to obtain performance data on Windows NT. You can find Pdh.dll in the Platform SDK. This library is redistributable for Windows NT 4.0, as described in the following file:
%MSSDK%\license\redist.txt
				
You won't need to redistribute Pdh.dll on Windows 2000 and Windows XP, because it is already part of the operating system.

You can download the Platform SDK from the following Web site:
http://www.microsoft.com/msdownload/platformsdk/setuplauncher.htm
The following files pertain to PDH:
  • PDH.H
  • PDHMSG.H
  • PDH.LIB
  • PDH.DLL

Sample Code

//**********************************************************************
// 
//  This program determines the current and maximum registry size.
//  It uses PDH to query the "% Registry Quota In Use" counter within
//  the System object.
// 
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
//  ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
//  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
// 
//  Copyright (C) 1999 Microsoft Corporation. All rights reserved.
// 
//**********************************************************************

// NOTE: This file must be linked with PDH.LIB.

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <pdh.h>

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


//**********************************************************************
// 
//  FUNCTION:     main - This is the entry point for the program. This
//                function contains sample code demonstrating how to use
//                the GetRegistrySize() function implemented below.
// 
//  PARAMETERS:   argc - The number of command-line arguments.
// 
//                argv - An array of null-terminated strings containing
//                the command-line arguments. This program will use the
//                first argument, if present, as the name of the machine
//                whose registry you wish to determine. If unspecified,
//                it will query the local machine's registry size.
// 
//  RETURN VALUE: Always zero.
// 
//**********************************************************************

int _tmain( int argc, TCHAR *argv[] ) {

   LPTSTR      szMachineName  = NULL;
   PDH_STATUS  pdhStatus      = 0;
   DWORD       dwCurrent      = 0;
   DWORD       dwMaximum      = 0;

   // Allow a machine name to be specified on command-line
   if ( argc > 1 )
      szMachineName = argv[1];

   // Get the registry size using PDH
   pdhStatus = GetRegistrySize( szMachineName, &dwCurrent, &dwMaximum );

   // Print the results
   if ( pdhStatus == ERROR_SUCCESS ) {

      _tprintf( TEXT("Current registry size: %ld bytes\n"), dwCurrent );
      _tprintf( 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 );

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

      LocalFree( szMessage );

   }

   return 0;
}


//**********************************************************************
// 
//  FUNCTION:     GetRegistrySize - This function uses PDH to retrieve
//                the current and maximum registry size. It gets this
//                information from the raw counter values for the
//                "% Registry Quota In Use" counter within the System
//                object.
// 
//  PARAMETERS:   szMachineName - NULL-terminated string indicating the
//                name of the machine whose registry you wish to query.
//                If this parameter is NULL, the local machine's
//                registry will be checked.
// 
//                lpdwCurrentSize - Pointer to DWORD to receive the
//                current registry size.
// 
//                lpdwMaximumSize  - Pointer to DWORD to receive 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() API.
// 
//**********************************************************************

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 )
      return pdhResult;

   __try {

      // 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 )
         __leave;

      // Add the counter to the query
      pdhResult = PdhAddCounter( hQuery, szCounterPath, 0, &hCounter );
      if ( pdhResult != ERROR_SUCCESS ) 
         __leave;

      // Run the query to collect the performance data
      pdhResult = PdhCollectQueryData( hQuery );
      if ( pdhResult != ERROR_SUCCESS ) 
         __leave;

      // 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 )
         __leave;

      // Store the sizes in the supplied variables
      if ( lpdwCurrentSize )
         *lpdwCurrentSize = (DWORD) pdhRawValues.FirstValue;
      
      if ( lpdwMaximumSize )
         *lpdwMaximumSize = (DWORD) pdhRawValues.SecondValue;

   } __finally {

      // Close the query
      PdhCloseQuery( hQuery );

   }

   return 0;
}
				

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:
124594 Understanding and Configuring Registry Size Limit (RSL)

Properties

Article ID: 235487 - Last Review: February 28, 2014 - Revision: 4.2
APPLIES TO
  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000 Standard Edition
    • the operating system: Microsoft Windows XP
Keywords: 
kbnosurvey kbarchive kbhowto kbregistry kbapi kbkernbase kbperfmon kbfaq KB235487

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com