PDA

View Full Version : Execute program from Windows Temp folder?



Grendelus
05-02-2008, 07:19 PM
unit Unit1;

interface

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

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin

{execute the game}
WinExec('%WINDIR%\Temp\game.exe', SW_SHOW);

{end the application}
Application.Terminate;
end;

end.

I am trying to create a program (launcher) which opens an executable (game.exe) when it runs. Problem is that the game is located in the Windows Temp directory. Is there a way to execute it from there? Also how can I execute programs and wait for them to finish?

paul_nicholls
05-02-2008, 10:07 PM
Hi,
perhaps this link will help you with executing a program and waiting for it to finish.

http://www.scalabium.com/faq/dct0050.htm

As for finding the windows temp folder name, I can't recall how to do that part.

cheers,
Paul

Legolas
05-02-2008, 10:49 PM
GetTempPath should do the trick :)

harrypitfall
06-02-2008, 01:34 AM
if you need delphi procedures...



Function GetTemporaryDirectory: String;
Var
lpBuffer: PChar;
Begin
lpBuffer := AllocMem(MAX_PATH + 1);
Try
GetTempPath(MAX_PATH, lpBuffer);
Result := Trim(StrPas(lpBuffer));
Finally
FreeMem(lpBuffer, MAX_PATH + 1);
End;
End;

Procedure Run(Operation, FileName, Parameters, Directory: String; ShowCmd: Integer; WaitTerminate: Boolean);
Var
Sei: TShellExecuteInfo;
Begin
FillChar(Sei, SizeOf(TShellExecuteInfo), 0);
Sei.cbSize := SizeOf(TShellExecuteInfo);
Sei.fMask := SEE_MASK_NOCLOSEPROCESS;
Sei.Wnd := Application.Handle;
Sei.lpVerb := PChar(Operation);
Sei.lpFile := PChar(FileName);
Sei.lpParameters := PChar(Parameters);
Sei.lpDirectory := PChar(Directory);
Sei.nShow := ShowCmd;
If Not (ShellExecuteEx(@Sei)) Then
Case Sei.hInstApp Of
SE_ERR_FNF: Raise Exception.CreateFmt('File "%s" not found', [FileName]);
SE_ERR_PNF: Raise Exception.CreateFmt('Path "%s" not found', [Directory]);
SE_ERR_NOASSOC: Raise Exception.Create('No associated program found');
ERROR_BAD_FORMAT: Raise Exception.Create('Invalid executable format');
SE_ERR_ACCESSDENIED: Raise Exception.Create('Access denied');
SE_ERR_DDETIMEOUT, SE_ERR_DDEFAIL, SE_ERR_DDEBUSY: Exception.Create('DDE transaction error');
Else
Raise Exception.Create(SysErrorMessage(GetLastError));
End
Else If WaitTerminate Then
WaitForSingleObject(Sei.hProcess, INFINITE);
End;

// example:
var Dir, FileName: String;
Begin
Dir := IncludeTrailingPathDelimiter(GetTemporaryDirectory );
FileName := Dir + 'GAME.EXE';
Run('open', FileName, '', Dir, SW_SHOWNORMAL, false);