Link to home
Start Free TrialLog in
Avatar of dys7opia
dys7opia

asked on

write to registry

Okay, what's the code to write a string to the registry.
The string I want is:

HKEY_CLASSES_ROOT\txtfile\shell\open\command

I'm doing this so I can make my program the default notepad. I really appreciate your help, thanks a lot! :)
Avatar of VSF
VSF
Flag of Brazil image

Try This:
procedure SaveSettings(Sender: TObject);
var
  hndKey: HKey;
  ValBuf: PChar;
  cb: Longint;
begin
  RegCreateKey('HKEY_CLASSES_ROOT\txtfile\shell\open\command',hndKey);
  RegSetValue(hndKey,'Test', 'Test');
  RegCloseKey(hndKey);
end;


Here is a bit of help:
Delphi offers the following API-routines for accessing the registry:

RegCreateKey()
RegOpenKey()
RegDeleteKey()
RegCloseKey()
RegEnumKey()
RegQueryValue()
RegSetValue()

3.1) RegCreateKey()
-------------------

Opens a key and if the key does not exist, it will be created.

Syntax: function RegCreateKey(Key: HKey; SubKey: PChar; var Result: HKey): Longint;

Key: The handle of the key which should be accessed. To write directly under
     the root, you can use HKEY_CLASSES_ROOT.

SubKey: The subkey to be accessed.

Result: The resulting key-handle.

Returns: ERROR_SUCCESS, if the function was successful, otherwise it will be
         an error value.

3.2) RegOpenKey()
-----------------

Opens an existing key. Unlike RegCreateKey, a non existing key returns an error
and will not be created.

Syntax: function RegOpenKey(Key: HKey; SubKey: PChar; var Result: HKey): Longint;

Key: The handle of the key which should be accessed. To write directly under
     the root, you can use HKEY_CLASSES_ROOT.

SubKey: The subkey to be accessed.

Result: The resulting key-handle.

Returns: ERROR_SUCCESS, if the function was successful, otherwise it will be
         an error value.


3.3) RegSetValue()
------------------

Writes a given value to the registry. Currently only a PChar-type can be
written. To store boolean or integer values, they must be converted.

Syntax: function RegSetValue(Key: HKey; SubKey: PChar; ValType: Longint; Value: PChar; cb: Longint): Longint;

Key: The Handle of the parent key (can be HKEY_CLASSES_ROOT).

SubKey: The subkey for which the value should be stored.

ValType: must be REG_SZ for win31.

Value: The value to be stored.

cb: Size in bytes for the Value parameter. Windows 3.1 ignores this paramater.

Returns: ERROR_SUCCESS if function was successful; otherwise an error is
         returned.


3.4) RegQueryValue()
--------------------

Reads a value from a given key (only PChar). If you want to read a boolean
or integer value, it must be converted since the win31 registry only stores
strings.

Syntax: function RegQueryValue(Key: HKey; SubKey: PChar; Value: PChar; var cb: Longint): Longint;

Key: The Handle of the parent key (can be HKEY_CLASSES_ROOT).

SubKey: The subkey from which the value should be read.

Value: Pointer to a buffer, which stores the read information. Must be a PChar.

cb: Size of the buffer. Contains the number of chars in the buffer, after
    completion of the function.

NOTE: The docs say, that the cb parameter is ignored for RegSetValue() and
      RegQueryValue(). My experience (Using Delphi and Win95 preview) is the
      contrary. Be sure alsways to set the cb parameter to the appropriate
      buffer size.

3.4) RegDeleteKey()
-------------------

Delets a key from the registry.

Syntax: function RegDeleteKey(Key: HKey; SubKey: PChar): Longint;

Key: The Handle of the parent key (can be HKEY_CLASSES_ROOT).

SubKey: The subkey which should be deleted.

Returns: ERROR_SUCCESS if the key was deleted, or ERROR_ACCESS_DENIED if
         the key is in use by another application.


