1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
{*------------------------------------------------------------------------------
Effectuer un ShellExecute et attendre la fin du process lancé
@Param vHandle Handle de la fenêtre parent pour toute fenêtre de message
que le système pourrait produire pendant l'exécution de
cette fonction
@Param vOperation Opération à réaliser :
- "open" : La fonction ouvre le fichier spécifié.
Le fichier à ouvrir peut être un exécutable, un document
ou un répertoire.
- "print" : La fonction imprime le fichier spécifié.
Le fichier à imprimer doit être un document.
Si le fichier à imprimer est un exécutalbe, la fonction
ouvre le fichier comme si "open" avait été spécifié.
- "explore" : La fonction explore le répertoire spécifié
@Param vFichier Fichier à ouvrir ou à imprimer ou répertoire à ouvrir ou
explorer. La fonction peut ouvrir un exécutable ou un
document ou imprimer un document.
@Param vParametres Paramètres à passer à l'application si le fichier
spécifié est un exécutable. Si le fichier spécifié est
un document, ne rien passer.
@Param vRepertoire Répertoire d'exécution. S'il n'est pas indiqué le
répertoire en cours est utilisé
@Param vAffichage Indique comment la fenêtre doit être montrée. Utiliser
les variables Delphi :
- SW_HIDE,
- SW_SHOWNORMAL,
- SW_NORMAL,
- SW_SHOWMINIMIZED,
- SW_SHOWMAXIMIZED,
- etc...
@Param vDuree Durée d'attente de la fin d'exécution en millisecondes.
Si la fin de l'exécution n'est pas terminée dans le temps
imparti, la fonction se termine.
Pour attendre à l'infini utiliser la variable INFINITE.
@Param ExitCode Le code de retour de l'application qui a été executée
@Return Renvoi un code d'erreur :
- NO_ERROR L'attente a aboutit
- WAIT_FAILED L'attente n'a pas aboutit
- ERROR_FILE_NOT_FOUND Le fichier spécifié n'a pas été
trouvé
- ERROR_PATH_NOT_FOUND Le chemin spécifié n'a pas été
trouvé
- ERROR_DDE_FAIL La transaction DDE a echoué
- ERROR_NO_ASSOCIATION Il n'y a pas d'application
associée à l'extension du
fichier
- ERROR_ACCESS_DENIED Accès refusé
- ERROR_DLL_NOT_FOUND DLL introuvable
- ERROR_CANCELLED La fonction a demandé à
l'utilisateur d'indiquer
l'emplacement de l'application
mais l'utilisateur a annulé la
demande
- ERROR_NOT_ENOUGH_MEMORY Mémoire insuffisante
- ERROR_SHARING_VIOLATION Une violation de partage a été
rencontrée
@Comment Exemples d'utilisation :
//Pour une application
if ShellExecuteAndWait(
Application.Handle,
'open',
'C:\_DELPHI\CODE_TO_DOC\DCTD_cmd.exe',
'VERIF_SRV.xml',
'X:\SIS_BETA\VERIF_SRV\CODE_TO_DOC\',
SW_SHOWNORMAL,
INFINITE,
ExitCode
) = NO_ERROR then...
//Pour un document
if ShellExecuteAndWait(
Application.Handle,
'print',
'C:\Mes documents\toto.pdf',
'',
'',
SW_SHOWNORMAL,
INFINITE,
ExitCode
) = NO_ERROR then...
-------------------------------------------------------------------------------}
function ShellExecuteAndWait(
vHandle : HWND;
vOperation : string;
vFichier : string;
vParametres : string;
vRepertoire : string;
vAffichage : Integer;
vDuree : DWORD;
var ExitCode : DWORD
) : Cardinal;
var
vSEInfo : TShellExecuteInfo;
begin
//Initialisation de la structure ShellExecuteInfo
ZeroMemory(@vSEInfo, SizeOf(vSEInfo));
//Remplissage de la structure ShellExecuteInfo
vSEInfo.cbSize := SizeOf(vSEInfo);
vSEInfo.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI;
vSEInfo.Wnd := vHandle;
vSEInfo.lpVerb := PChar(vOperation);
vSEInfo.lpFile := PChar(vFichier);
vSEInfo.lpParameters := PChar(vParametres);
vSEInfo.lpDirectory := PChar(vRepertoire);
vSEInfo.nShow := vAffichage;
ExitCode := 1;
//Execution
if ShellExecuteEx(@vSEInfo) then
begin
//ShellExecuteEx OK : attendre la fin du process
Result := WaitForSingleObject(vSEInfo.hProcess, vDuree);
GetExitCodeProcess(vSEInfo.hProcess, ExitCode);
end
else
begin
//ShellExecuteEx KO : renvoyer l'erreur
Result := GetLastError();
end;
CloseHandle(vSEInfo.hProcess);
Result := 0;
end; |
Partager