Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running external Apps from ini

Status
Not open for further replies.

Knad04

Programmer
Sep 3, 2004
11
GB
Hello,

Im new to delphi and just finished a tutorial on how to make a currency converter lol.

Well..

I want to make a programme that launches external programmes using file paths in an external file (ini maybe) and was wondering how to do this.

Any help is much appreciated
 
I bet you could use the ShellExecute command after extracting the path information from the ini file.

les
 
I have found this on many sites..but it doesnt seem to work..some error with the 'shellexecute' bit :(
And i dont knwo how to load from ini's lol, im kinda new and cant find any gd tuts

thanks
 
Ok, well, here's some code examples that may help:

Code:
uses
TIniFiles;

var
FileInfo: TIniFile;
strFileName : string;

begin

//I use the ExtractFilePath so that the INI file that is found is the same one in the directory where the application is being run

FileInfo:= TIniFile.Create(ExtractFilePath(Application.ExeName) + 'FileInfo.ini');


strFileName := judgeIni.ReadString('File Info', 'File1', '');

ShellExecute(Handle, 'open', PChar(strFileName), nil, nil, SW_SHOWNORMAL);

and your ini file should look something like:

Code:
[FileInfo]
File1=  Q:\DelphSource\DisplayDocket\Newest
[SomeOtherInfo]
OtherInfo=display

Leslie
 
There is a class called TIniFile - look for it in the Help - it's very easy to use.

You could also use a TStringList to load text from a file, something like this:
Code:
function TForm1.LoadTxt(sFileName: String): Boolean;
var
  sl: TStringList;
  i: Integer;
  sPath: String;
begin
  result := False;
  if FileExists(sFileName) then
  begin
    sl := TStringList.Create;
    try
      sl.LoadFromFile(sFileName);
      for i := 0 to (sl.Count - 1) do
      begin
        sPath := sl[i];
        //do whatever you want with the info you just got from the file 
      end
    finally
      sl.Free;
    end;
  end
  else
    ShowMessage(sFileName + ' Does Not Exist);
end;

As for your problem with ShellExecute, post your code so that we can see what you're doing with it.

-Dell
 
For ShellExecute you must add ShellAPI to your uses clause.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
how do i do that?? cos shell execute still wont work lol

Thanks :D
 
When you say it "wont work" do you mean that it won't compile correctly or do you mean that there is a runtime error?

In either case it would be helpful to know exactly what the error message is or exactly what the unexpected behaviour is.


Andrew
Hampshire, UK
 
Thanks all.

Error is

[Error] Unit1.pas(27): Undeclared identifier: 'ShellExecute'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

Code:
ShellExecute(Handle, 'open',
  'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL);
 
this is what you need to do:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.OpenNotePad1Click(Sender: TObject);
begin
ShellExecute(Handle, 'open', 'c:\Windows\notepad.exe', nil, nil, SW_SHOWNORMAL);
end;

end.

You can just type it in there :)

Hope this helps,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
IT WORKS :D thank you :)

now i know im probs pushin now lol, but anyhoo:

How do i load a list of exe locations from an ini file and assign them to buttons??

so i can have say a notepad button, and internbet button and a msn messenger button maybe.

Thank you so much all :D
 
Well, although I personally prefer working with the registry, you could do that like this if you have those three buttons on your form (I'll take the code I posted above and elaborate on that):

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    App1, App2, App3: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IniFiles;

{$R *.dfm}

function RunApp(MyApp: string): Pointer;
begin
ShellExecute(handle,PChar('open'),PChar(MyApp),nil,nil,SW_SHOWNORMAL);
Result := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunApp(App1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunApp(App2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunApp(App3);
end;

procedure TForm1.FormCreate(Sender: TObject);
var Ini: TIniFile;
begin
// you do not actually need to make the extension .ini, it can be whatever you want.
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
app1 := Ini.ReadString('Buttons','MSN','C:\Program Files\MSN\messenger.exe');
app2 := ini.ReadString('Buttons','Notepad','C:\Windows\notepad.exe');
app3 := ini.ReadString('Buttons','IE','C:\Program Files\Internet Explorer\iexplorer.exe');
Ini.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var Ini: TIniFile;
begin
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
ini.WriteString('Buttons','MSN',app1);
ini.WriteString('Buttons','Notepad',app2);
ini.WriteString('Buttons','IE',app3);
Ini.Free;
end;

end.

To explain:

What I did was create a function which allows me to run a program if I enter the correct path and filename, the information is loaded from the ini as the form is created.
App1, app2 and App3 variables are declared globally so you can access them from any procedure in the unit. When someone clicks the button the variable is passed to the function and set in the correct place and the application will be run. When the form closes the information in app1, app2 and app3 are being written to the ini file. All you need to do now is make a procedure that allows you to add the correct information to the corresponding global variable, and therefore button.

Should you have no idea how many buttons and variables you are going to have try working with an object list and an array.

I hope this helps,

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
Ow and make sure you have the same sections and keynames in your load procedure and your save procedure.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
ok, ill give it a go thanks alot m8 :)

ill tell ya how it goes..not sure about arrays and makin procedure to make globar vars, but im sure ill work it out lolz

thanks :)
 
OK tried that, and edited code a bit...

i get this error tho :S

[Error] Unit1.pas(36): Undeclared identifier: 'Handle'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

And here the prog i use (thanks so much BobbaFet)
Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    App1, App2, App3: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IniFiles;

{$R *.dfm}

function RunApp(MyApp: string): Pointer;
begin
ShellExecute(Handle,PChar('open'),PChar(MyApp),nil,nil,SW_SHOWNORMAL);
Result := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunApp(App1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
RunApp(App2);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
RunApp(App3);
end;

procedure TForm1.FormCreate(Sender: TObject);
var Ini: TIniFile;
begin
// you do not actually need to make the extension .ini, it can be whatever you want.
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
app1 := Ini.ReadString('Buttons','MSN','C:\Program Files\MSN\messenger.exe');
app2 := ini.ReadString('Buttons','Notepad','C:\Windows\notepad.exe');
app3 := ini.ReadString('Buttons','IE','C:\Program Files\Internet Explorer\iexplorer.exe');
Ini.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var Ini: TIniFile;
begin
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
ini.WriteString('Buttons','MSN',app1);
ini.WriteString('Buttons','Notepad',app2);
ini.WriteString('Buttons','IE',app3);
Ini.Free;
end;

end.
 
my bad, that should be application.handle

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
it runs with no errors :)

but no ini is created or no prgrammes run wen i click buttons :(

Sorry if i sound like a n00b...i am lol

Thanks fo all the help, especially BobbaFet..derserves medal for puttin up wit me!
 
this line of code:

Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');

looks for an ini file in the same directory that the program is running from named 'app'

Do you have this app.ini file in the directory? Does it have the following?:

Code:
[Buttons]
MSN =  C:\Program Files\MSN\messenger.exe
Notepad = C:\Windows\notepad.exe
IE = C:\Program Files\Internet Explorer\iexplorer.exe

if you put a break point in the FormCreate, do app1, app2 and app3 get the correct information assigned to them?


Leslie
 
OK there wasnt an 'app.ini' in the folder so i put one in and inserted the code u gave me, but when i run the programme (i built it to .exe) the 3 buttons on my form appear, but when i click them nothing happens.

ftp://ftp.knad.co.uk/public_html/AppLoader.zip <-- here is my programme with delphi files etc

I know alot of you dont trust downloadin files, so here is code:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ShellAPI, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    App1, App2, App3: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses IniFiles;

{$R *.dfm}

function RunApp(MyApp: string): Pointer;
begin
ShellExecute(Application.Handle,PChar('open'),PChar(MyApp),nil,nil,SW_SHOWNORMAL);
Result := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
RunApp(App1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
RunApp(App2);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
RunApp(App3);
end;

procedure TForm1.FormCreate(Sender: TObject);
var Ini: TIniFile;
begin
// you do not actually need to make the extension .ini, it can be whatever you want.
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
app1 := Ini.ReadString('Buttons','MSN','C:\Program Files\MSN\messenger.exe');
app2 := ini.ReadString('Buttons','Notepad','C:\Windows\notepad.exe');
app3 := ini.ReadString('Buttons','IE','C:\Program Files\Internet Explorer\iexplorer.exe');
Ini.Free;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var Ini: TIniFile;
begin
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + '\app.ini');
ini.WriteString('Buttons','MSN',app1);
ini.WriteString('Buttons','Notepad',app2);
ini.WriteString('Buttons','IE',app3);
Ini.Free;
end;

end.

Thank you so much all :D your a gr8 help :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top