Showing posts with label registry. Show all posts
Showing posts with label registry. Show all posts

This code demonstrates use of windows registery and function overloading to read from registery:
if key doesnt exist it's created and Default passed to key value ReadReg(KeyToRead,DefaultValue)
to write to registery:
writereg(KeyToWrite,ValueToKey)
Supports integer,string and boolean types, other type can be similary added.}

unit module1;

interface

uses Windows, registry;

function WriteReg(Key:string;value:integer) :boolean; overload;
function WriteReg(Key:string;value:boolean) :boolean; overload;
function WriteReg(Key:string;value:String) :boolean; overload;
function ReadReg(Key:string;default:String='') :string; overload;
function ReadReg(Key:string;default:integer=0) :integer; overload;
function ReadReg(Key:string;default:boolean=false):boolean;overload;

const
ApplicationName:string ='MyProgram'; //Your Programname

implementation

function ReadReg(Key:string;default:integer=0):integer;
var
Registry: TRegistry;
begin
Registry :=TRegistry.Create;
Registry.RootKey :=HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ ApplicationName,True);
if registry.ValueExists(key) =true then
result:=Registry.readinteger(key)
else
begin
Registry.Writeinteger(key,default);
result:=Registry.readinteger(key);
end;
Registry.Free;
end;

function ReadReg(Key:string;default:string=''):string;
var
Registry : TRegistry;
begin
Registry := TRegistry.Create;
Registry.RootKey :=HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ApplicationName,True);
if registry.ValueExists(key) then
readreg := Registry.readstring(key)
else
begin
Registry.Writestring(key,default);
Readreg := Registry.readstring(key);
end;
Registry.Free;
end;


function ReadReg(Key:string;default:boolean=false):boolean;
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ApplicationName,True);
if registry.ValueExists(key) then
readreg:=Registry.readbool(key)
else
begin
Registry.Writebool(key,default);
readreg:=Registry.readbool(key);
end;
Registry.Free;
end;

function WriteReg(Key:string;value:boolean):boolean;
var Registry: TRegistry;
begin
Registry:=TRegistry.Create;
Registry.RootKey:=HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ApplicationName,True);
registry.WriteBool(key,value);
result:= registry.KeyExists(key);
Registry.Free;
end;

function WriteReg(Key:string;value:string):boolean;
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ApplicationName,True);
registry.Writestring(key,value);
result:= registry.KeyExists(key);
Registry.Free;
end;

