如何控制我的机器上只有一个project.exe在运行
当第二个打开时提示错误信息。
先谢过
当第二个打开时提示错误信息。
先谢过
解决方案 »
- 关于delphi连接数据库的问题
- 如何用delphi控制excel单元格合并
- 开发软件采用DB2数据库,用DBExpress中的SQLConnection控件连接数据库,但dbexpdb2.dll不支持直接远程连接。
- delphi中 for 语句有没有类试于vb中step的步长?
- 选中listview中的一个item后,当鼠标点击该listview之外的地方时,如何让listview中的item取消选中?
- 关于stringgrid列宽
- 有没办法把系统栏的小图标给隐藏掉啊!各位大哥!~
- 关于从固定目录中获取文件的问题?
- 求知!
- 菜鸟的小问题(关于listbox)(还有些问题呀)
- 如何像QQ那样实现在文本输入框中插入图片?
- 300 分 求一个简单功能。
用CTEATEMUTEX或者其他很多方法都可以
var
hwnd: THandle;
begin
// form1为主窗体的Caption
hwnd:= FindWindow(nil, 'form1');
if hwnd <> 0 then
begin
ShowMessage('asd');
exit;
end;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end;
if hwnd <> 0 then
begin
ShowMessage('提示错误信息');
exit; // 退出
end;
Mutex:HWND;
begin
Application.Initialize;
Application.Title := 'ISI';
Mutex:=CreateMutex(nil,False,'main');
if GetLastError<>ERROR_ALREADY_EXISTS Then
begin
Application.CreateForm(Tform1, form1);
Application.Run;
end
else
begin
Application.MessageBox('程序已经在运行!','提示框',MB_OK+mb_iconwarning);
ReleaseMutex(Mutex);
end;
end.
hPrev: THandle;
pClass: PChar;
pClass := PChar('MainForm名称');
hPrev := FindWindowEx(0, 0 ,pClass, nil);
IF hPrev <> 0 THEN
BEGIN
hPrev := GetWindow(hPrev,GW_HWNDNEXT);
IF IsIconic(hPrev) THEN
ShowWindow(hPrev,SW_RESTORE);
SetForegroundWindow(hPrev);
Exit;
END;
这样就可以了。
上面是我自己编程中常使用的,绝对可行
以下帖一些教程关于这方面的 希望对你有帮助
实现单实例运行的关键是判断前一实例是否存在,Win3.x中运行的程序能获知前一
实例的句柄,从而可以方便地进行判断,但 Windows 95 是抢先式多任务系统,其程序
的前一实例句柄恒为零,所以只有另寻其他办法。目前最有效的办法是通过查看是否有
相同窗口类名的例程存在来进行判断。下面介绍在Delphi中实现的方法。
1、对主窗口程序的改动:
在主窗口(即程序创建的第一个窗口)中interface节加入
const
CM_RESTORE = WM_USER + $1000; {自定义的“恢复”消息}
MYAPPNAME = "My Delphi Program";
并在Form的定义的public节中加入
procedure CreateParams(var Params: TCreateParams); override;
Procedure RestoreRequest(var message: TMessage); message CM_RESTORE;
在implementation节中加入
{指定窗口名称}
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.WinClassName := MYAPPNAME;
end;{处理“恢复”消息}
procedure TForm1.RestoreRequest(var message: TMessage);
begin
if IsIconic(Application.Handle) = TRUE then
Application.Restore
else
Application.BringToFront;
end;经过以上修改,程序的主窗口的类名已经被指定了,这是进行判断的基础。
一般在程序刚开始运行的时候进行判断,所以还要对DPR文件进行修改。begin Application.Initialize;
Application.Title := 'xxx管理系统;
hMutex:=CreateMutex(nil,False,'xxx管理系统');
Ret:=GetLastError;
If Ret<>ERROR_ALREADY_EXISTS Then
Begin
Application.CreateForm(TajMAIN, ajMAIN); Application.Run;
End
Else
Application.MessageBox('已经运行!','注意!',MB_OK);
ReleaseMutex(hMutex);end.
4.
写注册表:
procedure WriteReg(input:word);
var
tempexe:string;
begin
tempexe:=inttostr(input);
try
Regtmp:=TRegistry.Create;
Regtmp.RootKey :=HKEY_LOCAL_MACHINE;
if Regtmp.OpenKey('\SOFTWARE\Mykey\currentexe',true) then
begin
Regtmp.WriteString('curexe',tempexe);
end;
Regtmp.Free;
finally
//
end;
end;
5.
//读注册表
function ReadReg:word;
var
str:string;
begin
try
Regtmp:=TRegistry.Create;
Regtmp.RootKey:=HKEY_LOCAL_MACHINE;
if Regtmp.OpenKey('\SOFTWARE\Mykey\currentexe',true) then
begin
str:= Regtmp.ReadString('curexe');
result:=strtoint(str);
end
else
result:=0;
Regtmp.Free;
finally
//
end;
end;6.
//工程文件里。
uses Windows;
var
MutexHandle: THandle;
begin
MutexHandle := CreateMutex(nil, TRUE, 'Handlename');
if (MutexHandle<>0) and (GetLastError=ERROR_ALREADY_EXISTS) then
begin
CloseHandle(MutexHandle);
Halt;
end;
//..................
Application.Initialize;
Application.Run;
end;
呵呵
无论用哪种方法加到工程文件里,都提示代码中的一些方法没有定义
如:[Error] CltXGP.dpr(24): Undeclared identifier: 'GetWindow'
[Error] CltXGP.dpr(24): Undeclared identifier: 'GW_HWNDNEXT'
[Error] CltXGP.dpr(26): Undeclared identifier: 'ShowWindow'
等。
在使 用(郁闷人)的代码中出现如下错误:
[Error] CltXGP.dpr(14): Undeclared identifier: 'HWND'
[Error] CltXGP.dpr(19): Undeclared identifier: 'CreateMutex'
[Error] CltXGP.dpr(20): Undeclared identifier: 'ERROR_ALREADY_EXISTS'
[Error] CltXGP.dpr(30): Undeclared identifier: 'MB_OK'
[Error] CltXGP.dpr(31): Undeclared identifier: 'ReleaseMutex'
请问如何解决?
procedure TForm1.FormCreate(Sender: TObject);
begin
{搜索数据库看程序是否运行}
if GlobalFindAtom('PROGRAM_RUNNING') = 0 then
{ 假如没有找到,就添加到数据库}
atom := GlobalAddAtom('PROGRAM_RUNNING')
else begin
{ 如果程序已经运行,显示信息并退出程序 }
MessageDlg('程序已经运行!', mtWarning, [mbOK], 0);
Halt;
end;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
{ 退出程序时,从数据表中删除添加的条目 }
GlobalDeleteAtom(atom);
end;
[Error] LogonUT.pas(110): '(' expected but ':=' found
说在GlobalAddAtom('PROGRAM_RUNNING')前需要加'('啊