问:怎么样通过进程来判断一个程序是否运行.(200分)

  • 主题发起人 主题发起人 Bigsun
  • 开始时间 开始时间
B

Bigsun

Unregistered / Unconfirmed
GUEST, unregistred user!
不要通过Title来判断的.
 
hnd: Integer;
initialization //·ÀÖ¹¶à´ÎÔËÐгÌÐò¡£
hnd := CreateMutex(nil, True, 'RelTest.exe');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Application.MessageBox('¸Ã³ÌÐòÒѾ­ÔËÐС£', 'RelTest', mb_ok + MB_ICONWARNING);
Halt;
end;

finalization
if hnd <> 0 then CloseHandle(hnd);
 
上面的代码好像只能判断自己是不是已经运行,要想判断别人的窗口是否运行,好像不可以啊
试试这个吧,好像没有别的办法了
var
hnd: Integer;
begin
hnd := findwindow(nil,'你想找的窗口的title');
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=630036
解决了。
 
1.通过类来判断。
Wnd := FindWindow('IEFrame', nil);
if Wnd = 0 then
begin
MessageDlg ('No Running instance of Internet Explorer!',mtError, [mbOK], 0);
end;

2.枚举所有进程和模块:
implementation
uses psapi;
{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
type
integer = DWORD; // different versions of psapi.pas floating around
var
i,j,pidNeeded,modNeeded : Integer;
PIDList : array[0..1000] of Integer; // 1000 should be enough
MODList : array[0..1000] of HInst;
PIDName : array [0..MAX_PATH - 1] of char;
MODName : array [0..MAX_PATH - 1] of char;
PH : THandle;
begin

// fill an array with process ids
if not enumprocesses (@PIDList, 1000, pidNeeded) then
begin
ListBox1.Items.Add('Need psapi.dll');
exit;
end;

// now open each process and its modules
for i := 0 to (pidNeeded div sizeof (Integer)- 1) do
begin
// get a handle to the process
PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,
PIDList);

if PH <> 0 then
begin
// print the process name
if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) > 0 then
begin
ListBox1.Items.Add('process : ' + PIDName);

// fill an array of modules associated with this process
if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0;

// print the modules in the list
for j := 0 to (modNeeded div sizeof (hInst) - 1) do
if GetModuleFileNameEx (PH, MODList[j], MODName,sizeof(MODName)) > 0 then
begin
ListBox1.Items.Add(' module: ' + MODName);
if LowerCase(trim(ExtractFileName(ModName))) = LowerCase('HKTEST.dll') then ShowMessage('Keyboard hook already running');
end;
if PH > 0 then CloseHandle(PH);
end;
end;
end;

end;
 
后退
顶部