Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.05.06;
Скачать: CL | DM;

Вниз

Подскажите как получить PID процеса по заголовку окна ?   Найти похожие ветки 

 
dreamse   (2007-03-13 15:59) [0]

Приветствую. Необходимо узнать как получить Pid процеса ( для его завершения ) по заголовку окна. т.е получаем Handle заголовка ... дальше застрял :(

Есть решение ?


 
Сергей М. ©   (2007-03-13 16:21) [1]


> получаем Handle заголовка


У "заголовка" нет handle.

И как ты его умудрился получить - одному богу известно)


> Необходимо узнать как получить Pid процеса ( для его завершения
> )


Сам придумал ? Иль кто подсказал ?


 
Rouse_ ©   (2007-03-13 16:29) [2]

GetWindowThreadProcessId()


 
dreamse   (2007-03-13 16:29) [3]

Решил проблему специально для тебя привожу код чтобы заткнуть :)


function IsWinXP: Boolean;
begin
 Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and
   (Win32MajorVersion = 5) and (Win32MinorVersion = 1);
end;

function IsWin2k: Boolean;
begin
 Result := (Win32MajorVersion >= 5) and
   (Win32Platform = VER_PLATFORM_WIN32_NT);
end;

function IsWinNT4: Boolean;
begin
 Result := Win32Platform = VER_PLATFORM_WIN32_NT;
 Result := Result and (Win32MajorVersion = 4);
end;

function IsWin3X: Boolean;
begin
 Result := Win32Platform = VER_PLATFORM_WIN32_NT;
 Result := Result and (Win32MajorVersion = 3) and
   ((Win32MinorVersion = 1) or (Win32MinorVersion = 5) or
   (Win32MinorVersion = 51));
end;

function RunningProcessesList(const List: TStrings; FullPath: Boolean): Boolean;

 function ProcessFileName(PID: DWORD): string;
 var
   Handle: THandle;
 begin
   Result := "";
   Handle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, PID);
   if Handle <> 0 then
   try
     SetLength(Result, MAX_PATH);
     if FullPath then
     begin
       if GetModuleFileNameEx(Handle, 0, PChar(Result), MAX_PATH) > 0 then
         SetLength(Result, StrLen(PChar(Result)))
       else
         Result := "";
     end
     else
     begin
       if GetModuleBaseNameA(Handle, 0, PChar(Result), MAX_PATH) > 0 then
         SetLength(Result, StrLen(PChar(Result)))
       else
         Result := "";
     end;
   finally
     CloseHandle(Handle);
   end;
 end;

 function BuildListTH: Boolean;
 var
   SnapProcHandle: THandle;
   ProcEntry: TProcessEntry32;
   NextProc: Boolean;
   FileName: string;
 begin
   SnapProcHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   Result := (SnapProcHandle <> INVALID_HANDLE_VALUE);
   if Result then
   try
     ProcEntry.dwSize := SizeOf(ProcEntry);
     NextProc := Process32First(SnapProcHandle, ProcEntry);
     while NextProc do
     begin
       if ProcEntry.th32ProcessID = 0 then
       begin
            // PID 0 is always the "System Idle Process" but this name cannot be
           // retrieved from the system and has to be fabricated.
         FileName := RsSystemIdleProcess;
       end
       else
       begin
         if IsWin2k or IsWinXP then
         begin
           FileName := ProcessFileName(ProcEntry.th32ProcessID);
           if FileName = "" then
             FileName := ProcEntry.szExeFile;
         end
         else
         begin
           FileName := ProcEntry.szExeFile;
           if not FullPath then
             FileName := ExtractFileName(FileName);
         end;
       end;
       List.AddObject(FileName, Pointer(ProcEntry.th32ProcessID));
       NextProc := Process32Next(SnapProcHandle, ProcEntry);
     end;
   finally
     CloseHandle(SnapProcHandle);
   end;
 end;

 function BuildListPS: Boolean;
 var
   PIDs: array[0..1024] of DWORD;
   Needed: DWORD;
   I: Integer;
   FileName: string;
 begin
   Result := EnumProcesses(@PIDs, SizeOf(PIDs), Needed);
   if Result then
   begin
     for I := 0 to (Needed div SizeOf(DWORD)) - 1 do
     begin
       case PIDs[I] of
         0:
            // PID 0 is always the "System Idle Process" but this name cannot be
           // retrieved from the system and has to be fabricated.
           FileName := RsSystemIdleProcess;
         2:
            // On NT 4 PID 2 is the "System Process" but this name cannot be
           // retrieved from the system and has to be fabricated.
           if IsWinNT4 then
             FileName := RsSystemProcess
           else
             FileName := ProcessFileName(PIDs[I]);
         8:
            // On Win2K PID 8 is the "System Process" but this name cannot be
           // retrieved from the system and has to be fabricated.
           if IsWin2k or IsWinXP then
             FileName := RsSystemProcess
           else
             FileName := ProcessFileName(PIDs[I]);
       else
         FileName := ProcessFileName(PIDs[I]);
       end;
       if FileName <> "" then
         List.AddObject(FileName, Pointer(PIDs[I]));
     end;
   end;
 end;
