要判断某个程序是否已经运行,该如何做?(100分)

  • 主题发起人 主题发起人 gonebygone
  • 开始时间 开始时间
G

gonebygone

Unregistered / Unconfirmed
GUEST, unregistred user!
要判断某个程序是否已经运行,该如何做?
 
找程序的句柄啊
 
program DEL3test;

uses
Forms,Windows,SysUtils,
DEL3unit in 'DEL3unit.pas' {Form1};

{$R *.RES}

Var
hMutex:HWND;
Ret:Integer;
begin
Application.Initialize;
Application.Title := 'aaaaaa';
hMutex:=CreateMutex(nil,False,'aaaaaa');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then
Begin
Application.CreateForm(TForm1, Form1);
Application.Run;
End
Else
Application.MessageBox('Run Twice!','Notes!',MB_OK);
ReleaseMutex(hMutex);
end.
 
//判断文件是否正在使用--摘自 cnpack
function IsFileInUse(FName: string): Boolean;
var
HFileRes: HFILE;
begin
Result := False;
if not FileExists(FName) then
Exit;
HFileRes := CreateFile(PChar(FName), GENERIC_READ or GENERIC_WRITE, 0,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
end;
 
下面这段代码把所有的进程名称全列出来了,并放到Memo1里,你要怎么处理看自己了,
另外一定要引用单元 TlHelp32
procedure TForm1.Button2Click(Sender: TObject);
var vh:THandle; vT:TProcessEntry32; s:String;
begin
vH:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if vH>=0 then
begin
if Process32First(vH,vT) then
begin
s:=vT.szExeFile;
Memo1.Lines.Add(s);
While Process32Next(vH,vT) do
begin
s:=vT.szExeFile;
Memo1.Lines.Add(s);
end;
end;
end;
CloseHandle(vH);
end;
 
上面的老兄说的很对,找程序的句柄啊!
 
多人接受答案了。
 
var
MutexHandle: THandle;
begin
MutexHandle := CreateMutex(nil, TRUE, '汉语宝典');
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '程序已经运行...','任远软件', mb_IconHand);
CloseHandle(MutexHandle);
Halt;
end;
end;
 
方法1:创建互斥变量
方法2:创建原子
 
to gonebygone:
你写一个 "多人接受答案了。" 就行了?
不结贴我们怎么得分? [:D]
 
接受答案了.
 
后退
顶部