Appel de programme externe

Salut les clubiquien :stuck_out_tongue:

Je fais un lanceur d’applications. J’utilise plusieures ComboBox : Appli, Utilitaire…
Je voudrai que lorsque je choisi une appli dans une des ComboBox, Delphi aille ouvrir l’exe correspondant sur le dur.
Ex : Quand je clic sur Word, Delphi cherche dans C:\Program Files\Microsoft Office\Microsoft Word.exe et lance Microsoft Word.exe

J’espère être assez clair.

Merci d’avance :smiley: :wink:

Salut Ghost39,

ShellExecute(0,‘OPEN’,‘c:\windows\notepad.exe’, Nil, Nil, SW_SHOW);

pour lancer notepad…

Sans oublier ShellApi dans la section uses de ton programme :

uses […], ShellApi;

Bye.

Etienne.

Tu as cette fonction aussi qui te creé un thread de ton executable.
Cette fonction est sympa notamment pour les programmes en ligne de commande dont tu veux récupérer le résultat qui s’affiche pendant et à la suite du programme.

La variable cmd est la commande proprement dite et la variable buff récupère le résultat.

procedure TBankBlaster.ExecuteBinary(const cmd: string; var Buff: string);
var
Env: array[0…255] of char;
sa: SECURITY_ATTRIBUTES;
si: STARTUPINFO;
pi: PROCESS_INFORMATION;
hPipeOutputRead: Thandle;
hPipeOutputWrite: Thandle;
hPipeInputRead: Thandle;
hPipeInputWrite: Thandle;
bTest: boolean;
dwNumberOfBytesRead: dword;
//szMsg : array[0…100] of char;
szBuffer: array[0…256] of char;
//res_wait: integer;
begin
FillChar(si, SizeOf(si), 0);
FillChar(pi, SizeOf(pi), 0);
FillChar(sa, SizeOf(sa), 0);

sa.nLength := sizeof(sa);
sa.bInheritHandle := TRUE;
sa.lpSecurityDescriptor := nil;

// Create pipe for standard output redirection.
CreatePipe(hPipeOutputRead, // read handle
hPipeOutputWrite, // write handle
@sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);

// Create pipe for standard input redirection.
CreatePipe(hPipeInputRead, // read handle
hPipeInputWrite, // write handle
@sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);

// Make child process use hPipeOutputWrite as standard out,
// and make sure it does not show on screen.
si.cb := sizeof(si);
si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
si.wShowWindow := SW_HIDE;
si.hStdInput := hPipeInputRead;
si.hStdOutput := hPipeOutputWrite;
si.hStdError := hPipeOutputWrite;
//Env := ‘MEME_DIRECTORY=c:’ + #0 + #0;
CreateProcess(
nil, PChar(cmd),
nil, nil,
TRUE, CREATE_NEW_CONSOLE or CREATE_NEW_PROCESS_GROUP,
@Env, nil,
si, pi);
// Now that handles have been inherited, close it to be safe.
// You don’t want to read or write to them accidentally.
CloseHandle(hPipeOutputWrite);
CloseHandle(hPipeInputRead);

// Now test to capture DOS application output by reading
// hPipeOutputRead. Could also write to DOS application
// standard input by writing to hPipeInputWrite.

//while not FStop do
while TRUE do
begin
bTest := ReadFile(
hPipeOutputRead, // handle of the read end of our pipe
szBuffer, // address of buffer that receives data
256, // number of bytes to read
dwNumberOfBytesRead, // address of number of bytes read
nil // non-overlapped.
);

if not (bTest) then
begin
      // writeln(szMsg, 'Error reading pipe.',GetLastError());
      //  MessageBox(nil, szMsg, 'Test', MB_OK);
  break;
end;

  // do something with data.
szBuffer[dwNumberOfBytesRead] := #0; // null terminate
  //writeln(szBuffer)
Buff := Buff + szBuffer;
  //MessageBox(nil, szBuffer, 'Test', MB_OK);

end;

Buff := Buff + szBuffer;

// Wait for CONSPAWN to finish.
{
res_wait := WaitForSingleObject(pi.hProcess, 20);
if not FStop and (res_wait = WAIT_TIMEOUT) then
WaitForSingleObject(pi.hProcess, INFINITE)
else
}
TerminateProcess(pi.hProcess, 0);
// Close all remaining handles
CloseHandle(pi.hProcess);
CloseHandle(hPipeOutputRead);
CloseHandle(hPipeInputWrite);
end;