==============================ModuleP===================================== | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Compiled using VC++/VC++ .Net.....unmanaged... // QueryKey() - Enumerates the subkeys of key and its associated values. // hKey - Key whose subkeys and values are to be enumerated. // #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 #include #include #define MAX_KEY_LENGTH 255 #define MAX_VALUE_NAME 16383 void QueryKey(HKEY hKey) { CHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name DWORD cbName; // size of name string CHAR achClass[MAX_PATH] = ""; // buffer for class name DWORD cchClassName = MAX_PATH; // size of class string DWORD cSubKeys=0; // number of subkeys DWORD cbMaxSubKey; // longest subkey size DWORD cchMaxClass; // longest class string DWORD cValues; // number of values for key DWORD cchMaxValue; // longest value name DWORD cbMaxValueData; // longest value data DWORD cbSecurityDescriptor; // size of security descriptor FILETIME ftLastWriteTime; // last write time DWORD i, retCode; CHAR achValue[MAX_VALUE_NAME]; DWORD cchValue = MAX_VALUE_NAME; // Get the class name and the value count. retCode = RegQueryInfoKey( hKey, // key handle achClass, // buffer for class name &cchClassName, // size of class string NULL, // reserved &cSubKeys, // number of subkeys &cbMaxSubKey, // longest subkey size &cchMaxClass, // longest class string &cValues, // number of values for this key &cchMaxValue, // longest value name &cbMaxValueData, // longest value data &cbSecurityDescriptor, // security descriptor &ftLastWriteTime); // last write time // Enumerate the subkeys, until RegEnumKeyEx() fails. if(cSubKeys) { printf("Subkey Names:\n"); for(i=0; i #include #include // 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 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; } ----------------------------------------------------------------------------------------------------------- // #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 #include #include #define BUFSIZE 80 int main(int argc, char *argv[]) { OSVERSIONINFOEX osver; BOOL bOsVersionInfoEx; // Try calling GetVersionEx() using the OSVERSIONINFOEX structure. // If that fails, try using the OSVERSIONINFO structure. ZeroMemory(&osver, sizeof(OSVERSIONINFOEX)); osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if(!(bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO *) &osver))) { osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if(!GetVersionEx((OSVERSIONINFO *) &osver)) return FALSE; else printf("Buffer for the structure size is OK.\n"); } else printf("GetVersionEx() is OK.\n"); switch (osver.dwPlatformId) { //Test for the Windows NT product family. case VER_PLATFORM_WIN32_NT: // Test for the specific product family. if(osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2) printf("Microsoft Windows Server 2003 family, "); if(osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1) printf("Microsoft Windows XP "); if(osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0) printf("Microsoft Windows 2000 "); if(osver.dwMajorVersion <= 4) printf("Microsoft Windows NT "); // Test for specific product on Windows NT 4.0 SP6 and later. if(bOsVersionInfoEx) { // Test for the workstation type. if(osver.wProductType == VER_NT_WORKSTATION) { if(osver.dwMajorVersion == 4) printf("Workstation 4.0 "); else if(osver.wSuiteMask & VER_SUITE_PERSONAL) printf("Home Edition "); else printf("Professional "); } // Test for the server type. else if(osver.wProductType == VER_NT_SERVER || osver.wProductType == VER_NT_DOMAIN_CONTROLLER) { if((osver.dwMajorVersion == 5) && (osver.dwMinorVersion == 2)) { if(osver.wSuiteMask & VER_SUITE_DATACENTER) printf("Datacenter Edition "); else if(osver.wSuiteMask & VER_SUITE_ENTERPRISE) printf("Enterprise Edition "); else if(osver.wSuiteMask == VER_SUITE_BLADE) printf("Web Edition "); else printf("Standard Edition "); } else if((osver.dwMajorVersion == 5) && (osver.dwMinorVersion == 0)) { if(osver.wSuiteMask & VER_SUITE_DATACENTER) printf("Datacenter Server "); else if(osver.wSuiteMask & VER_SUITE_ENTERPRISE) printf("Advanced Server "); else printf("Server "); } // Windows NT 4.0 else { if(osver.wSuiteMask & VER_SUITE_ENTERPRISE) printf("Server 4.0, Enterprise Edition "); else printf("Server 4.0 "); } } } // Test for specific product on Windows NT 4.0 SP5 and earlier else { HKEY hKey; char szProductType[BUFSIZE]; DWORD dwBufLen=BUFSIZE; LONG lRet; lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\ProductOptions", 0, KEY_QUERY_VALUE, &hKey); if(lRet != ERROR_SUCCESS) return FALSE; else printf("RegOpenKeyEx() is OK.\n"); lRet = RegQueryValueEx(hKey, "ProductType", NULL, NULL, (LPBYTE)szProductType, &dwBufLen); if((lRet != ERROR_SUCCESS) || (dwBufLen > BUFSIZE)) return FALSE; else printf("RegQueryValueEx() is OK.\n"); RegCloseKey(hKey); if(lstrcmpi("WINNT", szProductType) == 0) printf("Workstation "); if(lstrcmpi("LANMANNT", szProductType) == 0) printf("Server "); if(lstrcmpi("SERVERNT", szProductType) == 0) printf("Advanced Server "); printf("%d.%d ", osver.dwMajorVersion, osver.dwMinorVersion); } // Display service pack (if any) and build number. if(osver.dwMajorVersion == 4 && lstrcmpi(osver.szCSDVersion, "Service Pack 6") == 0) { HKEY hKey; LONG lRet; // Test for SP6 versus SP6a. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009", 0, KEY_QUERY_VALUE, &hKey); if(lRet == ERROR_SUCCESS) { printf("RegOpenKeyEx() is OK.\n"); printf("Service Pack 6a (Build %d)\n", osver.dwBuildNumber & 0xFFFF); } // Windows NT 4.0 prior to SP6a else { printf("%s (Build %d)\n", osver.szCSDVersion, osver.dwBuildNumber & 0xFFFF); } RegCloseKey(hKey); } // Windows NT 3.51 and earlier or Windows 2000 and later else { printf("%s (Build %d)\n", osver.szCSDVersion, osver.dwBuildNumber & 0xFFFF); } break; // Test for the Windows 95 product family. case VER_PLATFORM_WIN32_WINDOWS: if(osver.dwMajorVersion == 4 && osver.dwMinorVersion == 0) { printf("Microsoft Windows 95 "); if(osver.szCSDVersion[1] == 'C' || osver.szCSDVersion[1] == 'B') printf("OSR2 "); } if(osver.dwMajorVersion == 4 && osver.dwMinorVersion == 10) { printf("Microsoft Windows 98 "); if(osver.szCSDVersion[1] == 'A') printf("SE "); } if((osver.dwMajorVersion == 4) && (osver.dwMinorVersion == 90)) { printf("Microsoft Windows Millennium Edition\n"); } break; case VER_PLATFORM_WIN32s: printf("Microsoft Win32s\n"); break; } return TRUE; } ----------------------------------------------------------------------------------------------------------- // #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 #include #include // Change accordingly... #define POLICY_KEY TEXT("Software\\Policies\\Microsoft\\Windows\\Explorer") #define PREFERENCE_KEY TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer") DWORD ReadValue(LPTSTR lpValueName, DWORD dwDefault) { HKEY hKey; LONG lResult; DWORD dwValue, dwType, dwSize = sizeof(dwValue); // First, check for a policy. lResult = RegOpenKeyEx(HKEY_CURRENT_USER, POLICY_KEY, 0, KEY_READ, &hKey); if(lResult == ERROR_SUCCESS) { lResult = RegQueryValueEx(hKey, lpValueName, 0, &dwType, (LPBYTE)&dwValue, &dwSize); RegCloseKey(hKey); } // Exit if a policy value was found. if(lResult == ERROR_SUCCESS) { // return the data value return dwValue; } else printf("Policy: value not found!\n"); // Second, check for a preference. lResult = RegOpenKeyEx(HKEY_CURRENT_USER, PREFERENCE_KEY, 0, KEY_READ, &hKey); if(lResult == ERROR_SUCCESS) { lResult = RegQueryValueEx(hKey, lpValueName, 0, &dwType, (LPBYTE)&dwValue, &dwSize); RegCloseKey (hKey); } // Exit if a preference was found. if(lResult == ERROR_SUCCESS) { // Return the data value return dwValue; } else printf("Preference: value not found!\n"); // Neither a policy nor a preference was found; return the default value. return dwDefault; } int main() { LPTSTR lpValueName = "Browse For Folder Height"; DWORD dwDefault = 0x00000000; DWORD ret = ReadValue(lpValueName, dwDefault); printf("The value data for the \'%s\' value name is 0X%.8X(%d).\n", lpValueName, ret, ret); return 0; } ------------------------------------------------------------------------------------------------------------- // #define _WIN32_WINNT 0x0502 // Windows Server 2003 family // For Win Xp, change accordingly... #define _WIN32_WINNT 0x0501 // #define _WIN32_WINNT 0x0500 // Windows 2000 #include #include #include #include #include #define TOTALBYTES 20000 #define BYTEINCREMENT 2048 LPSTR lpNameStrings; LPSTR *lpNamesArray; // Functions used to navigate through the performance data. PPERF_OBJECT_TYPE FirstObject(PPERF_DATA_BLOCK PerfData) { return((PPERF_OBJECT_TYPE)((PBYTE)PerfData + PerfData->HeaderLength)); } PPERF_OBJECT_TYPE NextObject(PPERF_OBJECT_TYPE PerfObj) { return((PPERF_OBJECT_TYPE)((PBYTE)PerfObj + PerfObj->TotalByteLength)); } PPERF_INSTANCE_DEFINITION FirstInstance(PPERF_OBJECT_TYPE PerfObj) { return((PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj + PerfObj->DefinitionLength)); } PPERF_INSTANCE_DEFINITION NextInstance(PPERF_INSTANCE_DEFINITION PerfInst) { PPERF_COUNTER_BLOCK PerfCntrBlk; PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst + PerfInst->ByteLength); return((PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk + PerfCntrBlk->ByteLength)); } PPERF_COUNTER_DEFINITION FirstCounter(PPERF_OBJECT_TYPE PerfObj) { return((PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj + PerfObj->HeaderLength)); } PPERF_COUNTER_DEFINITION NextCounter(PPERF_COUNTER_DEFINITION PerfCntr) { return((PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr + PerfCntr->ByteLength)); } // Load the counter and object names from the registry to the // global variable lpNamesArray. BOOL GetNameStrings() { HKEY hKeyPerflib; // handle to registry key HKEY hKeyPerflib009; // handle to registry key DWORD dwMaxValueLen; // maximum size of key values DWORD dwBuffer; // bytes to allocate for buffers DWORD dwBufferSize; // size of dwBuffer LPSTR lpCurrentString; // pointer for enumerating data strings DWORD dwCounter; // current counter index // Get the number of Counter items. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib", 0, KEY_READ, &hKeyPerflib) != ERROR_SUCCESS) return FALSE; else printf("RegOpenKeyEx() is OK.\n"); dwBufferSize = sizeof(dwBuffer); if(RegQueryValueEx( hKeyPerflib, "Last Counter", NULL, NULL, (LPBYTE) &dwBuffer, &dwBufferSize) != ERROR_SUCCESS) return FALSE; else printf("RegQueryValueEx() is OK.\n"); RegCloseKey(hKeyPerflib); // Allocate memory for the names array. lpNamesArray = (LPTSTR *)malloc((dwBuffer+1) * sizeof(LPSTR)); if(lpNamesArray == NULL) return FALSE; // Open the key containing the counter and object names. if(RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009", 0, KEY_READ, &hKeyPerflib009) != ERROR_SUCCESS) return FALSE; else printf("RegOpenKeyEx() is OK.\n"); // Get the size of the largest value in the key (Counter or Help). if(RegQueryInfoKey( hKeyPerflib009, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueLen, NULL, NULL) != ERROR_SUCCESS ) return FALSE; else printf("RegQueryInfoKey() is OK.\n"); // Allocate memory for the counter and object names. dwBuffer = dwMaxValueLen + 1; lpNameStrings = (LPTSTR)malloc(dwBuffer * sizeof(CHAR)); if(lpNameStrings == NULL) { free(lpNamesArray); return FALSE; } else printf("Memory allocated for lpNameStrings.\n"); // Read the Counter value. if(RegQueryValueEx( hKeyPerflib009, "Counter", NULL, NULL, (LPBYTE)lpNameStrings, &dwBuffer) != ERROR_SUCCESS) return FALSE; else printf("RegQueryValueEx() is OK.\n"); printf("Please wait...\n"); // Load names into an array, by index. for(lpCurrentString = lpNameStrings; *lpCurrentString; lpCurrentString += (lstrlen(lpCurrentString)+1)) { dwCounter = atol(lpCurrentString); lpCurrentString += (lstrlen(lpCurrentString)+1); lpNamesArray[dwCounter] = (LPSTR)lpCurrentString; } return TRUE; } // Display the indexes and/or names for all performance // objects, instances, and counters. int main() { PPERF_DATA_BLOCK PerfData = NULL; PPERF_OBJECT_TYPE PerfObj; PPERF_INSTANCE_DEFINITION PerfInst; PPERF_COUNTER_DEFINITION PerfCntr, CurCntr; PPERF_COUNTER_BLOCK PtrToCntr; DWORD BufferSize = TOTALBYTES; DWORD i, j; LONG k; // Get the name strings through the registry. if(!GetNameStrings()) return FALSE; // Allocate the buffer for the performance data. PerfData = (PPERF_DATA_BLOCK) malloc(BufferSize); if(PerfData == NULL) return FALSE; while(RegQueryValueEx( HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, (LPBYTE) PerfData, &BufferSize) == ERROR_MORE_DATA) { // Get a buffer that is big enough. BufferSize += BYTEINCREMENT; PerfData = (PPERF_DATA_BLOCK) realloc(PerfData, BufferSize); } // Get the first object type. PerfObj = FirstObject(PerfData); // Process all objects. for(i=0; i < PerfData->NumObjectTypes; i++) { // Display the object by index and name. printf("\nObject %ld: %s\n", PerfObj->ObjectNameTitleIndex, lpNamesArray[PerfObj->ObjectNameTitleIndex]); // Get the first counter. PerfCntr = FirstCounter(PerfObj); if(PerfObj->NumInstances > 0) { // Get the first instance. PerfInst = FirstInstance(PerfObj); // Retrieve all instances. for(k=0; k < PerfObj->NumInstances; k++) { // Display the instance by name. printf("\n\tInstance %S: \n", (char *)((PBYTE)PerfInst + PerfInst->NameOffset)); CurCntr = PerfCntr; // Retrieve all counters. for(j=0; j < PerfObj->NumCounters; j++) { // Display the counter by index and name. printf("\t\tCounter %ld: %s\n", CurCntr->CounterNameTitleIndex, lpNamesArray[CurCntr->CounterNameTitleIndex]); // Get the next counter. CurCntr = NextCounter(CurCntr); } // Get the next instance. PerfInst = NextInstance(PerfInst); } } else { // Get the counter block. PtrToCntr = (PPERF_COUNTER_BLOCK)((PBYTE)PerfObj + PerfObj->DefinitionLength); // Retrieve all counters. for(j=0; j < PerfObj->NumCounters; j++) { // Display the counter by index and name. printf("\tCounter %ld: %s\n", PerfCntr->CounterNameTitleIndex, lpNamesArray[PerfCntr->CounterNameTitleIndex]); // Get the next counter. PerfCntr = NextCounter(PerfCntr); } } // Get the next object type. PerfObj = NextObject(PerfObj); } // Release all the memory back to system... free(lpNamesArray); free(lpNameStrings); free(PerfData); return TRUE; } ====================================================================================================================