Results 1 to 4 of 4

Thread: Execute program from Windows Temp folder?

  1. #1

    Execute program from Windows Temp folder?

    Code:
    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?

  2. #2

    Execute program from Windows Temp folder?

    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

  3. #3

    Execute program from Windows Temp folder?

    GetTempPath should do the trick
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  4. #4

    Execute program from Windows Temp folder?

    if you need delphi procedures...

    Code:
    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);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •