Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279////////////////////////////////////////////////////////////////////// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine// All rights reserved// This file was released under the GPLv2 on June 2015.////////////////////////////////////////////////////////////////////
#include "regtools.h"
#ifndef WIN_32_MODE
NTSTATUSRegTGetKeyHandle( IN HKEY hRootKey, IN PCWSTR KeyName, OUT HKEY* hKey ){ OBJECT_ATTRIBUTES ObjectAttributes; UNICODE_STRING NameString; NTSTATUS status;
//UDFPrint(("RegTGetKeyHandle: h=%x, %S\n", hRootKey, KeyName));
RtlInitUnicodeString(&NameString, KeyName);
InitializeObjectAttributes( &ObjectAttributes, &NameString, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, hRootKey, NULL );
status = ZwOpenKey( hKey, KEY_WRITE | KEY_READ, &ObjectAttributes );
if(!NT_SUCCESS(status)) { //UDFPrint((" status %x\n", status)); *hKey = NULL; }
return status;} // end RegTGetKeyHandle()
VOIDRegTCloseKeyHandle( IN HKEY hKey){ ZwClose(hKey);} // end RegTCloseKeyHandle()
#else //WIN_32_MODE
NTSTATUSRegTGetKeyHandle( IN HKEY hRootKey, IN PWCHAR KeyName, OUT HKEY* hKey ){ LONG status;
if(!hRootKey) hRootKey = HKEY_LOCAL_MACHINE;
status = RegOpenKeyExW( hRootKey, KeyName, 0, KEY_WRITE | KEY_READ, hKey );
if(status != ERROR_SUCCESS) { *hKey = NULL; }
return status;} // end RegTGetKeyHandle()
VOIDRegTCloseKeyHandle( IN HKEY hKey){ if(!hKey) { return; } RegCloseKey(hKey);} // end RegTCloseKeyHandle()
#endif //WIN_32_MODE
BOOLEANRegTGetDwordValue( IN HKEY hRootKey, IN PCWSTR RegistryPath, IN PCWSTR Name, IN PULONG pUlong ){#ifndef WIN_32_MODE UNICODE_STRING NameString; PKEY_VALUE_PARTIAL_INFORMATION ValInfo;#endif //WIN_32_MODE ULONG len; NTSTATUS status; HKEY hKey; BOOLEAN retval = FALSE; BOOLEAN free_h = FALSE;
#ifdef WIN_32_MODE if(!hRootKey) hRootKey = HKEY_LOCAL_MACHINE;#endif //WIN_32_MODE
if(RegistryPath && RegistryPath[0]) { status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);#ifdef WIN_32_MODE if(status != ERROR_SUCCESS)#else //WIN_32_MODE if(!NT_SUCCESS(status))#endif //WIN_32_MODE return FALSE; free_h = TRUE; } else { hKey = hRootKey; } if(!hKey) return FALSE;
#ifndef WIN_32_MODE/* UDFPrint(("h=%x|%S, %S (%x)\n", hRootKey, RegistryPath, Name, *pUlong));*/ len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG) + 0x20; ValInfo = (PKEY_VALUE_PARTIAL_INFORMATION) MyAllocatePool__(NonPagedPool, len); if(!ValInfo) { if(free_h) { RegTCloseKeyHandle(hKey); } return FALSE; }
RtlInitUnicodeString(&NameString, Name);
status = ZwQueryValueKey(hKey, &NameString, KeyValuePartialInformation, ValInfo, len, &len); if(NT_SUCCESS(status) && ValInfo->DataLength == sizeof(ULONG)) { RtlCopyMemory(pUlong, ValInfo->Data, sizeof(ULONG)); retval = TRUE; //UDFPrint((" -> %x\n",*pUlong)); } else { //UDFPrint((" err %x\n",status)); }
MyFreePool__(ValInfo);#else //WIN_32_MODE len = sizeof(ULONG); if (ERROR_SUCCESS == RegQueryValueExW( hKey, // handle of key to query Name, // address of name of value to query 0, // reserved NULL, // address of buffer for value type (BYTE *)pUlong, // address of data buffer &len // address of data buffer size ) && len == sizeof(ULONG)) { retval = TRUE; }#endif //WIN_32_MODE if(free_h) { RegTCloseKeyHandle(hKey); } return retval;} // end RegTGetDwordValue()
BOOLEANRegTGetStringValue( IN HKEY hRootKey, IN PCWSTR RegistryPath, IN PCWSTR Name, IN PWCHAR pStr, IN ULONG MaxLen ){#ifndef WIN_32_MODE UNICODE_STRING NameString; PKEY_VALUE_PARTIAL_INFORMATION ValInfo;#endif //USER_MODE ULONG len; NTSTATUS status; HKEY hKey; BOOLEAN retval = FALSE; BOOLEAN free_h = FALSE;
#ifdef WIN_32_MODE if(!hRootKey) hRootKey = HKEY_LOCAL_MACHINE;#endif //WIN_32_MODE
if(RegistryPath && RegistryPath[0]) { status = RegTGetKeyHandle(hRootKey, RegistryPath, &hKey);#ifdef WIN_32_MODE if(status != ERROR_SUCCESS)#else //WIN_32_MODE if(!NT_SUCCESS(status))#endif //WIN_32_MODE return FALSE; free_h = TRUE; } else { hKey = hRootKey; } if(!hKey) return FALSE;
pStr[0] = 0;#ifndef WIN_32_MODE len = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + MaxLen + 0x20; ValInfo = (PKEY_VALUE_PARTIAL_INFORMATION) MyAllocatePool__(NonPagedPool, len); if(!ValInfo) { if(free_h) { RegTCloseKeyHandle(hKey); } return FALSE; }
RtlInitUnicodeString(&NameString, Name);
status = ZwQueryValueKey(hKey, &NameString, KeyValuePartialInformation, ValInfo, len, &len); if(NT_SUCCESS(status) && ValInfo->DataLength) { RtlCopyMemory(pStr, ValInfo->Data, min(ValInfo->DataLength, MaxLen) ); if(pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1]) { pStr[(ValInfo->DataLength)/sizeof(WCHAR)-1] = 0; } retval = TRUE; }
MyFreePool__(ValInfo);#else //WIN_32_MODE len = MaxLen; if (ERROR_SUCCESS == RegQueryValueExW( hKey, // handle of key to query Name, // address of name of value to query 0, // reserved NULL, // address of buffer for value type (BYTE *)pStr, // address of data buffer &len // address of data buffer size ) && len) { if(pStr[len-1]) { pStr[len-1] = 0; } retval = TRUE; }#endif //WIN_32_MODE
if(free_h) { RegTCloseKeyHandle(hKey); } return retval;} // end RegTGetStringValue()