Mega Code Archive

 
Categories / Delphi / ADO Database
 

Another way to store a DWORD into the Registry database

Title: Another way to store a DWORD into the Registry database Question: An example of how to use RegQueryValueEx to write a DWORD into the Registry database. Answer: use Registry; ... function ReadDWORD(vKey, vName: String): DWORD; var iType: DWORD; iSize: DWORD; iResult: Array [0..3] of Byte; hkResult: HKEY; begin Result:=0; iType:=REG_DWORD; iSize:=4; if RegOpenKeyEx(HKEY_LOCAL_MACHINE,LPTSTR(vKey), 0, KEY_READ, hkResult)ERROR_SUCCESS then Exit; if RegQueryValueEx(hkResult,LPTSTR(vName),Nil,@iType,@iResult,@iSize)=ERROR_SUCCESS then begin Result:=iResult[0]+(iResult[1] SHL 8)+(iResult[2] SHL 16)+(iResult[3] SHL 24); end; end; function WriteDWORD(vKey, vName: String; iValue: DWORD): Boolean; var iType: DWORD; iSize: DWORD; hkResult: HKEY; ibValue: Array [0..3] of Byte; begin Result:=False; if RegOpenKeyEx(HKEY_LOCAL_MACHINE,LPTSTR(vKey), 0, KEY_WRITE, hkResult)ERROR_SUCCESS then Exit; iType:=REG_DWORD; iSize:=4; ibValue[0]:=(iValue AND $000000FF); ibValue[1]:=(iValue AND $0000FF00) SHR 8; ibValue[2]:=(iValue AND $00FF0000) SHR 16; ibValue[3]:=(iValue AND $FF000000) SHR 24; if RegSetValueEx(hkResult,LPTSTR(vName),0,iType,@ibValue[0],iSize)=ERROR_SUCCESS then Result:=True; end;