Mega Code Archive

 
Categories / Delphi / System
 

TRegistry has a weird behaviour within Windows 2000

Title: TRegistry has a weird behaviour within Windows 2000 Question: If you log in Windows 2000 as a Power User, you have to pay attention to a small "bug" (?) in TRegistry Answer: I'm still sorry for my english ;-) I discovered a weird behaviour of the TRegistry object when I'm developing software on Windows 2000. Follow these steps: 1) If you are the Administrator of the Windows 2000 system, create a new user as "Power user" (or "Standard user"). 2) Log out and log in again as that power user. Probably you have to re-install Delphi, so I suggest to use the "Registry" setting during the setup, so it will create only the required links for Delphi (notice: you may also have to re-install every component). 3) Let's suppose you did the previous two steps: launch Delphi and place a Button on the form that appears after startup, and double click on the button. 4) Copy the following code and paste into the unit: //------ Beginning of the Code ------------ unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Registry, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var Reg: TRegistry begin Reg:=TRegistry.Create; with Reg do begin RootKey:=HKEY_CLASSES_ROOT; OpenKey('\TestKey',true); // (1) WriteString('Test String','Test Value'); CloseKey; end; Reg.Free; end; end. //------ End of the Code ------------ When you are developing within Windows 98 the syntax of the line that I marked with (1) is correct: it means "Open the registry key 'TestKey' - if it doesn't exists then create it, else just open it". In Windows 98 this code works, but in Windows 2000 (and a Power User logged in) you will get a "failed set data for..." error. This error means that something went wrong. Let's go back to the code and replace only the line (1) with the following code: //------ Beginning of the Code ------------ if KeyExists('\TestKey') then OpenKey('\TestKey',false) else OpenKey('\TestKey',true); //------ End of the Code ------------ Okay, I know: it's really a weird thing but this seems to be the only way to avoid that error. What should I say? This little snippet does the job that the OpenKey function should do (as it does in Windows 98). Bye.