3.5) RegEnumKey()
-----------------

Enumerates the keys for an open key.

Syntax: function RegEnumKey(Key: HKey; index: Longint; Buffer: PChar; cb: Longint): Longint;

Key: The handle of an open key (can be HKEY_CLASSES_ROOT)

index: The index of the subkey to retrieve. Should be zero on the first call.

Buffer: A buffer which will contain the name of the subkey when the function
       returns.

cb: The size of the buffer. Holds the number of chars copied to the buffer
    after completion of the function.

Returns: ERROR_SUCCESS if the function was successful. Otherwise an error is
         returned.

NOTE: Normally an application starts the enumeration with an index value
      of zero and increments it step by step.

GENERAL NOTES: HKEY_CLASSES_ROOT does not need to be opened. It is always
               open and avaliable.  However, using RegOpenKey() and
               RegCloseKey() on the HKEY_CLASSES_ROOT will speed up performance
               on subsequent read/write calls.

Hope this helps!
VSF
www.victory.hpg.com.br
www.boatoda.hpg.com.br
Avatar of dys7opia
dys7opia

ASKER

I put in:
---------------------------------------------
procedure SaveSettings(Sender: TObject);
var
 hndKey: HKey;
 ValBuf: PChar;
 cb: Longint;
begin
 RegCreateKey('HKEY_CLASSES_ROOT\txtfile\shell\open\command',hndKey);
 RegSetValue(hndKey,'Test', 'Test');
 RegCloseKey(hndKey);
end;
---------------------------------------------
and it keeps giving me this error:

"Incompatible Types: 'HKEY' and 'STRING'"

Do you know what this means and how I can fix it? Sorry, I'm still in the learning process of Delphi, only been doing it for about a month or so, thanks :)
API registry methods take time to learn. You could use the Delphi registry methods, here's somethin from the Delphi Help


procedure TForm1.Button1Click(Sender: TObject);
var
  Reg: TRegIniFile;
begin
  if Length(NameofKey.Text) or Length(ValueforKey.Text) <=0 then
    Showmessage('Either the key name or value is missing.')
  else
  begin
    Reg:=TRegIniFile.Create;
    try
      Reg.RootKey:=HKey_Local_Machine; // Section to look for within the registry
      if not Reg.OpenKey(NameofKey.Text,False) then
        if MessageDlg('The specified key does not exist, create it?'

                 ,Mtinformation,[mbYes,mbNo],0)=mryes then
        begin
          Reg.CreateKey(NameofKey.Text);
          if not Reg.OpenKey(NameofKey.Text,False) then
            ShowMessage('Error in Opening Created Key')
          else
            Reg.WriteString('Value1',ValueForKey.Text);
        end
     else
       Reg.WriteString('Value1',ValueForKey.Text);
    finally
      Reg.Free;
    end;

  end;
end;
HKEY_CLASSES_ROOT\txtfile\shell\open\command

Reg:=TRegIniFile.Create;
   try
     Reg.RootKey:=HKEY_CLASSES_ROOT;
     Reg.OpenKey('txtfile\shell\open\command',False);
     Reg.WriteString('', 'C:\WINDOWS\NOTEPAD.EXE %1');
   finally
     Reg.Free;
   end;
All of the code you guys have shown me seem to be very similiar, so it must work. But I have to be doing something wrong...but I dont know what! With each of the code I tired it keeps saying:

"Not Enough Actual Parameters"

I dont know how to resolve this....sorry for being a  other. :\
a bother****

i hate typos, heh :)
can you show your code?
Delphi Help (add Registry in you 'uses' clause):

function GetRegistryValue(KeyName: string): string;
var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create(KEY_READ);
  try
    Registry.RootKey = HKEY_LOCAL_MACHINE;
    // False because we do not want to create it if it doesn't exist
    Registry.OpenKey(KeyName, False);
    Result := Registry.ReadString('VALUE1');
  finally
    Registry.Free;
  end;
