L'auteur
Gregory Adam Belgique Membre Actif (personne physique) # 0000001121 enregistré le 04/06/2006 Fiche personnelle
Note des membres
pas de note
Contributions > 05 - API et appels systèmes
Fermer une appli dont on connait un handle d'une fenetre
# 0000000427
ajouté le 13/03/2007 19:31:57 et modifié le 14/03/2007
consulté 9129 fois
Niveau
débutant Version(s) Foxpro : VFP 9.0 VFP 8.0 VFP 7.0 VFP 6.0 VFP 5.0 VFP 3.0
Description
Bjr J'ai trouvé le handle d'une appli avec FindWindows() Comment la fermer ET être sur du résultat ? OlivierKiller
Reponse en bas
Code source :
&& On envoie d'abord une demande de fermeture : PostMessage()
&& si le processus ne disparait pas au bout d'un nombre de millisecondes, on utilise TerminateProcess()
?Process_kill_hwnd(hwnd , 2000)
#IFNDEF TRUE
#define TRUE .T.
#define FALSE .F.
#define MAX_INTEGER (0x7fffffff)
#ENDIF
#define WM_CLOSE 0x0010
#define STANDARD_RIGHTS_REQUIRED (0x000F0000)
#define SYNCHRONIZE (0x00100000)
#define PROCESS_ALL_ACCESS bitor (bitor (STANDARD_RIGHTS_REQUIRED, SYNCHRONIZE), 0xFFF)
#define STATUS_WAIT_0 0x00000000
#define STATUS_ABANDONED_WAIT_0 0x00000080
#define STATUS_TIMEOUT 0x00000102
#define WAIT_FAILED (-1)
#define WAIT_OBJECT_0 STATUS_WAIT_0
#define WAIT_ABANDONED STATUS_ABANDONED_WAIT_0
#define WAIT_TIMEOUT STATUS_TIMEOUT
#define TIMELAPSE_LOOP 100
function Process_kill_hwnd(hwnd , Grace)
declare long OpenProcess in win32api ;
long dwDesiredAccess, integer bInheritHandle, long pid
declare integer CloseHandle in win32api long Handle
declare long WaitForSingleObject in win32api ;
long handle, long MilliSeconds
declare integer TerminateProcess in Kernel32.dll long , long
declare integer PostMessage in user32.dll long , long , long , long
declare long GetWindowThreadProcessId in user32.dll long , long @
local Success
Success = TRUE
local Done, ProcessId, ProcessHandle, WaitExitStatus
Done = FALSE
ProcessId = 0
grace = iif (empty (m.grace), 5000, m.grace)
do case
case !m.Success
&& (1) Send a quit
case empty (PostMessage(m.hwnd , WM_CLOSE, 0, 0))
&& assert FALSE && not there
Done = TRUE
&& (2) get process Id
case empty (GetWindowThreadProcessId(m.hwnd , @m.ProcessId))
&& assert FALSE && not there
Done = TRUE
&& (3) Open the process
otherwise
ProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, m.ProcessId)
endcase
do while m.Success and !m.Done
WaitExitStatus = WaitForSingleObject(m.ProcessHandle, TIMELAPSE_LOOP)
do case
case m.WaitExitStatus = WAIT_OBJECT_0
&& ok, gone
Done = TRUE
case m.WaitExitStatus = WAIT_TIMEOUT
Grace = m.Grace - TIMELAPSE_LOOP
do case
case m.Grace > 0
loop
&& force terminate
case empty (TerminateProcess(m.ProcessHandle, 0))
Success = FALSE
assert FALSE
endcase
otherwise
pp=getlasterror()
?LastError_Format(m.pp)
assert FALSE
Success = FALSE
endcase
enddo
= empty (m.ProcessHandle) or !empty (CloseHandle(m.ProcessHandle))
return m.Success
endfunc
*---------------------------------------------------------------------------
function LastError_Format(LastError)
declare Integer FormatMessage in win32api ;
Integer dwFlags, ;
string @lpSource, ;
Integer dwMessageId, ;
Integer dwLanguageId, ;
string @lpBuffer, ;
Integer nSize, ;
Integer
local n, buf
buf = space (1024)
#define FORMAT_MESSAGE_ALLOCATE_BUFFER 0x00000100
#define FORMAT_MESSAGE_IGNORE_INSERTS 0x00000200
#define FORMAT_MESSAGE_FROM_STRING 0x00000400
#define FORMAT_MESSAGE_FROM_HMODULE 0x00000800
#define FORMAT_MESSAGE_FROM_SYSTEM 0x00001000
#define FORMAT_MESSAGE_ARGUMENT_ARRAY 0x00002000
#define FORMAT_MESSAGE_MAX_WIDTH_MASK 0x000000FF
n = FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, ;
0, ;
m.LastError, ;
0, ;
@m.buf, ;
len (m.buf), ;
0 ;
)
local msg
msg = 'Error ' + ltrim (str (m.LastError, 18, 0))
do case
case !empty (m.n)
msg = m.msg + ' : ' + left (m.buf, m.n)
endcase
return m.msg
#undefine FORMAT_MESSAGE_ALLOCATE_BUFFER
#undefine FORMAT_MESSAGE_IGNORE_INSERTS
#undefine FORMAT_MESSAGE_FROM_STRING
#undefine FORMAT_MESSAGE_FROM_HMODULE
#undefine FORMAT_MESSAGE_FROM_SYSTEM
#undefine FORMAT_MESSAGE_ARGUMENT_ARRAY
#undefine FORMAT_MESSAGE_MAX_WIDTH_MASK
endfunc
*-------------------------------------------------------------------------------
Commentaires
Aucun commentaire enregistré ...