建立互斥 CreateMutex(Nil,false,'工程名字); if GetLastError = ERROR_ALREADY_EXISTS then begin Application.MessageBox('系统已经运行','抱歉',0); Application.Terminate ; Exit; end;
保证可以: 在uses中加入头文件 shellapi在主窗口下声明: function CreateMutex: Boolean; // 全项目公用函数 procedure DestroyMutex; // 全项目公用函数//接着定义 procedure DestroyMutex; begin if Mutex <> 0 then CloseHandle(Mutex); end;function CreateMutex: Boolean; var PrevInstHandle: THandle; AppTitle: PChar; begin AppTitle := StrAlloc(100); StrPCopy(AppTitle, Application.Title); Result := True; Mutex := Windows.CreateMutex(nil, False, AppTitle); if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin Result := False; SetWindowText(Application.Handle, ''); PrevInstHandle := FindWindow(nil, AppTitle); if PrevInstHandle <> 0 then begin if IsIconic(PrevInstHandle) then ShowWindow(PrevInstHandle, SW_RESTORE) else BringWindowToTop(PrevInstHandle); SetForegroundWindow(PrevInstHandle); end; if Mutex <> 0 then Mutex := 0; end; StrDispose(AppTitle); end;//按F7跳到工程文件这样加入这两个函数,重新运行就可以了 begin Application.Initialize; if CreateMutex then //创建句柄,判断此应用程序是否在运行 begin //把创建窗体语句放在这里eg //Application.CreateForm(TMainForm, MainForm); Application.Run; end else DestroyMutex; end;end.如果行了,记得给分啊!!
然后查找有没有你要判断的exe
----------------
简直可以把个人给气得死~~~你今天的程序可以把Form叫做Form1,难道明天你就不可能把另一个程序的窗体叫做Fomr1?
真正唯一的只有路径。
CreateMutex(Nil,false,'工程名字);
if GetLastError = ERROR_ALREADY_EXISTS then
begin
Application.MessageBox('系统已经运行','抱歉',0);
Application.Terminate ;
Exit;
end;
在uses中加入头文件
shellapi在主窗口下声明:
function CreateMutex: Boolean; // 全项目公用函数
procedure DestroyMutex; // 全项目公用函数//接着定义
procedure DestroyMutex;
begin
if Mutex <> 0 then CloseHandle(Mutex);
end;function CreateMutex: Boolean;
var
PrevInstHandle: THandle;
AppTitle: PChar;
begin
AppTitle := StrAlloc(100);
StrPCopy(AppTitle, Application.Title);
Result := True;
Mutex := Windows.CreateMutex(nil, False, AppTitle);
if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
Result := False;
SetWindowText(Application.Handle, '');
PrevInstHandle := FindWindow(nil, AppTitle);
if PrevInstHandle <> 0 then begin
if IsIconic(PrevInstHandle) then
ShowWindow(PrevInstHandle, SW_RESTORE)
else
BringWindowToTop(PrevInstHandle);
SetForegroundWindow(PrevInstHandle);
end;
if Mutex <> 0 then Mutex := 0;
end;
StrDispose(AppTitle);
end;//按F7跳到工程文件这样加入这两个函数,重新运行就可以了
begin
Application.Initialize;
if CreateMutex then //创建句柄,判断此应用程序是否在运行
begin
//把创建窗体语句放在这里eg
//Application.CreateForm(TMainForm, MainForm);
Application.Run;
end
else
DestroyMutex;
end;end.如果行了,记得给分啊!!
这个朋友的我试过了,一个字:强!
即使不同路径,不同文件名都不可以运行两次.服!!
因为我有2个不同的EXE 我第一个A。EXE打开B。EXE 完成后 我再想他发送一个内存映射的消息
所以要判断一下他的程序打开没 所以用这个findwindow ,然后我循环判断找他 找到了才执行发送消息 没找到就一直找
你们有没更好的方法