Scintilla - Terminate Process

Il Client di Scintilla si deve occupare anche di terminare le applicazioni presenti in una determinata lista. Se vuoi provarlo qui trovi client server. Il cuore della vicenda è rintracciare in memoria la classe della finestra, la caption o più semplicemente l'eseguibile specificato. E' importante risalire alla finestra principale dall' handle dell'oggetto di partenza. Una volta rintracciata la MainWindow è possibile terminarla.


procedure TScintillaDebug.UpdateInfo(W: HWnd; Refresh: Boolean);
var
  B: array[0..255] of Char;
  D: array[0..255] of Char;
  C: array[0..16384] of Char;
  PID: UINT;
  WndParent: HWND;
  TmpWndParent: HWND;
  i: Integer;
  buf:  Array [0..255] of char;
  ProcHandle: THandle;
  sss: string;
  ClassName, aCaption: string;
begin


    if LoadingDenyText then Exit;
    if Target = W then Exit;

    Target:=W;
    SendMessage( W, WM_GETTEXT, 256, integer(@C));
    GetWindowThreadProcessID(W,@PID);

    ScintillaDebug.CleanPnl1;
    ScintillaDebug.edtText.Text:=C;

    ScintillaDebug.edtProcess.Text:=ScintillaDebug.IntToText(PID);
    ScintillaDebug.edtInstance.Text:=ScintillaDebug.IntToText(GetWindowLong(W,GWL_HINSTANCE));
    ScintillaDebug.edtHandle.Text:=ScintillaDebug.IntToText(W);
    ScintillaDebug.edtParent.Text:=ScintillaDebug.IntToText(GetParent(W));
    ScintillaDebug.edtControlID.Text:=ScintillaDebug.IntToText(GetWindowLong(W,GWL_ID));
    ScintillaDebug.edtFunction.Text:=ScintillaDebug.IntToText(GetWindowLong(W,GWL_WNDPROC));
    if GetParent(W)=0 then ScintillaDebug.edtMenu.Text:=ScintillaDebug.IntToText(GetMenu(W))
    else ScintillaDebug.edtMenu.Text:=ScintillaDebug.IntToText(0);

    ScintillaDebug.edtModuleHandle.Text:=ScintillaDebug.IntToText(GetClassLong(W,GCL_HMODULE));

    // class info

    GetClassName( W, Buf, 256);
    ScintillaDebug.edtClass.Text:=string(Buf);

    (* Cerco la Main Window*)
    WndParent:=GetWindowLong(W,GWL_HWNDPARENT);
    if WndParent = 0 then begin
      ScintillaDebug.CleanPnl2;
      WndParent := W;
    end;

    tmpWndParent:=WndParent;
    while tmpWndParent<>0 do
    begin
      tmpWndParent:=GetWindowLong(WndParent,GWL_HWNDPARENT);
      if tmpWndParent <> 0 then begin
        WndParent:= tmpWndParent;
      end;
    end;
    TargetMainWindow := WndParent;

    if (WndParent <> 0) then begin
      ScintillaDebug.CleanPnl2;

            // add "Class name" column
            GetClassName(WndParent,B,SizeOf(B));
            ClassName := string(B);
            ScintillaDebug.edt2.Text:= ClassName;

            for I := 0 to lstDenyClass.Count -1 do begin

              if lstDenyClass.Strings [i] = lowercase(ClassName) then begin
                sss := GetPathFromPID (PID);
                ScintillaDebug.memo2.lines.Add ('Class: '+ lowercase(ClassName) + ' PID: ' + IntToStr(PID) + ' exe: ' + sss);
                if ScintillaDebug.rg1.ItemIndex = 1 then begin
                  ProcHandle := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, PID);
                  TerminateProcess(ProcHandle,0);
                  Exit;
                end;

              end;

            end;

            // add "Text" column
            SendMessage(WndParent,WM_GETTEXT,SizeOf(D),integer(@D));
            aCaption :=  string(D);
            ScintillaDebug.edt1.Text:= aCaption;
            for I := 0 to lstDenyCaption.Count -1 do begin

              if Pos (lstDenyCaption.Strings [i] , lowercase(aCaption),1)  <> 0 then begin
                sss := GetPathFromPID (PID);
                ScintillaDebug.memo2.lines.Add ('Caption: '+ lowercase(aCaption) + ' PID: ' + IntToStr(PID)+ ' exe: ' + sss);
                if ScintillaDebug.rg1.ItemIndex = 1 then begin
                  ProcHandle := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, PID);
                  TerminateProcess(ProcHandle,0);
                  Exit;
                end;
              end;

            end;


                exepath:= GetPathFromPID (PID);
                ScintillaDebug.edtExePath.text := exepath;
                UpdateExeInfo(exepath,W,PID);

    end;
end;

Commenti

Post più popolari