Before we even talk about Delphi programming, we'll get accustomed to Regedit, which is a tool that windows provides for searching and modifying the registry. Start up regedit either by going Start -> Run -> regedit, or going to your system32 folder and double clicking regedit.exe.
Each program installed on the computer leaves a little bit of information behind in order for it to properly load to your specifications. (Cheat Engine leaves behind quite a lot of registry keys, just btw) This is mainly stored in the Registry, which is what we're looking at in regedit. Much of the information on programs will be stored in the HKEY_LOCAL_MACHINE\SOFTWARE path. However, some will be stored in the HKEY_CURRENT_USER\SOFTWARE path (this includes Cheat Engine registry entries).
Each Registry key contains a little bit of information in the form of Strings (SZ), DWORD, and Binary. Strings, of course, store arrays of characters. DWORD is mainly used for Hexidecimal entries, but it can also be used for Decimal (base Ten) entries. Binary is used to store arrays of bytes, and that's all that matters for this tutorial.
Regedit works just like Windows Explorer, in list Details view. It shows the each Key's name, data type, and value. You can double click to change a value, but that is not recommended.
I was working with the registry in an attempt to be able to access the proper installation folder of windows no matter what platform I was on. Finding the windows file path will be the first example in this little tutorial. First, though, you need to know some delphi registry commands.
Working with the Registry in Delphi
As with any other addon to Delphi code, you'll need add something to your uses list. For this, just add registry to the list, like so:
Quote:
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Registry;
Now, as with any windows application, you'll need to click a button or something to start your procedure(s), so let's add a button to your main form.
Now, double click that button, and we can get to working with the Registry.
In order to use Registry keys, we'll need to declare a variable with variable type TRegistry. So in your procedure, add a var section and put something like this:
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
begin
end;
In order to implement a registry key, you need to .create it so let's put:
Reg := TRegistry.Create; right after the begin.
Be sure to put a Reg.Free; after you finish... (Create and Free go hand in hand...)
Then you need to assign a root path, or it assumes it to be HKEY_CURRENT_USER (which is not as good as HKEY_LOCAL_MACHINE). So we put: Reg.Rootkey:= HKEY_LOCAL_MACHINE;
For now, i'll just list out the commands that we'll use in this tutorial:
Bold = required; Italics = not required
TRegistry:
procedure OpenKey- Moves the focus onto the registry path specified.
Syntax: TRegistry.Openkey(path: string, CreateIfNotExistant: BOOLEAN);
The path is added onto the rootkey to form the whole path such as HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows where the blue is the rootkey, and the red is the path.
The CreateIfNotExistant boolean variable indicates whether or not to create the registry path if it does not exist. Typically, this would be FALSE.
function ReadString- Reads a string from the key specified. Returns the string;
Syntax: some variable (string) := ReadString(keyname: string,MaxNumOfChar: Integer);
Keyname is the name of the key to be read from.
MaxNumOfChar is the user-specified maximum number of characters to be read
These two functions are enough for this tutorial. If you want to fool around with the registry more, you'll want to refer to Delphi help.
Finding the Windows Installation Path:
For this part, you won't need regedit, as I will provide you the path to windows. We'll start off with a skeleton of the program:
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
WindowsFilePath: String;
begin
Reg:= TRegistry.Create;
Reg.Rootkey:= HKEY_LOCAL_MACHINE;
// Code goes here
Reg.Free;
end;
Now, we'll observe that windows' registry key is in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion. So let's openkey that.
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
WindowsFilePath: String;
begin
Reg:= TRegistry.Create;
Reg.Rootkey:= HKEY_LOCAL_MACHINE;
Reg.Openkey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion', FALSE);
// Code goes here
Reg.Free;
end;
This path exists, so the FALSE shouldn't matter.
Now, we'll notice that the key name is 'SystemRoot', so we'll read string from that key, and voila, we have the windows installation folder.
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
WindowsFilePath: String;
begin
Reg:= TRegistry.Create;
Reg.Rootkey:= HKEY_LOCAL_MACHINE;
Reg.Openkey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion', FALSE);
WindowsFilePath:= Reg.ReadString('SystemRoot');
Reg.Free;
end;
Ok, this does BASICALLY nothing right now, so we'll just have a little fun in the windows folder, shall we?
Nah, i'm just joking. We'll just open regedit, not matter what system we're on.
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
WindowsFilePath: String;
begin
Reg:= TRegistry.Create;
Reg.Rootkey:= HKEY_LOCAL_MACHINE;
Reg.Openkey('\SOFTWARE\Microsoft\Windows NT\CurrentVersion', FALSE);
WindowsFilePath:= Reg.ReadString('SystemRoot');
Reg.Free;
ShellExecute(handle,'',PChar(Concat(WindowsFilePat h,'\system32\regedit.exe')),'','',SW_SHOW);
//If you didn't know what that meant, don't worry about it
end;
Be sure to add shellapi to your uses list if you're going to use ShellExecute.
Getting Application's File Path from Registry
This step is a little harder, because different companies put different names on their registry keys, and they put them in a lot of different places. I'm assuming we're all using Borland Delphi 7, so we'll just use that as an example.
First, let's load up regedit and try to find a path to Borland.
First, we look in Local Machine, and voila, we find it under \software\Borland.
Now we'll find Delphi's keys.
We look in Borland, and we find Delphi, however there are no keys in that folder. However, when we look under 7.0, we find a key called 'RootDir'. Even the name gives it away, so let's keep that key.
We've arrived at...
Path: HKEY_LOCAL_MACHINE\SOFTWARE\Borland\Delphi\7.0
Keyname: RootDir
So let's make a program that looks for the Root Directory.
Quote:
Procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
DelphiPath: String;
begin
Reg:= TRegistry.Create;
Reg.Rootkey:= HKEY_LOCAL_MACHINE;
Reg.Openkey('\SOFTWARE\Borland\Delphi\7.0', FALSE);
DelphiPath:= Reg.ReadString('RootDir');
Reg.Free;
end;
That was simple enough, however we saw that the Keyname changed, so we have to be cautious.
Whenever you're looking for a directory key, look for giveaway names like 'RootDir' or 'InstDir' or something like that.
Now, to implement this into a button that loads the program, we'll have to look in that path to find where the executable is.
It just so happens that the delphi executable is not in the rootdir, but in a subfolder: \Bin\delphi32.exe
So we'll put that into one giant string by using concatenation (Concat();)
DelphiPath:= Concat(DelphiPath,'\Bin\delphi32.exe');
To execute this program, we do:
shellexecute(handle,'',PChar(DelphiPath),'','',SW_ SHOW);
Read more on shellexecute in Delphi Help.
That concludes this tutorial...