function WriteReg(Key:string;value:integer):boolean;
var
Registry: TRegistry;
begin
Registry := TRegistry.Create;
Registry.RootKey := HKEY_CURRENT_USER;
Registry.OpenKey('\software\'+ApplicationName,True);
registry.Writeinteger(key,value);
writereg:= registry.KeyExists(key);
Registry.Free;
end;

Compare dates

Uses SysUtils;
...
if(Date < EncodeDate( 2000, 1, 1 )) then
Showmessage(' The date is ' + DateToStr(Date));
…

You may have noticed that using SystemParametersInfo to change the wallpaper when ActiveDesktop is turned on doesn't work. The reason is because you need to use the IActiveDesktop COM interface. Using SystemParametersInfo still works, but it doesn't update the wallpaper.

Note that the IActiveDesktop interface requires a Shell32.dll version >= 4.71. The document titled "Get a File Version" demonstrates how to check the file version and uses the shell32.dll as an example.

Here is an example of using IActiveDesktop to work with
the wallpaper. It assumes that you have 3 labels on a form
and two buttons with the default names.

uses
ComObj, // For CreateComObject and Initialization/Finalization of COM
ShlObj; // For IActiveDesktop

{ The CLASS ID for ActiveDesktop is not defined in ShlObj, while the IID is so we define it here. }
const
CLSID_ActiveDesktop : TGUID = '{75048700-EF1F11D0-9888-06097DEACF9}';

{ Demonstrate getting the Wallpaper }
procedure TForm1.Button1Click(Sender: TObject);
var
ActiveDesktop : IActiveDesktop;
CurrentWallpaper : string;
CurrentPattern : string;
WallpaperOptions : TWallpaperOpt;
TmpBuffer : PWideChar;
begin
// Create the ActiveDesktop COM Object
ActiveDesktop:=CreateComObject(CLSID_ActiveDesktop) as IActiveDesktop;
// We now need to allocate some memory to get the current Wallpaper.
// However, tmpBuffer is a PWideChar which means 2 bytes make
// up one Char. In order to compenstate for the WideChar, we
// allocate enough memory for MAX_PATH*2
tmpBuffer := AllocMem(MAX_PATH*2);
try
ActiveDesktop.GetWallpaper(tmpBuffer, MAX_PATH*2, 0);
CurrentWallpaper := tmpBuffer;
finally
FreeMem(tmpBuffer);
end;
if CurrentWallpaper <> '' then
Label1.Caption := 'Current Wallpaper: ' + CurrentWallpaper
else
Label1.Caption := 'No Wallpaper set';

// Now get the current Wallpaper options.
// The second parameter is reserved and must be 0.
WallpaperOptions.dwSize := SizeOf(WallpaperOptions);
ActiveDesktop.GetWallpaperOptions(WallpaperOptions, 0);
case WallpaperOptions.dwStyle of
WPSTYLE_CENTER : Label2.Caption := 'Centered';
WPSTYLE_TILE : Label2.Caption := 'Tiled';
WPSTYLE_STRETCH : Label2.Caption := 'Stretched';
WPSTYLE_MAX : Label2.Caption := 'Maxed';
end;
{Now get the desktop pattern. The pattern is a string of decimals whose bit pattern represents a picture. Each decimal represents the on/off state of the 8 pixels in that row. }
tmpBuffer := AllocMem(256);
try
ActiveDesktop.GetPattern(tmpBuffer, 256, 0);
CurrentPattern := tmpBuffer;
finally
FreeMem(tmpBuffer);
end;
if CurrentPattern <> '' then
Label3.Caption := CurrentPattern
else
Label3.Caption := 'No Pattern set';
end;
{ Demonstrate setting the wallpaper }
procedure TForm1.Button2Click(Sender: TObject);
var
ActiveDesktop: IActiveDesktop;
begin
ActiveDesktop := CreateComObject(CLSID_ActiveDesktop) as IActiveDesktop;
ActiveDesktop.SetWallpaper('c:\downloads\images\test.bmp', 0);
ActiveDesktop.ApplyChanges(AD_APPLY_ALL or AD_APPLY_FORCE);
end;

Responds:
Good programming. But I just calling

SystemParametersInfo( SPI_SETDESKWALLPAPER, 0, @WallpaperBMPFileAndPath[1], 3);

Question:
For evaluation, I would like to have application that shows the list of installed software in my computer. Can you give me?

Answer:
Yup, since those data keep inside registry, we cant avoid to use registry unit.
Here the codes:

uses
Registry;


procedure TForm1.Button1Click(Sender: TObject);
const
UNINST_PATH = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
var
Reg: TRegistry;
SubKeys: TStringList;
ListItem: TlistItem;
i: integer;
sDisplayName, sUninstallString: string;
begin
{
ListView1.ViewStyle := vsReport;
ListView1.Columns.add;
ListView1.Columns.add;
ListView1.Columns[0].caption := 'DisplayName';
ListView1.Columns[1].caption := 'UninstallString';
ListView1.Columns[0].Width := 300;
ListView1.Columns[1].Width := 300;
}
Reg := TRegistry.Create;
with Reg do
try
with ListView1.Items do
try
BeginUpdate;
Clear;
RootKey := HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly(UNINST_PATH) then
begin
SubKeys := TStringList.Create;
try
GetKeyNames(SubKeys);
CloseKey;
for i := 0 to subKeys.Count - 1 do
if OpenKeyReadOnly(Format('%s\%s', [UNINST_PATH, SubKeys[i]])) then
try
sDisplayName := ReadString('DisplayName');
sUninstallString := ReadString('UninstallString');
if sDisplayName <> '' then
begin
ListItem := Add;
ListItem.Caption := sDisplayName;
ListItem.subitems.Add(sUninstallString);
end;
finally
CloseKey;
end;
finally
SubKeys.Free;
end;
end;
finally
ListView1.AlphaSort;
EndUpdate;
end;
finally
CloseKey;
Free;
end;
end;

Question:
It will be nice if I can associate an application to a file extension. I know, windows already has it, but I wish to have more simple and faster application. How?

Answer:
This can only be done with work around in registry,
thanks God, and thanks to borland that delphi has uses to complete this work done.
Here the example.

uses
registry, shlobj;

procedure TForm1.RegisterFileType(prefix: string; exepfad: string);
var
reg: TRegistry;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_CLASSES_ROOT;
reg.OpenKey('.' + prefix, True);
try
reg.Writestring('', prefix + 'file');
finally
reg.CloseKey;
end;
reg.CreateKey(prefix + 'file');
reg.OpenKey(prefix + 'file\DefaultIcon', True);
try
reg.Writestring('', exepfad + ',0');
finally
reg.CloseKey;
end;
reg.OpenKey(prefix + 'file\shell\open\command', True);
try
reg.Writestring('', exepfad + ' "%1"');
finally
reg.CloseKey;
end;
finally
reg.Free;
end;
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterFileType('pci', 'c:\file.exe');
end;

CLICK TO REGISTER