end;
your code could be better, here is some code that works for me to read a string value from the registry. . . When you deal with the registry do not assume that a value will be there or that a "Name" of a Data value will contain the type of value (string, integer, boolean) that you expect, TEST IT FIRST!


function TForm1.GetRegistryValue(KeyName, DataName: string): string;
var
 Reg1: TRegistry;
 Str1: String;
 RDInfo1: TRegDataInfo;
begin
Result := '';
Reg1 := TRegistry.Create;
 try
   Reg1.RootKey := HKEY_LOCAL_MACHINE;
{the registry can be changed by the user and any program,
 so you must test all values to see if they exist and to see
 if they have the kind of data you want to read}
   if not Reg1.OpenKey(KeyName, False) then Exit;
   if not Reg1.GetDataInfo(DataName, RDInfo1) then Exit;
   if RDInfo1.RegData = rdString then
   Result := Reg1.ReadString(DataName);
 finally
   Reg1.Free;
 end;
end;

procedure TForm1.but_WriteRegClick(Sender: TObject);
begin
Label6.Caption := GetRegistryValue('\Software\Microsoft\Internet Explorer\Main', 'Window Title');
end;
Okay...I just cant seem to get it to work. I dont see why though :(

Okay, I'm just gonna post the code to my program. I dont care if you steal it...but I prefer you not to, but who's stopping ya? Open source forever! :P

//------------------------------------------------------

unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdActns, ActnList, Menus, ExtActns, ComCtrls, ToolWin, ImgList,
  StdCtrls, StrUtils, ExtCtrls, clipbrd, OleCtrls, VCSpell3;

type
  TMainForm = class(TForm)
    MainMenu: TMainMenu;
    ActionList: TActionList;
    File1: TMenuItem;
    Edit1: TMenuItem;
    Format1: TMenuItem;
    Tools1: TMenuItem;
    Help1: TMenuItem;
    Open: TFileOpen;
    SaveAs: TFileSaveAs;
    EditCut: TEditCut;
    EditCopy: TEditCopy;
    EditPaste: TEditPaste;
    EditSelectAll: TEditSelectAll;
    EditUndo: TEditUndo;
    EditDelete: TEditDelete;
    RichEditBold: TRichEditBold;
    RichEditItalic: TRichEditItalic;
    RichEditUnderline: TRichEditUnderline;
    RichEditStrikeOut: TRichEditStrikeOut;
    RichEditBullets: TRichEditBullets;
    RichEditAlignLeft: TRichEditAlignLeft;
    RichEditAlignCenter: TRichEditAlignCenter;
    RichEditAlignRight: TRichEditAlignRight;
    About1: TMenuItem;
    FontDialog1: TFontDialog;
    ColorDialog1: TColorDialog;
    PrintDialog1: TPrintDialog;
    Icons: TImageList;
    Text: TRichEdit;
    SearchFind1: TSearchFind;
    SearchFindNext1: TSearchFindNext;
    PopupMenu1: TPopupMenu;
    New1: TMenuItem;
    Open1: TMenuItem;
    Save1: TMenuItem;
    SaveAs1: TMenuItem;
    Print1: TMenuItem;
    Exit1: TMenuItem;
    Undo1: TMenuItem;
    Cut1: TMenuItem;
    Copy1: TMenuItem;
    Paste1: TMenuItem;
    SelectAll1: TMenuItem;
    Font1: TMenuItem;
    FontColor1: TMenuItem;
    FileNew: TAction;
    FilePrint: TAction;
    Save: TAction;
    Find1: TMenuItem;
    FindNext1: TMenuItem;
    Replace1: TMenuItem;
    Memo: TMemo;
    Left1: TMenuItem;
    Center1: TMenuItem;
    Right1: TMenuItem;
    DateandTime1: TMenuItem;
    Cut2: TMenuItem;
    Copy2: TMenuItem;
    Paste2: TMenuItem;
    Font2: TMenuItem;
    Delete2: TMenuItem;
    SelectAll2: TMenuItem;
    Font3: TMenuItem;
    FontStyle2: TMenuItem;
    FontColor2: TMenuItem;
    Strikeout1: TMenuItem;
    Underline1: TMenuItem;
    Italic1: TMenuItem;
    Bold1: TMenuItem;
    ReplaceDialog1: TReplaceDialog;
    Minimize1: TMenuItem;
    Maximize1: TMenuItem;
    Restore1: TMenuItem;
    DisableKeyboard1: TMenuItem;
    EnableKeyboard1: TMenuItem;
    StatusBar1: TStatusBar;
    EnableClipboard1: TMenuItem;
    DisableClipboard1: TMenuItem;
    SpellCheck1: TMenuItem;
    VSSpell1: TVSSpell;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    N4: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    N7: TMenuItem;
    N8: TMenuItem;
    N12: TMenuItem;
    N13: TMenuItem;
    DeleteSelected1: TMenuItem;
    DeleteAll1: TMenuItem;
    WordCount1: TMenuItem;
    Calculator1: TMenuItem;
    N11: TMenuItem;
    ClearClipboard1: TMenuItem;
    procedure About1Click(Sender: TObject);
    procedure FileNewExecute(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure Exit1Click(Sender: TObject);
    procedure SaveExecute(Sender: TObject);
    procedure SaveAsSaveDialogTypeChange(Sender: TObject);
    procedure SaveAsBeforeExecute(Sender: TObject);
    procedure SaveAsCancel(Sender: TObject);
    procedure SaveAsAccept(Sender: TObject);
    procedure OpenAccept(Sender: TObject);
    procedure FontColor1Click(Sender: TObject);
    procedure Font1Click(Sender: TObject);
    procedure SearchFind1FindDialogFind(Sender: TObject);
    procedure SearchFind1BeforeExecute(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure Replace1Click(Sender: TObject);
    procedure DateandTime1Click(Sender: TObject);
    procedure FilePrintExecute(Sender: TObject);
    procedure Font3Click(Sender: TObject);
    procedure FontColor2Click(Sender: TObject);
    procedure Minimize1Click(Sender: TObject);
    procedure Maximize1Click(Sender: TObject);
    procedure Restore1Click(Sender: TObject);
    procedure DisableKeyboard1Click(Sender: TObject);
    procedure EnableKeyboard1Click(Sender: TObject);
    procedure EnableClipboard1Click(Sender: TObject);
    procedure DisableClipboard1Click(Sender: TObject);
    procedure SpellCheck1Click(Sender: TObject);
    procedure DeleteSelected1Click(Sender: TObject);
    procedure DeleteAll1Click(Sender: TObject);
    procedure WordCount1Click(Sender: TObject);
    procedure Calculator1Click(Sender: TObject);
    procedure ClearClipboard1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  Saved: Boolean = False;
  Filename: String = 'Untitled';
  Cancel: Boolean;
  Changes: Integer;
implementation

uses About, Word, DateTime, U_Calc1;

{$R *.dfm}

procedure TMainForm.About1Click(Sender: TObject);
begin
  AboutForm.Show;
end;

procedure TMainForm.FileNewExecute(Sender: TObject);
  Var Close: Boolean;
begin
  Close := False;
  FormCloseQuery(FileNew,Close);
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  Var Response: Integer;
begin
  Cancel := True;
  If Text.Modified = False then
    If Sender = FileNew then
      Text.Text := ''
    else
      Application.Terminate
  else
  begin
    Response := Messagedlg('Do you want to save the changes you made to '
      + Extractfilename(Filename),mtWarning,mbYesNoCancel,0);
    If Response = mrNo then
      If Sender = FileNew then
        Text.Text := ''
      else
        Application.Terminate
    else If Response = mrYes then
    begin
      If Saved = False then
        MainForm.ExecuteAction(SaveAs)
      else
        MainForm.ExecuteAction(Save);
      If Sender = FileNew then
      begin
        If Saved = True then
        begin
        Cancel := False;
        Text.Text := ''
        end;
      end;
    end
    else
      Cancel := False;
  end;
  CanClose := Cancel;
end;

procedure TMainForm.Exit1Click(Sender: TObject);
begin
  MainForm.Close;
end;

procedure TMainForm.SaveExecute(Sender: TObject);
begin
  If saved = True then
  begin
    Text.Modified := False;
    If Filename[Length(Filename)] = 't' then
    begin
      Memo.Lines := Text.Lines;
      Memo.Lines.SaveToFile(Filename);
    end
    else
      Text.Lines.SaveToFile(FileName);
  end
  Else
    MainForm.ExecuteAction(SaveAs);
end;

procedure TMainForm.SaveAsSaveDialogTypeChange(Sender: TObject);
begin
  Case SaveAs.Dialog.FilterIndex of
    1: SaveAs.Dialog.DefaultExt := '.rtf';
    2: SaveAs.Dialog.DefaultExt := '.txt';
    3: SaveAs.Dialog.DefaultExt := '.doc';
    4: SaveAs.Dialog.DefaultExt := '.rtf';
  end;
end;

procedure TMainForm.SaveAsBeforeExecute(Sender: TObject);
begin
  Case Filename[Length(Filename)] of
    'f':  SaveAs.Dialog.FilterIndex := 1;
    't':  SaveAs.Dialog.FilterIndex := 2;
    'c':  SaveAs.Dialog.FilterIndex := 3;
  end;
  If filename <> 'Untitled' then
    SaveAs.Dialog.FileName := Leftstr(Filename,Length(Filename)-4)
  Else
    SaveAs.Dialog.FileName := 'Untitled';
end;

procedure TMainForm.SaveAsCancel(Sender: TObject);
begin
  Cancel := False;
end;

procedure TMainForm.SaveAsAccept(Sender: TObject);
begin
  Saved := True;
  Text.Modified := False;
  Filename := SaveAs.Dialog.FileName;
  If SaveAs.Dialog.FilterIndex = 2 then
  begin
    Memo.Lines := Text.Lines;
    Memo.Lines.SaveToFile(SaveAs.Dialog.FileName);
  end
  else
    Text.Lines.SaveToFile(SaveAs.Dialog.FileName);
  MainForm.Caption := ExtractFileName(SaveAs.Dialog.FileName)
    + ' - Bitchin Notepad 2.0';
  Application.Title := ExtractFileName(SaveAs.Dialog.FileName)
    + ' - Bitchin Notepad 2.0';
end;

procedure TMainForm.OpenAccept(Sender: TObject);
begin
  Saved := True;
  Filename := Open.Dialog.FileName;
  Text.Lines.LoadFromFile(Open.Dialog.FileName);
  MainForm.Caption := ExtractFileName(Open.Dialog.FileName)
    + ' - Bitchin Notepad 2.0';
  Application.Title := ExtractFileName(Open.Dialog.FileName)
    + ' - Bitchin Notepad 2.0';
end;

procedure TMainForm.FontColor1Click(Sender: TObject);
begin
  If ColorDialog1.Execute then
    Text.SelAttributes.Color := ColorDialog1.Color;
end;

procedure TMainForm.Font1Click(Sender: TObject);
begin
  If FontDialog1.Execute then
  begin
    Text.SelAttributes.Name := FontDialog1.Font.Name;
    Text.SelAttributes.Size := FontDialog1.Font.Size;
    Text.SelAttributes.Style := FontDialog1.Font.Style;
  end;
end;

procedure TMainForm.SearchFind1FindDialogFind(Sender: TObject);
begin
  If AnsiPos(LowerCase(SearchFind1.Dialog.FindText),LowerCase(Text.Text)) > 0 then
  begin
    SearchFind1.Dialog.CloseDialog;
    Text.SelStart := AnsiPos(SearchFind1.Dialog.FindText,LowerCase(Text.Text)) - 1;
    Text.SelLength := Length(SearchFind1.Dialog.FindText);
  end
  else
    Messagebox(MainForm.Handle,'The search item was not found',
      'Bitchin Search',0);
end;

procedure TMainForm.SearchFind1BeforeExecute(Sender: TObject);
begin
  If Text.SelText <> '' then SearchFind1.Dialog.FindText := Text.SelText;
end;

procedure TMainForm.ReplaceDialog1Replace(Sender: TObject);
begin
  Changes := 0;
  repeat
  Text.SetFocus;
  If Text.SelText <> '' then
  begin
    Changes := Changes + 1;
    Text.SelText := Replacedialog1.ReplaceText;
  end;
  ReplaceDialog1Find(nil);
  until not (frReplaceAll in ReplaceDialog1.Options) or (Text.SelText = '')
end;

procedure TMainForm.ReplaceDialog1Find(Sender: TObject);
Var SText: String;
begin
  If LowerCase(Text.SelText) <> LowerCase(Replacedialog1.FindText) then
  begin
    Text.SelStart := 0;
    Text.SelLength := 0;
  end;
    SText := RightStr(LowerCase(Text.Text),Length(Text.Text) - Text.SelStart -
     Length(Replacedialog1.FindText));
  If (Text.SelStart = 0) and (Text.SelLength = 0) then SText := Text.Text;
  If AnsiPos(LowerCase(Replacedialog1.FindText),LowerCase(SText)) > 0 then
  begin
      Text.SetFocus;
      Text.SelStart := AnsiPos(LowerCase(Replacedialog1.FindText),
        LowerCase(SText)) - 1 + (Length(Text.Text) - Length(SText));
      Text.SelLength := Length(Replacedialog1.FindText);
  end
  else
    If frReplaceAll in Replacedialog1.Options then
    begin
      Messagedlg('Search was completed and ' + InttoStr(Changes) + ' replacements'
        + ' were made',mtInformation,[mbOK],0);
    end
    else
      Messagebox(MainForm.Handle,'The search item was not found',
        'Bitchin Search',0);
end;

procedure TMainForm.Replace1Click(Sender: TObject);
begin
  If Text.SelText <> '' then Replacedialog1.FindText := Text.SelText;
  Replacedialog1.Execute;
end;

procedure TMainForm.DateandTime1Click(Sender: TObject);
begin
  DateTimeForm.Show;
end;

procedure TMainForm.FilePrintExecute(Sender: TObject);
begin
  If PrintDialog1.Execute then
  begin
    Text.Print(Text.Text);
  end;
end;

procedure TMainForm.Font3Click(Sender: TObject);
begin
  Font1Click(nil);
end;

procedure TMainForm.FontColor2Click(Sender: TObject);
begin
  FontColor1Click(nil)
end;

procedure TMainForm.Minimize1Click(Sender: TObject);
begin
showwindow(handle, SW_MINIMIZE)
end;

procedure TMainForm.Maximize1Click(Sender: TObject);
begin
showwindow(handle, SW_MAXIMIZE)
end;

procedure TMainForm.Restore1Click(Sender: TObject);
begin
showwindow(handle, SW_RESTORE)
end;

procedure TMainForm.DisableKeyboard1Click(Sender: TObject);
begin
      Asm
     in  al,21h
     or al,00000010b
     out 21h,al
end;
end;

procedure TMainForm.EnableKeyboard1Click(Sender: TObject);
begin
      Asm
     in  al,21h
     mov al,0
    out 21h,al
end;
end;
 //dys7opia o_0
procedure TMainForm.EnableClipboard1Click(Sender: TObject);
begin
 CloseClipboard;

end;

procedure TMainForm.DisableClipboard1Click(Sender: TObject);
begin
 OpenClipboard (Application.MainForm.Handle);
end;

procedure TMainForm.SpellCheck1Click(Sender: TObject);
Var
sMemo : String;
begin
sMemo := Text.Text;
If Length(Trim(sMemo)) = 0 Then
MessageDlg('There isnt any text to check...type something first genius!',mtError,[MBOK],0);
If Length(Trim(sMemo)) <> 0 Then
VSSpell1.CheckText := Text.Text;
If VSSpell1.ResultCode = 0 Then
Text.Text := VSSpell1.Text;
end;

procedure TMainForm.DeleteSelected1Click(Sender: TObject);
begin
Text.ClearSelection
end;

procedure TMainForm.DeleteAll1Click(Sender: TObject);
begin
Text.Clear
end;

procedure TMainForm.WordCount1Click(Sender: TObject);
begin
  WordCountForm.Show;
end;

procedure TMainForm.Calculator1Click(Sender: TObject);
begin
  Form1.Show;
end;

procedure TMainForm.ClearClipboard1Click(Sender: TObject);
begin
clipboard.Clear;
end;

end.
My program works great! The only thing I have left to do is have an option for the user to choose to make it the default notepad (they can choose to make it default for the following file types: .rtf, .doc and .txt). Well, if you want me to post here when it's finished, I'll be happy to. Later days! :)
That would be helpfull if you posted your "try" to getting the registry too, you might be close and our examples would be closer to what you want to accomplish.

But add this:

===================================
uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdActns, ActnList, Menus, ExtActns, ComCtrls, ToolWin, ImgList,
 StdCtrls, StrUtils, ExtCtrls, clipbrd, OleCtrls, VCSpell3, {here -->} REGISTRY;
===================================

And use Slick182 function:

===================================
function TForm1.GetRegistryValue(KeyName, DataName: string): string;
var
Reg1: TRegistry;
Str1: String;
RDInfo1: TRegDataInfo;
begin
Result := '';
Reg1 := TRegistry.Create;
try
  Reg1.RootKey := HKEY_LOCAL_MACHINE;
{the registry can be changed by the user and any program,
so you must test all values to see if they exist and to see
if they have the kind of data you want to read}
  if not Reg1.OpenKey(KeyName, False) then Exit;
  if not Reg1.GetDataInfo(DataName, RDInfo1) then Exit;
  if RDInfo1.RegData = rdString then
  Result := Reg1.ReadString(DataName);
finally
  Reg1.Free;
end;
end;
===================================

And use this to getting this function to work on one of your button:

===================================
Caption := GetRegistryValue('\Software\Microsoft\Internet Explorer\Main', 'Window Title');
===================================

And to write to registry use something like:

===================================
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER; // or HKEY_LOCAL_MACHINE
    if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\RunOnce', True) then
    begin
      Reg.WriteString('MyApp','"' + ParamStr(0) + '"');
      Reg.CloseKey;
    end;
  finally
    Reg.Free;
    inherited;
  end;
===================================

The last code would put your program to run on startup the next time you start your computer. You can adapt it by posting your values like Slick182 code.

Things to remember:

  - Reg.RootKey := HKEY_CURRENT_USER; // this in capital is not a string, it is a constant available after adding Registry to you uses clause.
  - if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\RunOnce', True) // the path to where you want to write or read, after the HKEY thing is a string.

Hope it helps and post your code to use the registry that doesn't work if you need more help on this.
procedure TForm1.but_DefaultTextClick(Sender: TObject);
var
 Reg1: TRegistry;
 Str1: String;
 RDInfo1: TRegDataInfo;

procedure DoError;
begin
MessageBox(Handle,'There was a Registry access Error, Unable to make this the default text editor',
          'ERROR, Registry access Error', MB_OK or MB_ICONERROR);
end;

begin
if MessageBox(Handle,'Do you want to make this your Default text Editor? ?',
          'Make this the Text Editor? ?', MB_YESNO or MB_ICONQUESTION) <> IDYES then Exit;
Reg1 := TRegistry.Create;
 try
   Reg1.RootKey := HKEY_CLASSES_ROOT;
   if not Reg1.OpenKey('.txt', true) then
     begin
     DoError;
     Exit;
     end;
   Str1 := Reg1.ReadString('');
{an empty string is the {Default) item}
   if Str1 = '' then
     begin
     Reg1.WriteString('', 'txtfile');
     Str1 := 'txtfile';
     end;
   if not Reg1.OpenKey('\'+Str1+'\shell\open\command', true) then
     begin
     DoError;
     Exit;
     end;
   Reg1.WriteString('', '"'+ParamStr(0)+'" "%1"');
   Reg1.OpenKey('\'+Str1+'\DefaultIcon', true);
   Reg1.WriteString('', Application.ExeName+',0');
{you should create a special file Icon}
   Label6.Caption := Str1;
  finally
   Reg1.Free;
 end;
end;
you don't need the

 RDInfo1: TRegDataInfo;

I forgot to remove it
@slick812: I tried your code, it was working too, no errors in the compiling or anything, but when i clicked make it default, "yes", it didnt work. I tried restarting the computer, then editing the code a little, still no luck. I'm just gonna post a download link to my source code, heh. Could someone...edit the code, lol. I'd like it to have the "default option" in the ABOUT drop down menu. I'll be sure to add you to the "greets and thanks" section. :)

Link: http://www.geocities.com/dys7opia/bitchin_notepad_source.zip
Size: 593 KB

Thank you again for all your help. It's REALLY apprecaited. :)
I used your Editor.exe from the zip file on 2 different systems, Win98, and Win XP, and in both systems when i clicked the Default menu Item, it set the default text editor to your program. So what does not work about the code I gave you? ? ? Since you gave your entire app, I am guessing you want suggestions for improvement? ?

things that might need improvement -
1. your app does not get command line parameters to open a file sent to it by the system. So even if your app is the default editor and you double click on a text file, your app opens without any text document in it at all.

2. There is no drag and drop of text files on your app, this is a windows standard and should be in any app that opens files.

3. Why do you have "Disable Keyboard" ? ?, who would use this?

4. Why do you have "Disable Clipboard" ? ?, who would use this?

5. Your Find text dialog box does not allow a paste into it's Edit control, And when you do a text search, the text does NOT scroll the found text into view.

6. Why do you have the choose font options all listed, it seems like you could have a single "Choose Font" and get all the info about the font like font name, font color, bold, and anything else about the font from a single dialog box.

7. your app does NOT remember it's size or position or state (maximized), from when it was last closed, seems like this is a windows standard.

8. Why would you have the minimize, maximize and restore in the file menu? Aren't they on the title bar anyway? And you use the wrong code to minimize a delphi MainForm. You could use -
Application.Minimize;

9. Your Status bar does not show any status at all, even from the menu.
Hmm...I have WindowsME and I clicked, make it default, it didnt work...

1. I cant seem to get it to be the default, so I wouldnt know...

2. i never thought of that, thanks for bringing it up.

3. I dont know, heh...

4. I dont know about that one either. :P

5. I just used the basic Find feature given in Delphi 6, I never though about trying to edit it or anything.

6. I dont really think its that big of deal... Unless you're a REALLY lazy person and you dont want to click another button. But I guess you have a point...people today are extremely lazy, heh. :P

7. I dont think that's a windows standard... Tons of apps dont use this feature. But I suppose it would be useful for a text editor, thanks.

8. I just thought it would be useful, someone might have some weird skin for windows that might now have any of those buttons in the title bar. I used to have some before.

9. Yeah...I've been meaning to change that. :\
[EDIT #8]
some weird skin for windows that might NOT*

I should really start reading through my posts before I submit them... :\
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
dys7opia:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.