Expermicid Posted October 1, 2012 Share Posted October 1, 2012 (edited) This is the hidden content, please Sign In or Sign Up [lenguaje=delphi]//****************************************************************************** //* UNIT: UNT_RegManager //* AUTOR: Fakedo0r //* FECHA: 15.08.2012 //* CORREO: [email protected] //* BLOG: Sub-Soul.blogspot.com / Sub-Soul.com //****************************************************************************** Unit UNT_RegManager; //****************************************************************************** // DECLARACION LIBRERIAS / CLASES //****************************************************************************** Interface Uses Winapi.Windows, System.SysUtils, System.Classes; //****************************************************************************** // DECLARACION DE FUNCIONES / PROCEDIMIENTOS //****************************************************************************** Function GetRegType(wType: WORD): String; Function EnumRegKeysAPI(hRoot: HKEY; sPath: String): TStringList; Function EnumRegValuesAPI(hRoot: HKEY; sPath: String; sDel: String) : TStringList; Function CreateKeyAPI(hRoot: HKEY; sPath: String; IsReg64: BOOL): BOOL; Function DeleteKeyAPI(hRoot: HKEY; sPath: String; IsReg64: BOOL): BOOL; Function GetKeyValueAPI(hRoot: HKEY; sPath: String; sKeyName: String; wKeyType: DWORD): String; Function WriteRegKeyValueAPI(hRoot: HKEY; sPath: String; sKeyName: String; sValue: String; wType: WORD): BOOL; Function StringToHex(sCadena: String): String; Function ByteArrayToString(bByteArray: Array Of Byte): String; //****************************************************************************** Implementation //****************************************************************************** // <--- CREA LA CLAVE ---> //****************************************************************************** Function CreateKeyAPI(hRoot: HKEY; sPath: String; IsReg64: BOOL): BOOL; Var hRes: HKEY; iRet: Integer; Begin If IsReg64 Then iRet := RegCreateKeyEx(hRoot, PChar(sPath), 0, Nil, REG_OPTION_NON_VOLATILE, KEY_WRITE Or KEY_WOW64_64KEY, Nil, hRes, PDWORD(Nil)) Else iRet := RegCreateKeyEx(hRoot, PChar(sPath), 0, Nil, REG_OPTION_NON_VOLATILE, KEY_WRITE Or KEY_WOW64_32KEY, Nil, hRes, PDWORD(Nil)); RegCloseKey(hRes); If iRet = ERROR_SUCCESS Then Result := True Else Result := False; End; //****************************************************************************** // <--- ELIMINA LA CLAVE ---> //****************************************************************************** Function DeleteKeyAPI(hRoot: HKEY; sPath: String; IsReg64: BOOL): BOOL; Var iRet: Integer; Begin If IsReg64 Then iRet := RegDeleteKeyEx(hRoot, PChar(sPath), KEY_WOW64_64KEY, 0) Else iRet := RegDeleteKeyEx(hRoot, PChar(sPath), KEY_WOW64_32KEY, 0); If iRet = ERROR_SUCCESS Then Result := True Else Result := False; End; //****************************************************************************** // <--- LEE EL VALOR DE LA CLAVE ---> //****************************************************************************** Function GetKeyValueAPI(hRoot: HKEY; sPath: String; sKeyName: String; wKeyType: DWORD): String; Var hOpenKey: HKEY; sData: String; iData: Integer; iSize: Integer; bData: Array Of Byte; Begin Result := ''; If RegOpenKeyEx(hRoot, PChar(sPath), 0, KEY_READ, hOpenKey) <> ERROR_SUCCESS Then Exit; Case wKeyType Of 1: Begin RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, Nil, @iSize); SetLength(sData, iSize); RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, @sData[1], @iSize); sData := String(sData); End; 2: Begin SetLength(sData, iSize); RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, @sData[1], @iSize); sData := String(sData); End; 3: Begin RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, Nil, @iSize); SetLength(bData, iSize); RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, @bData[0], @iSize); sData := LowerCase(StringToHex(ByteArrayToString(bData))); End; 4: Begin RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, @iData, @iSize); sData := IntToStr(iData); End; 7: Begin RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, Nil, @iSize); SetLength(bData, iSize); RegQueryValueEx(hOpenKey, PChar(sKeyName), Nil, @wKeyType, @bData[0], @iSize); sData := String(sData); End; End; RegCloseKey(hOpenKey); Result := sData; End; //****************************************************************************** // <--- ESCRIBE LA CLAVE ---> //****************************************************************************** Function WriteRegKeyValueAPI(hRoot: HKEY; sPath: String; sKeyName: String; sValue: String; wType: WORD): BOOL; Var hOpenKey: HKEY; iValue: Integer; Begin Result := False; If RegOpenKeyEx(hRoot, PChar(sPath), 0, KEY_WRITE, hOpenKey) <> ERROR_SUCCESS Then Exit; Case wType Of 1: Begin If RegSetValueEx(hOpenKey, PChar(sKeyName), 0, REG_SZ, PChar(sValue + #0), (Length(sValue) + 1) * 2) <> ERROR_SUCCESS Then Exit; End; 2: Begin If RegSetValueEx(hOpenKey, PChar(sKeyName), 0, REG_SZ, PChar(sValue + #0), (Length(sValue) + 1) * 2) <> ERROR_SUCCESS Then Exit; End; 4: Begin iValue := StrToInt(sValue); If RegSetValueEx(hOpenKey, PChar(sKeyName), 0, REG_DWORD, @iValue, 4) <> ERROR_SUCCESS Then Exit; End; 7: Begin If RegSetValueEx(hOpenKey, PChar(sKeyName), 0, REG_MULTI_SZ, PChar(sValue + #0#0), (Length(sValue) + 2) * 2) <> ERROR_SUCCESS Then Exit; End; End; RegCloseKey(hOpenKey); Result := True; End; //****************************************************************************** // <--- ENUMERA LAS CLAVES ---> //****************************************************************************** Function EnumRegKeysAPI(hRoot: HKEY; sPath: String): TStringList; Var hOpenKey: HKEY; dwIndex: DWORD; dwBuffSize: DWORD; cBuff: Array [0 .. MAX_PATH] Of Char; Begin Result := TStringList.Create; dwIndex := 0; dwBuffSize := MAX_PATH; If RegOpenKeyEx(hRoot, PChar(sPath), 0, KEY_READ, hOpenKey) <> ERROR_SUCCESS Then Exit; While RegEnumKeyEx(hOpenKey, dwIndex, cBuff, dwBuffSize, Nil, Nil, Nil, Nil) = ERROR_SUCCESS Do Begin Result.Add(String(cBuff)); dwBuffSize := MAX_PATH; Inc(dwIndex); End; RegCloseKey(hOpenKey); End; //****************************************************************************** // <--- ENUMERA LOS VALORES DE LAS CLAVES ---> //****************************************************************************** Function EnumRegValuesAPI(hRoot: HKEY; sPath: String; sDel: String) : TStringList; Var hOpenKey: HKEY; dwIndex: DWORD; dwBuffSize: DWORD; dwType: DWORD; dwDataSize: DWORD; cBuff: Array [0 .. MAX_PATH] Of Char; Begin Result := TStringList.Create; dwBuffSize := MAX_PATH; dwIndex := 0; If RegOpenKeyEx(hRoot, PChar(sPath), 0, KEY_READ, hOpenKey) <> ERROR_SUCCESS Then Exit; While RegEnumValue(hOpenKey, dwIndex, cBuff, dwBuffSize, Nil, @dwType, Nil, @dwDataSize) <> ERROR_NO_MORE_ITEMS Do Begin Result.Add(String(cBuff) + sDel + GetRegType(dwType) + sDel + GetKeyValueAPI(hRoot, sPath, String(cBuff), dwType)); dwBuffSize := MAX_PATH; Inc(dwIndex); End; RegCloseKey(hOpenKey); End; //****************************************************************************** // <--- OBTIENE EL TIPO DEL VALOR ---> //****************************************************************************** Function GetRegType(wType: WORD): String; Var sTemp: String; Begin Case wType Of 0: sTemp := 'REG_NONE'; 1: sTemp := 'REG_SZ'; 2: sTemp := 'REG_EXPAND_SZ'; 3: sTemp := 'REG_BINARY'; 4: sTemp := 'REG_DWORD'; 5: sTemp := 'REG_DWORD_BIG_ENDIAN'; 6: sTemp := 'REG_LINK'; 7: sTemp := 'REG_MULTI_SZ'; 8: sTemp := 'REG_RESOURCE_LIST'; 9: sTemp := 'REG_FULL_RESOURCE_DESCRIPTOR'; 10: sTemp := 'REG_RESOURCE_REQUIREMENTS_LIST'; 11: sTemp := 'REG_QWORD'; Else sTemp := 'UNKNOWN REG TYPE'; End; Result := sTemp; End; //****************************************************************************** // <--- CONVERSION DE STRING A HEX ---> //****************************************************************************** Function StringToHex(sCadena: String): String; Var I: Integer; Begin Result := ''; For I := 1 To Length(sCadena) Do Result := Result + IntToHex(Ord(sCadena), 2) + ' '; End; //****************************************************************************** // <--- CONVERSION DE BYTE ARRAY A STRING ---> //****************************************************************************** Function ByteArrayToString(bByteArray: Array Of Byte): String; Var I: Integer; Begin SetLength(Result, Length(bByteArray)); For I := 0 To High(bByteArray) Do Result[i + 1] := Char(bByteArray); End; End.[/lenguaje] Edited February 16, 2014 by Expermicid Link to comment Share on other sites More sharing options...
Recommended Posts