You may, for some reasons not want to use Delphi’s TRegistry component but still want to use the system’s Registry. This may come in handy when creating applications for Windows NT/2000/XP/Vista. Delphi’s TRegistry component will raise an error when attempting to write a string value to the HKEY_LOCAL_MACHINE key. Well, the way to get over this is to call the Windows API directly. This is a little harder than using the TRegistry component, but Delphi users that have some C/C++ experience shouldn’t have any problems. If you like Object Pascal’s standard data types you’re going to hate this
Although this may look like a lot of work it’s really fun. When you’re program saves/reads a lot of data to/from the Registry (like Option dialogs) this may dramatically shorten the time needed for these operations.
This example will not work on Windows 3.x with Win32s installed because the API functions used here are native Win32 functions. Check up the function parameters in the Win32 help.
The API functions we will need:
- RegCreateKeyEx() or RegOpenKeyEx()
- RegCloseKeyE()
- RegSetValueEx()
uses
Windows;
...
var
hOpenKey: HKEY;
iI, iSize, iType: Integer;
pcTemp: PChar;
pdI: PDWORD;
...
(* This example creates and opens the key HKEY_CURRENT_USERSoftwarem3RlinDelphi FAQ
** and saves data in it. If you don't want to create a key but just open an
** existing one use the RegOpenKeyEx() function.
*)
pdI := nil;
if RegCreateKeyEx(HKEY_CURRENT_USER, 'Software\\m3Rlin\\Delphi FAQ', 0, nil,
REG_OPTION_NON_VOLATILE, KEY_WRITE, nil, hOpenKey, pdI) = ERROR_SUCCESS then begin
// Saving a Boolean value
iI := Integer(checkboxMain.Checked);
RegSetValueEx(hOpenKey, 'Boolean Value', 0, REG_DWORD, @iI, SizeOf(iI));
// Saving a Integer value
iI := Integer(MainForm.Height);
RegSetValueEx(hOpenKey, 'Integer Value', 0, REG_DWORD, @iI, SizeOf(iI));
// Saving a string value
RegSetValueEx(hOpenKey, 'String Value', 0, REG_SZ, PChar(MainForm.Caption),
Length(MainForm.Caption) + 1); // The 1 is for the terminating 0 (PChar)
RegCloseKey(hOpenKey); // Close the open registry key
end;
(* This example opens the key HKEY_CURRENT_USERSoftwarem3RlinDelphi FAQ and reads
** data from it.
*)
pdI := nil;
if RegOpenKeyEx(HKEY_CURRENT_USER, 'Software\m3Rlin\Delphi FAQ', 0, KEY_READ,
hOpenKey) = ERROR_SUCCESS then begin
// Reading an Integer value
iType := REG_DWORD; // Type of data that is going to be read
iSize := SizeOf(Integer); // Buffer for the value to read
if RegQueryValueEx(hOpenKey, 'Integer Value', nil, @iType, @iI, @iSize) = ERROR_SUCCESS then
MainForm.Height := iI;
// Reading a string value
if RegQueryValueEx(hOpenKey, 'String Value', nil, @iType, pcTemp, @iSize) = ERROR_SUCCESS then
MainForm.Caption := string(pcTemp);
RegCloseKey(hOpenKey);
end;
...
Tags: access, api, api functions, boolean value, code, Delphi, delphi faq, free, function, hkey local machine, hyve, integer value, machine key, native win32, nil reg, object pascal, open source, read, registry, regopenkeyex function, rejestr, routine, source, source code, string value, tip, trick, win32 functions, windows, write
Merlin’s Delphi Forge
September 4, 2007 at 01:01
In my project, I use a TimeDatePicker which displays the shortdate asp per the local settings.
Although I use
ShortDateFormat:=’yyyy.mm.dd’;
DateSeparator:=’.';
The picker ignores this.
My program must use the ansi standard ‘yyyy.mm.dd’;
I test for this throughout my program but sometimes the date structure in the registry reverts to ‘mm/dd/yy/’ and buggers up.
I’m looking for some help in overwriting teh locale date structure i the registry to confirm the computer is running ansi.
My program is the main program in the offices I support, hense changinh the registry key in the local cid is OK.
I understand how to write to to a normal registry key, but don’t know how to path the cid.
Can you help me?