Microsoft KB Archive/254074: Difference between revisions
From BetaArchive Wiki
m (Text replacement - "&" to "&") |
m (Text replacement - """ to """) |
||
Line 45: | Line 45: | ||
void _tmain(int argc, TCHAR *argv[]) | void _tmain(int argc, TCHAR *argv[]) | ||
{ | { | ||
TCHAR szAvailBytes[256] = TEXT( | TCHAR szAvailBytes[256] = TEXT(""); | ||
TCHAR szCacheBytes[256] = TEXT( | TCHAR szCacheBytes[256] = TEXT(""); | ||
TCHAR szWorkingSet[256] = TEXT( | TCHAR szWorkingSet[256] = TEXT(""); | ||
DWORD dwBufferSize = sizeof(szAvailBytes); | DWORD dwBufferSize = sizeof(szAvailBytes); | ||
HCOUNTER hAvailBytes, hCacheBytes, hWorkingSet; | HCOUNTER hAvailBytes, hCacheBytes, hWorkingSet; | ||
Line 61: | Line 61: | ||
pdhStatus = PdhOpenQuery(NULL, 0, &hQuery); | pdhStatus = PdhOpenQuery(NULL, 0, &hQuery); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhOpenQuery failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 69: | Line 69: | ||
// | // | ||
pdhCpe.szMachineName = argv[1]; | pdhCpe.szMachineName = argv[1]; | ||
pdhCpe.szObjectName = TEXT( | pdhCpe.szObjectName = TEXT("Memory"); | ||
pdhCpe.szInstanceName = NULL; | pdhCpe.szInstanceName = NULL; | ||
pdhCpe.szParentInstance = NULL; | pdhCpe.szParentInstance = NULL; | ||
pdhCpe.dwInstanceIndex = -1; | pdhCpe.dwInstanceIndex = -1; | ||
pdhCpe.szCounterName = TEXT( | pdhCpe.szCounterName = TEXT("Available Bytes"); | ||
pdhStatus = PdhMakeCounterPath(&pdhCpe, szAvailBytes, | pdhStatus = PdhMakeCounterPath(&pdhCpe, szAvailBytes, | ||
&dwBufferSize, 0); | &dwBufferSize, 0); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"), | ||
pdhStatus); | pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
pdhCpe.szCounterName = TEXT( | pdhCpe.szCounterName = TEXT("Cache Bytes"); | ||
dwBufferSize = sizeof(szCacheBytes); | dwBufferSize = sizeof(szCacheBytes); | ||
pdhStatus = PdhMakeCounterPath(&pdhCpe, szCacheBytes, | pdhStatus = PdhMakeCounterPath(&pdhCpe, szCacheBytes, | ||
&dwBufferSize, 0); | &dwBufferSize, 0); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"), | ||
pdhStatus); | pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
pdhCpe.szObjectName = TEXT( | pdhCpe.szObjectName = TEXT("Process"); | ||
pdhCpe.szInstanceName = TEXT( | pdhCpe.szInstanceName = TEXT("_Total"); | ||
pdhCpe.szCounterName = TEXT( | pdhCpe.szCounterName = TEXT("Working Set"); | ||
dwBufferSize = sizeof(szWorkingSet); | dwBufferSize = sizeof(szWorkingSet); | ||
pdhStatus = PdhMakeCounterPath(&pdhCpe, szWorkingSet, | pdhStatus = PdhMakeCounterPath(&pdhCpe, szWorkingSet, | ||
&dwBufferSize, 0); | &dwBufferSize, 0); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhMakeCounterPath failed with %u\n"), | ||
pdhStatus); | pdhStatus); | ||
__leave; | __leave; | ||
Line 110: | Line 110: | ||
pdhStatus = PdhAddCounter(hQuery, szAvailBytes, 0, &hAvailBytes); | pdhStatus = PdhAddCounter(hQuery, szAvailBytes, 0, &hAvailBytes); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 116: | Line 116: | ||
pdhStatus = PdhAddCounter(hQuery, szCacheBytes, 0, &hCacheBytes); | pdhStatus = PdhAddCounter(hQuery, szCacheBytes, 0, &hCacheBytes); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 122: | Line 122: | ||
pdhStatus = PdhAddCounter(hQuery, szWorkingSet, 0, &hWorkingSet); | pdhStatus = PdhAddCounter(hQuery, szWorkingSet, 0, &hWorkingSet); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhAddCounter failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 131: | Line 131: | ||
pdhStatus = PdhCollectQueryData(hQuery); | pdhStatus = PdhCollectQueryData(hQuery); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhCollectQueryData failed with %u\n"), | ||
pdhStatus); | pdhStatus); | ||
__leave; | __leave; | ||
Line 141: | Line 141: | ||
pdhStatus = PdhGetFormattedCounterValue(hAvailBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtAvail); | pdhStatus = PdhGetFormattedCounterValue(hAvailBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtAvail); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 147: | Line 147: | ||
pdhStatus = PdhGetFormattedCounterValue(hCacheBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtCache); | pdhStatus = PdhGetFormattedCounterValue(hCacheBytes, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtCache); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
Line 153: | Line 153: | ||
pdhStatus = PdhGetFormattedCounterValue(hWorkingSet, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtWorking); | pdhStatus = PdhGetFormattedCounterValue(hWorkingSet, PDH_FMT_LONG | PDH_FMT_NOSCALE, NULL, &pdhfmtWorking); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhGetFormattedCounterValue failed with %u\n"), pdhStatus); | ||
__leave; | __leave; | ||
} | } | ||
_tprintf(TEXT( | _tprintf(TEXT("Avail Bytes = %u\n"), pdhfmtAvail.longValue); | ||
_tprintf(TEXT( | _tprintf(TEXT("Cache Bytes = %u\n"), pdhfmtCache.longValue); | ||
_tprintf(TEXT( | _tprintf(TEXT("Working Set = %u\n"), pdhfmtWorking.longValue); | ||
_tprintf(TEXT( | _tprintf(TEXT("Physical Mem = %u mb\n"), (pdhfmtAvail.longValue + pdhfmtCache.longValue + pdhfmtWorking.longValue)/(1024*1024)); | ||
} | } | ||
__finally{ | __finally{ | ||
Line 166: | Line 166: | ||
pdhStatus = PdhCloseQuery(hQuery); | pdhStatus = PdhCloseQuery(hQuery); | ||
if (pdhStatus != ERROR_SUCCESS){ | if (pdhStatus != ERROR_SUCCESS){ | ||
_tprintf(TEXT( | _tprintf(TEXT("PdhCloseQuery failed with %u\n"), | ||
pdhStatus); | pdhStatus); | ||
} | } |
Latest revision as of 12:52, 21 July 2020
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 |