有个a.exe程序已经执行,如果在打开a.exe程序时,由于已经存在了,所以希望第二次打开a.exe不执行了,请问如何处理?
我自己写了一个可是不行,请指教!
program a;uses
Forms,
SysUtils,
Windows,
Mainfromunit in 'Mainfromunit.pas' {MainForm},
{$R *.res}
var
RvHandle: hWnd;begin
RvHandle := FindWindow('a', nil);
if RvHandle > 0 then Exit;
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
我自己写了一个可是不行,请指教!
program a;uses
Forms,
SysUtils,
Windows,
Mainfromunit in 'Mainfromunit.pas' {MainForm},
{$R *.res}
var
RvHandle: hWnd;begin
RvHandle := FindWindow('a', nil);
if RvHandle > 0 then Exit;
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
解决方案 »
- 谁推荐本delphi方面的书啊,最好有案例的
- 请教一个椭圆算法
- Delphi7中emptyparam不能用?Delphi中可以?
- 关于DriveComboBox问题???当我选择a盘时a盘没有软盘,弹出i/o错误!我用try except捕获异常不行啊~求救!
- 谁能提供完整的键盘键值,有分相送。
- 想把xml转换成xslt用于浏览?
- 测试0.0001的帖子
- Delphi中有没有"Proxies.pas"这个文件。
- 高手请进,关于隐藏窗体
- 已知外部应用程序的窗口句柄,如何能得到窗口内所有图型按钮的句柄和名称??
- Borland要起诉我了,大家有这个经验吗?是个程序员也要起诉,郁闷,不理解啊
- Delphi中如何快速判断Sql Server服务器是否存在,或者是否正在运行?
foreach(Process p in ps)
{
遍历,寻找有没有a程序
}
var ZAppName:array[0..127] of char;
Hold:string;
Found:HWND;
begin
Hold:=Application.Title;
Application.Title:='OnlyOne'+IntToStr(HInstance);//暂时修改窗口标题
StrPCopy(ZAppName,Hold); //原窗口标题
Found:=FindWindow(nil,ZappName); //查找窗口
Application.Title:=Hold;
if Found<>0 then //找到则激活已运行的程序并结束自身
begin
ShowWindow(Found,SW_RESTORE);
Application.Terminate;
end;
end;
Forms,SysUtils,Windows,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}
var
FindHID: HWND;
MoudleName: string; function EnumWndProc(hWnd:THandle; Param:Cardinal): Boolean; stdcall;
var
ClassName, WinMoudleName:string;
WinInstance: THandle;
begin
Result := true;
SetLength(ClassName, 1000);
GetClassName(hWnd, pChar(ClassName), Length(ClassName));//获得当前遍历窗口的类名
ClassName := pChar(ClassName);//在字符串后加结束符,确定字符串结束
if ClassName = TForm1.ClassName then//比较
begin
WinInstance := GetWindowLong(hWnd, GWL_HINSTANCE);//获得当前遍历窗口的实例
SetLength(WinMoudleName, 1000);
GetModuleFileName(WinInstance, pChar(WinMoudleName), Length(WinMoudleName)); //获得当前遍历窗口的程序文件名
WinMoudleName := pChar(WinMoudleName);
if WinMoudleName = MoudleName then//MoudleName为工程全局变量,自身程序的文件名
begin
FindHid := hWnd;//FindHid为工程全局变量保存找到的句炳
Result := False;//找到以后就结束遍历
end;
end;
end;
function OneInstanceOnly(MutexName: string): Boolean;
var
Mutex: HWND;
begin
Result := True;
Mutex := CreateMutex(nil, False, PAnsiChar(MutexName)); //CreateMutex建立互斥对象,并且给互斥对象起一个唯一的名字。
if WaitForSingleObject(Mutex, 0) = WAIT_TIMEOUT then//程序已在运行
Result := False;
end;
begin
if not OneInstanceOnly('你的程序名,最好取名唯一点') then
begin
SetLength(MoudleName,1000);
GetModuleFileName(HInstance, pChar(MoudleName), Length(MoudleName)); //获得自己程序文件名
MoudleName := pChar(MoudleName); //在字符串后加结束符,确定字符串结束
EnumWindows(@EnumWndProc, 0); //调用枚举函数, @EnumWndProc 是处理找到窗口的回调函数。
if FindHid <> 0 then
SetForegroundWindow(FindHid);
Exit;
end; Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
program Project1;uses
Forms,
Windows,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}
var
hMutex:hWnd;
begin
Application.Initialize;
Application.Title:='test';
hMutex:=CreateMutex(nil,false,'test');
if GetLastError<>Error_Already_Exists then
begin
Application.CreateForm(TForm1, Form1);
Application.Run;
end
else
begin
Application.MessageBox('本程序只允许同时运行一个','Error');
ReleaseMutex(hMutex);
end;
end.