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
| //------------------------------------------------------------------------------
function TModuleXXXXMainForm.CloseChildProcesses(): Boolean;
type
PBufferWindowTexts = ^TBufferWindowTexts;
TBufferWindowTexts = record
Data: PChar;
Position: PByte;
Size: Integer;
Count: Integer;
end;
function EnumWindowsCallback(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
var
ProcessIdOfWnd: DWORD;
I: Integer;
TextOfWnd: array[0..255] of Char;
WindowText: string;
Len: Integer;
begin
Result := True;
if GetWindowThreadProcessId(hWnd, ProcessIdOfWnd) > 0 then
begin
for I := 0 to TModuleXXXX.ModuleTool.ShellExecuteProcessIdList.Count - 1 do
begin
if ProcessIdOfWnd = TModuleXXXX.ModuleTool.ShellExecuteProcessIdList[I] then
begin
// Que les fenêtres visibles !
if IsWindowVisible(hWnd) then
begin
if GetWindowText(hWnd, TextOfWnd, Length(TextOfWnd)) > 0 then
begin
WindowText := ' - ' + TextOfWnd + string(sLineBreak);
Len := Length(WindowText) * SizeOf(WindowText[1]);
if PBufferWindowTexts(lParam).Position + Len <= PBufferWindowTexts(lParam).Data + PBufferWindowTexts(lParam).Size then
begin
CopyMemory(PBufferWindowTexts(lParam).Position, @WindowText[1], Len);
Inc(PBufferWindowTexts(lParam).Position, Len);
Inc(PBufferWindowTexts(lParam).Count);
end
else
Result := False; // Buffer plein !
end;
end;
end;
end;
end;
end;
var
MsgConfirm: string;
I: Integer;
HandleFunctionProcess: THandle;
BufferWindowTexts: TBufferWindowTexts;
begin
Result := True;
if TModuleXXXX.ModuleTool.IsCitrix() then
begin
if TModuleXXXX.ModuleTool.ShellExecuteProcessIdList.Count >= 1 then
begin
BufferWindowTexts.Count := 0;
BufferWindowTexts.Size := 1024;
GetMem(BufferWindowTexts.Data, BufferWindowTexts.Size);
try
ZeroMemory(BufferWindowTexts.Data, BufferWindowTexts.Size);
BufferWindowTexts.Position := PByte(BufferWindowTexts.Data);
EnumWindows(@EnumWindowsCallback, LPARAM(@BufferWindowTexts));
if BufferWindowTexts.Count > 0 then
begin
MsgConfirm := 'Fermer le Module XXXX et les modules liés ?'+sLineBreak+'Attention, il y a un risque de perte de données dans les modules liés!'+sLineBreak;
if BufferWindowTexts.Count = 1 then
MsgConfirm := MsgConfirm + 'Le module lié : '
else
MsgConfirm := MsgConfirm +Format('Les %d modules liés : ', [TModuleXXXX.ModuleTool.ShellExecuteProcessIdList.Count]);
Result := MessageDlg(MsgConfirm + sLineBreak + BufferWindowTexts.Data, mtWarning, mbYesNo, 0) = mrYes;
if Result then
begin
for I := 0 to TModuleXXXX.ModuleTool.ShellExecuteProcessIdList.Count - 1 do
begin
HandleFunctionProcess := OpenProcess(PROCESS_TERMINATE, False, TModuleXXXX.ModuleTool.ShellExecuteProcessIdList[I]);
if HandleFunctionProcess > 0 then
begin
// Arrêt du process désigné par HandleProcess. Tous ses threads sont également arrétés.
// C'est brutal
if not TerminateProcess(HandleFunctionProcess, 0) then
MessageDlg('Echec de fermeture d''un module lié !', mtError, [mbIgnore], 0);
CloseHandle(HandleFunctionProcess);
end;
end;
end;
end;
finally
FreeMem(BufferWindowTexts.Data);
end;
end;
end;
end; |
Partager