lancer un exe depuis une appli delphi

lancer un exe depuis une appli delphi - Programmation

Marsh Posté le 09-04-2001 à 14:17:44    

A partir du source d'une appli delphi (4.0) comment qu'on fait pour lancer un exe étranger au programme ?
 
D'autre part mon appli dispose d'une aide au format html que je voudrais faire afficher par le nav par defaut du syst : comment qu'on fait ?
 
Merci aux âmes charitables !

Reply

Marsh Posté le 09-04-2001 à 14:17:44   

Reply

Marsh Posté le 09-04-2001 à 14:51:57    

pour lancer un exe je l'ai fait et ca marche bien, je post ca ce soir. et je te repondrais aussi pour le html;
 
a bientot

Reply

Marsh Posté le 09-04-2001 à 14:54:45    

mais en gros, il faut utiliser l'API Win32

Reply

Marsh Posté le 09-04-2001 à 21:52:39    

function TForm1.LanceAppliAttenteFin(NomFichier:string):boolean;
{permet de lancer un exécutable. NomFichier est le nom avec chemin     }
{de cet exécutable ou d'un raccourci qui pointe vers cet exécutable. }
{ notre programme est arrété tant que l'exécutable n'est pas fini      }
{tout est arrété on ne peut donc même plus déplacer sa fenêtre.        }
{ il est donc préférable de la rendre invisible avant le lancement     }
{de cette fonction.                                                    }
{ LanceAppliAttenteFin renvoie true si le lancement s'est bien passé   }
var
  StartInfo : TStartupInfo;
  ProcessInformation : TProcessInformation;
  n:longint;
begin
  n:=Length(NomFichier);
  NomFichier:=Lowercase(NomFichier);
  if ( n > 3 ) then
    if not ( ( NomFichier[n-2] = 'e' ) and ( NomFichier[n-1] = 'x' ) and ( NomFichier[n] = 'e' ) ) then
      NomFichier:='C:\WINDOWS\RUNDLL32.EXE amovie.ocx,RunDll /play /close '+NomFichier;
  result:=true;
  ZeroMemory(@StartInfo, sizeof(StartInfo)); // rempli de 0 StartInfo
  StartInfo.cb:=sizeof(StartInfo);
  if CreateProcess(nil,PChar(NomFichier),nil,nil,true,0,nil,nil,StartInfo,ProcessInformation) then
    begin
      Form1.FormStyle:=fsNormal;
      Form1.Hide;
      WaitForSingleObject(ProcessInformation.hProcess, INFINITE)// attend que l'application désignée par le handle ProcessInformation.hProcess soit terminée
    end
  else result:=false;
end;

Reply

Marsh Posté le 09-04-2001 à 22:28:46    

Y a vachement plus simple:
 
Il faut rajouter shellApi dans les Uses.
 
ShellExecute(0, Nil, 'nom_application.exe', 'param1 param2 ...', 'c:\repertoire_de_travail', SW_NORMAL);
 
val du dernier param: sw_normal, sw_maximized, sw_minimized (je crois)
 
si les chaines sont du type string il faut faire PCHAR(chaine) pour les param.
 
Pour lancer l'aide html:
 
ShellExecute(0, Nil, 'aide.html', Nil, Nil, SW_NORMAL);
ça marche très bien

 

[edit]--Message édité par antp--[/edit]


---------------
mes programmes ·· les voitures dans les films ·· apprenez à écrire
Reply

Marsh Posté le 09-04-2001 à 22:34:37    

en fait, les valeurs du dernier parametre de shellexecute sont:

Citation :


SW_HIDE
 Hides the window and activates another window.
 
SW_MAXIMIZE
 Maximizes the specified window.
 
SW_MINIMIZE
 Minimizes the specified window and activates the next top-level window in the Z order.
 
SW_RESTORE
 Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.
 
SW_SHOW
Activates the window and displays it in its current size and position.  
 
SW_SHOWDEFAULT
 Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.
 
SW_SHOWMAXIMIZED
 Activates the window and displays it as a maximized window.
 
SW_SHOWMINIMIZED
 Activates the window and displays it as a minimized window.
 
SW_SHOWMINNOACTIVE
 Displays the window as a minimized window. The active window remains active.
 
SW_SHOWNA
 Displays the window in its current state. The active window remains active.
 
SW_SHOWNOACTIVATE
 Displays a window in its most recent size and position. The active window remains active.
 
SW_SHOWNORMAL
 Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

 

[edit]--Message édité par JWhy--[/edit]


---------------
www.alliancefrancophone.org ... Home is where the heart is
Reply

Marsh Posté le 10-04-2001 à 13:56:32    

Merci à tous, j'vais essayer ça !

Reply

Marsh Posté le 10-04-2001 à 14:38:22    

antp a écrit a écrit :

Y a vachement plus simple:
 
Il faut rajouter shellApi dans les Uses.
 
ShellExecute(0, Nil, 'nom_application.exe', 'param1 param2 ...', 'c:\repertoire_de_travail', SW_NORMAL);
 
val du dernier param: sw_normal, sw_maximized, sw_minimized (je crois)
 
si les chaines sont du type string il faut faire PCHAR(chaine) pour les param.
 
Pour lancer l'aide html:
 
ShellExecute(0, Nil, 'aide.html', Nil, Nil, SW_NORMAL);
ça marche très bien
 
 




 
ce que j'ai mis est tres simple. seulement comme je suis un peu faigant, j'ai copier betement la fonction complete qui n'est pas tres utile.
 
En fait j'utilise (c'est la version Delphi 4.0 d'ou les nil à la place de NULL) :
 
  CreateProcess(nil,PChar(NomFichier),nil,nil,true,0,nil,nil,StartInfo,ProcessInformation)  
la documentation msdn est tres bien faites, profitez en !!!
 
http://msdn.microsoft.com/library/ [...] d_9dpv.htm
 
CreateProcess
The CreateProcess function creates a new process and its primary thread. The new process runs the specified executable file.  
 
To create a process that runs in a different security context, use the CreateProcessAsUser or CreateProcessWithLogonW function.  
 
BOOL CreateProcess(
  LPCTSTR lpApplicationName,                 // name of executable module
  LPTSTR lpCommandLine,                      // command line string
  LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
  LPSECURITY_ATTRIBUTES lpThreadAttributes,  // SD
  BOOL bInheritHandles,                      // handle inheritance option
  DWORD dwCreationFlags,                     // creation flags
  LPVOID lpEnvironment,                      // new environment block
  LPCTSTR lpCurrentDirectory,                // current directory name
  LPSTARTUPINFO lpStartupInfo,               // startup information
  LPPROCESS_INFORMATION lpProcessInformation // process information
);
Parameters
lpApplicationName  
[in] Pointer to a null-terminated string that specifies the module to execute.  
The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path.  
 
The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space-delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:  
 
c:\program.exe files\sub dir\program name
c:\program files\sub.exe dir\program name
c:\program files\sub dir\program.exe name
c:\program files\sub dir\program name.exe  
 
The specified module can be a Win32-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.  
 
Windows NT/2000: If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module as well as its arguments. A 16-bit application is one that executes as a VDM or WOW process.  
 
lpCommandLine  
[in] Pointer to a null-terminated string that specifies the command line to execute. The system adds a null character to the command line, trimming the string if necessary, to indicate which file was actually used.  
Windows NT/2000: The Unicode version of this function, CreateProcessW, will fail if this parameter is a const string.  
 
The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.  
 
If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. C runtime processes can use the argc and argv arguments. Note that it is a common practice to repeat the module name as the first token in the command line.  
 
If lpApplicationName is NULL, the first white-space – delimited token of the command line specifies the module name. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin (see the explanation for the lpApplicationName parameter). If the file name does not contain an extension, .exe is appended. If the file name ends in a period (.) with no extension, or if the file name contains a path, .exe is not appended. If the file name does not contain a directory path, the system searches for the executable file in the following sequence:  
 
The directory from which the application loaded.  
The current directory for the parent process.  
Windows 95/98: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.  
Windows NT/2000: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.  
 
Windows NT/2000: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is System.  
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.  
The directories that are listed in the PATH environment variable.  
lpProcessAttributes  
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpProcessAttributes is NULL, the handle cannot be inherited.  
Windows NT/2000: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new process. If lpProcessAttributes is NULL, the process gets a default security descriptor.  
 
lpThreadAttributes  
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.  
Windows NT/2000: The lpSecurityDescriptor member of the structure specifies a security descriptor for the main thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.  
 
bInheritHandles  
[in] Indicates whether the new process inherits handles from the calling process. If TRUE, each inheritable open handle in the calling process is inherited by the new process. Inherited handles have the same value and access privileges as the original handles.  
dwCreationFlags  
[in] Specifies additional flags that control the priority class and the creation of the process. The following creation flags can be specified in any combination, except as noted.

Reply

Marsh Posté le 10-04-2001 à 16:13:36    

Pour lancer un exe :
 
WinExec('c:\prog.exe',sw_shownormal);
 
c'est vachement plus simple que la methode ShellExecute quand
tu veut juste lancer un executable.
Mais c'est vrai que ShellExecute te permet de lancer nimportequoi.
 
Voila

Reply

Marsh Posté le 11-04-2001 à 09:24:04    

Realist a écrit a écrit :

Pour lancer un exe :
 
WinExec('c:\prog.exe',sw_shownormal);
 
c'est vachement plus simple que la methode ShellExecute quand
tu veut juste lancer un executable.
Mais c'est vrai que ShellExecute te permet de lancer nimportequoi.
 
Voila




 
Dans la doc que j'ai ils insistent en disant que WinExec c'est un vieux truc, et qu'il faut plus l'utiliser, mais il faut prendre ShellExecute qui le remplace. Comme tu le dis ça marche qu'avec les executables en plus.


---------------
mes programmes ·· les voitures dans les films ·· apprenez à écrire
Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed