Microsoft KB Archive/254074

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 11:35, 21 July 2020 by X010 (talk | contribs) (Text replacement - "&" to "&")

HOWTO: Determine Physical Memory on a Remote Machine

Q254074



The information in this article applies to:


  • Microsoft Win32 Application Programming Interface (API), included with:
    • Microsoft Windows NT Server version 4.0
    • Microsoft Windows NT Workstation version 4.0
    • the operating system: Microsoft Windows 2000





SUMMARY

You can approximate the physical memory on a remote machine by querying the following performance counters and summing the total:

Memory -> Available Bytes
Memory -> Cache Bytes
Process -> Working Set -> _Total



MORE INFORMATION

Use the following sample code to obtain the physical memory on a remote machine:

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

void _tmain(int argc, TCHAR *argv[])
{
    TCHAR                     szAvailBytes[256] = TEXT("");
    TCHAR                     szCacheBytes[256] = TEXT("");
    TCHAR                     szWorkingSet[256] = TEXT("");
    DWORD                     dwBufferSize = sizeof(szAvailBytes);
    HCOUNTER                  hAvailBytes, hCacheBytes, hWorkingSet;
    HQUERY                    hQuery = NULL;
    PDH_COUNTER_PATH_ELEMENTS pdhCpe;
    PDH_STATUS                pdhStatus;
    PDH_FMT_COUNTERVALUE      pdhfmtAvail,pdhfmtCache,pdhfmtWorking;

    __try{
        if (argc != 2)
            __leave;

        pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhOpenQuery failed with %u\n"), pdhStatus);
            __leave;
        }

        // 
        // Make Counter Path
        // 
        pdhCpe.szMachineName    = argv[1];
        pdhCpe.szObjectName     = TEXT("Memory");
        pdhCpe.szInstanceName   = NULL;
        pdhCpe.szParentInstance = NULL;
        pdhCpe.dwInstanceIndex  = -1;
        pdhCpe.szCounterName    = TEXT("Available Bytes");

        pdhStatus = PdhMakeCounterPath(&pdhCpe, szAvailBytes,
                                       &dwBufferSize, 0);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"),
                     pdhStatus);
            __leave;
        } 

        pdhCpe.szCounterName = TEXT("Cache Bytes");
        dwBufferSize = sizeof(szCacheBytes);
        pdhStatus = PdhMakeCounterPath(&pdhCpe, szCacheBytes,
                                       &dwBufferSize, 0);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"),
                     pdhStatus);
            __leave;
        } 

        pdhCpe.szObjectName   = TEXT("Process");
        pdhCpe.szInstanceName = TEXT("_Total");
        pdhCpe.szCounterName  = TEXT("Working Set");
        dwBufferSize = sizeof(szWorkingSet);
        pdhStatus = PdhMakeCounterPath(&pdhCpe, szWorkingSet,
                                       &dwBufferSize, 0);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"),
                     pdhStatus);
            __leave;
        } 

        // 
        // Add counters.
        // 
        pdhStatus = PdhAddCounter(hQuery, szAvailBytes, 0, &hAvailBytes);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus);
            __leave;
        } 

        pdhStatus = PdhAddCounter(hQuery, szCacheBytes, 0, &hCacheBytes);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus);
            __leave;
        } 

        pdhStatus = PdhAddCounter(hQuery, szWorkingSet, 0, &hWorkingSet);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus);
            __leave;
        } 

        // 
        // Get the data.
        // 
        pdhStatus = PdhCollectQueryData(hQuery);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhCollectQueryData failed with %u\n"), 
            pdhStatus);
            __leave;
        } 

        // 
        // Format counter values.
        // 
        pdhStatus = PdhGetFormattedCounterValue(hAvailBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtAvail);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus);
            __leave;
        } 

        pdhStatus = PdhGetFormattedCounterValue(hCacheBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtCache);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus);
            __leave;
        } 

        pdhStatus = PdhGetFormattedCounterValue(hWorkingSet, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtWorking);
        if (pdhStatus != ERROR_SUCCESS){
            _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus);
            __leave;
        } 

        _tprintf(TEXT("Avail Bytes  = %u\n"), pdhfmtAvail.longValue);
        _tprintf(TEXT("Cache Bytes  = %u\n"), pdhfmtCache.longValue);
        _tprintf(TEXT("Working Set  = %u\n"), pdhfmtWorking.longValue);
        _tprintf(TEXT("Physical Mem = %u mb\n"), (pdhfmtAvail.longValue + pdhfmtCache.longValue + pdhfmtWorking.longValue)/(1024*1024));
    }
    __finally{
        if (hQuery != NULL){
            pdhStatus = PdhCloseQuery(hQuery);
            if (pdhStatus != ERROR_SUCCESS){
                _tprintf(TEXT("PdhCloseQuery failed with %u\n"),
                         pdhStatus);
            }
        }
    }
} 

Additional query words:

Keywords : kbKernBase kbOSWinNT400 kbOSWin2000 kbSDKPlatform kbSDKWin32 kbDSupport kbGrpDSKernBase
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API


Last Reviewed: October 23, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.