在WINDOWS98,WINDOWS2000中如何让程序只能运行一次?(100分)

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

BLUE_SKY

Unregistered / Unconfirmed
GUEST, unregistred user!
在WINDOWS98,WINDOWS2000中如何让程序只能运行一次?曾看过一种使用WINAPI方法,但
在WINDOWS98中有效,在WINDOWS2000或WINDOWS NT中无效。谁有更好的方法呢?

 
program xxx;

....

var
OnlyRunOnce : THandle;

begin
OnlyRunOnce := CreateMutex(nil, True, 'XXX_CENTER');
if GetLastError = 183 then Halt;
Application.Initialize;
Application.Title := 'XXXXXX系统';
Application.CreateForm(TMainForm, MainForm);
Application.Run;
CloseHandle(OnlyRunOnce);
end.
 
在工程的source中加入:

var
handle:Integer;
{$R *.RES}
begin
handle:=findwindow('TmainForm',Nil);//Tmainform主表单名称
if handle<>0 then begin
messagebox(0,'本程序正在运行中,不能再次运行!','运行',0);
halt;
end;
 
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('程序已经运行!','提示!',MB_OK);
ReleaseMutex(hMutex);
end;
 
这样的帖子多的很,为什么不找以前的帖子了?
 
我有一个例子,在这里,你可以去看看,D5D6都可以用的。
http://www.lkgarden.com/lfpsoft/
 
随便一搜索就一堆!
 
多人接受答案了。
 
后退
顶部