begin
 if IsWin3X or IsWinNT4 then
   Result := BuildListPS
 else
   Result := BuildListTH;
end;

function GetProcessNameFromWnd(Wnd: HWND): string;
var
 List: TStringList;
 PID: DWORD;
 I: Integer;
begin
 Result := "";
 if IsWindow(Wnd) then
 begin
   PID := INVALID_HANDLE_VALUE;
   GetWindowThreadProcessId(Wnd, @PID);
   List := TStringList.Create;
   try
     if RunningProcessesList(List, True) then
     begin
       I := List.IndexOfObject(Pointer(PID));
       if I > -1 then
         Result := List[I];
     end;
   finally
     List.Free;
   end;
 end;
end;

function EnumWindowsProc(Wnd: HWND; lParam: lParam): BOOL; stdcall;
begin
  Result := True;
  if (IsWindowVisible(Wnd) or IsIconic(wnd)) and
    ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
    (GetWindowLong(Wnd, GWL_HWNDPARENT) = GetDesktopWindow)) and
  (GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
    Form1.Listbox1.Items.Add(GetProcessNameFromWnd(Wnd));
end;


Спсаибо всем за помощь :)


 
dreamse   (2007-03-13 16:30) [4]

Пердыдущее сообщение для Сергей М. ©   (13.03.07 16:21) [1]


 
Сергей М. ©   (2007-03-13 16:32) [5]


> Решил проблему


А , ну да, сами себе создаем "проблему" и сами же ее героически преодолеваем, при этом не стесняясь прорекламировать "способ" преодоления проблемы, высосанной из пальца)


 
Сергей М. ©   (2007-03-13 16:34) [6]


> dreamse   (13.03.07 16:30) [4]
>
> Пердыдущее сообщение для Сергей М. ©   (13.03.07 16:21)
> [1]


Не дурак-с) .. понял-с)

И где же здесь "получаем handle заголовка", позвольте осведомиться ?)


 
dreamse   (2007-03-13 16:48) [7]

> Сергей М. ©   (13.03.07 16:34) [6]

function EnumWindowsProc(Wnd: HWND; lParam: lParam): BOOL; stdcall;
begin
 Result := True;
 if (IsWindowVisible(Wnd) or IsIconic(wnd)) and
   ((GetWindowLong(Wnd, GWL_HWNDPARENT) = 0) or
   (GetWindowLong(Wnd, GWL_HWNDPARENT) = GetDesktopWindow)) and
 (GetWindowLong(Wnd, GWL_EXSTYLE) and WS_EX_TOOLWINDOW = 0) then
   Form1.Listbox1.Items.Add(GetProcessNameFromWnd(Wnd));
end;

Wnd

Спасибо большое всем ! Вопрос закрыт.

Сергей М. © Не стоит обижаться, резкий ответ соответствено резко и ответил тебе. Спасибо вопрос закрыт.


 
dreamse   (2007-03-13 16:49) [8]

Handle окна по заголовку. Если изначально неверно сказал не стоит придираться к словам


 
Сергей М. ©   (2007-03-13 16:54) [9]


> dreamse   (13.03.07 16:48) [7]


> Не стоит обижаться,


Да что ты ?!
Да неуже ли я обиделся ?!))


> Если изначально неверно сказал не стоит придираться к словам


Каков вопрос, таково и к нему отношение.



Страницы: 1 вся ветка

Текущий архив: 2007.05.06;
Скачать: CL | DM;

Наверх




Память: 0.5 MB
Время: 0.021 c
2-1176924939
Шурикс
2007-04-18 23:35
2007.05.06
Delphi2007


15-1176018916
Йцукен
2007-04-08 11:55
2007.05.06
схождение Благодатного Огня


1-1172824942
Kirflaer
2007-03-02 11:42
2007.05.06
Как перенести текст из RichEdit с сохранением форматирования


15-1175977895
vasIZmax
2007-04-08 00:31
2007.05.06
Как правильно писать программу?!


15-1176183399
Megabyte
2007-04-10 09:36
2007.05.06
Нынешнее состояние "начинающим"