Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2002.08.19;
Скачать: [xml.tar.bz2];

Вниз

Помогите определить запущен файл или нет только по его имени?   Найти похожие ветки 

 
Bart   (2002-08-08 13:30) [0]

Помогите определить запущен файл или нет только по его имени? поиск по заголовку не усраивает :(


 
Turalyon   (2002-08-08 13:32) [1]

Ищи по списку процессов


 
Bart   (2002-08-08 13:40) [2]

Tutalyon, а примерчик не кинешь?


 
Alx2   (2002-08-08 13:48) [3]


{
ProcessViewer Unit V1.0
by Leo, March 2001

See the file ProcessViewer.txt for informations about the properties and methods.
}

unit ProcessViewer;

interface

uses
Windows, Dialogs, SysUtils, Classes, ShellAPI, TLHelp32, Forms;

const
SleepForReCheck=5000;

type TProcessInfo=record
FileName: string;
Caption: string;
Visible: boolean;
Handle: DWord;
PClass: string;
ThreadID: DWord;
PID: DWord;
end;


var
DateiList,CaptionList,VisibleList,HandleList,ClassList,ThreadIdList,PIDList: TStringList;
ProcessInfo: array of TProcessInfo;

function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool; stdcall;
function KillProcessByPID(PID: DWord): boolean;
function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
procedure GetProcessList;
function GetFileNameFromHandle(Handle: hwnd):string;
function IsFileActive(FileName: String): boolean;

implementation

procedure GetProcessList;
var
i,Laenge: integer;
begin
DateiList.Clear;
HandleList.Clear;
ClassList.Clear;
CaptionList.Clear;
VisibleList.Clear;
ThreadIdList.Clear;
PIDList.Clear;
EnumWindows(@EnumWindowsProc, 0);
Laenge:=DateiList.Count;
SetLength(ProcessInfo,Laenge);
for i:=0 to Laenge-1 do
begin
DateiList[i]:=UpperCase(DateiList[i]);
with ProcessInfo[i] do
begin
FileName:=DateiList[i];
Caption:=CaptionList[i];
Visible:=VisibleList[i]="1";
Handle:=StrToInt64(HandleList[i]);
PClass:=ClassList[i];
ThreadID:=StrToInt64(ThreadIdList[i]);
PID:=StrToInt64(PIDList[i]);
end;
end;
end;

function IsFileActive(FileName: String): boolean;
var
i: integer;
begin
result:=false;
if FileName="" then exit;
GetProcessList;
FileName:=UpperCase(ExtractFileName(FileName));
for i:=0 to Length(ProcessInfo)-1 do
begin
if Pos(FileName,ProcessInfo[i].FileName)>0 then
begin
result:=true;
break;
end;
end;
end;

function GetFileNameFromHandle(Handle: hwnd):string;
var
PID: DWord;
aSnapShotHandle: THandle;
ContinueLoop: Boolean;
aProcessEntry32: TProcessEntry32;
begin
GetWindowThreadProcessID(Handle, @PID);
aSnapShotHandle := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
aProcessEntry32.dwSize := SizeOf(aProcessEntry32);
ContinueLoop := Process32First(aSnapShotHandle, aProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
if aProcessEntry32.th32ProcessID = PID then
begin
result:=aProcessEntry32.szExeFile;
break;
end;
ContinueLoop := Process32Next(aSnapShotHandle, aProcessEntry32);
end;
CloseHandle(aSnapShotHandle);
end;

function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): Bool;
var
Capt,Cla: array[0..255] of char;
Datei: string;
ident: dword;
begin
GetWindowText(hWnd, Capt, 255);
GetClassName(hwnd,Cla,255);
ThreadIdList.Add(IntToStr(GetWindowThreadProcessId(hwnd,nil)));
Datei:=GetFileNameFromhandle(hwnd);
DateiList.Add(Datei);
HandleList.Add(IntToStr(HWnd));
if IsWindowVisible(HWnd) then VisibleList.Add("1") else VisibleList.Add("0");
ClassList.Add(Cla);
CaptionList.Add(Capt);
GetWindowThreadProcessId(StrToInt(HandleList[HandleList.Count-1]),@ident);
PIDList.Add(IntToStr(ident));
Result:=true;
end;

function KillProcessByPID(PID : DWord): boolean;
var
myhandle : THandle;
i: integer;
begin
myhandle := OpenProcess(PROCESS_TERMINATE, False, PID);
TerminateProcess(myhandle, 0);
for i:=0 to SleepForReCheck do Application.ProcessMessages; //Genug Zeit geben
GetProcessList;
Result:=PIDList.IndexOf(IntToStr(PID))=-1;
end;

function KillProcessByFileName(FileName: string; KillAll: boolean): boolean;
var
i: integer;
FileFound: boolean;
begin
result:=false;
if FileName="" then exit;
FileName:=UpperCase(ExtractFileName(FileName));
result:=true;
GetProcessList;
if KillAll then
begin
//Kill all
FileFound:=false;
repeat
GetProcessList;
FileFound:=false;
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])>0 then
begin
FileFound:=true;
break;
end;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt64(PIDList[i])) then
begin
result:=false;
exit;
end;
end;
until not FileFound;
end else
begin
//Kill one
for i:=0 to DateiList.Count-1 do
begin
if Pos(Filename,DateiList[i])>0 then break;
end;
if i<DateiList.Count then
begin
if not KillProcessByPID(StrToInt64(PIDList[i])) then
begin
result:=false;
exit;
end;
end;
end;
end;

initialization
DateiList:=TStringList.Create;
HandleList:=TStringList.Create;
ClassList:=TStringList.Create;
CaptionList:=TStringList.Create;
VisibleList:=TStringList.Create;
ThreadIdList:=TStringList.Create;
PIDList:=TStringList.Create;

finalization
DateiList.Free;
HandleList.Free;
ClassList.Free;
CaptionList.Free;
VisibleList.Free;
ThreadIdList.Free;
PIDList.Free;

end.


 
Turalyon   (2002-08-08 13:52) [4]

Ща попробую... надо будет только немного его подрехтовать...

uses TlHelp32;

...

Var
Snap : THandle;
PE : TProcessEntry32;

...

MyFlag := False;
if Snap > 0 then CloseHandle(Snap);
Snap := CreateToolhelp32Snapshot(th32cs_snapprocess, 0);
PE.dwSize := SizeOf(PE);
if Process32First(Snap,PE) then;
repeat
if PE.szExeFile = MyFileName then MyFlag := True;
until not Process32Next(Snap,PE);

if MyFlag /// файл с таким именем запущен...


Учти, только что если у тебя клиент W2k то PE.szExeFile - голый экзешени, если W98 то вместе с путем к нему...
ExtractFileName(PE.szExeFile) тебе поможет для универсализации...

ЗЫ Могу тебе замылить pdf-чик по использованию всей этой библиотеки... (сам по этому делу разбирался). Вот


 
Bart   (2002-08-08 14:44) [5]

Спасибо, спасли!



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

Форум: "Основная";
Текущий архив: 2002.08.19;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.47 MB
Время: 0.006 c
4-60897
Freezer
2002-06-14 16:00
2002.08.19
Invalid window handle


14-60827
AL2002
2002-07-24 16:23
2002.08.19
Always, AllDays, TmpX etc.


1-60728
MikeFW
2002-08-06 11:11
2002.08.19
Как организовать сортировку в TreeView?


4-60891
Керик
2002-06-09 05:36
2002.08.19
Как сделать окно активным


6-60787
DenKop
2002-06-03 01:26
2002.08.19
Список файлов





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский