请问如何禁止程序二次运行?确保只有一个程序在运行。
解决方案 »
- 怎么从繁多的文件中判断哪个文件名是像####-##-##如2012-03-03这样的格式?
- delphi access 两个表的结构相同,偶想把表1的几列内容考到表2中,不知道该如何实现?
- 问一个关于卸载OCX再安装这个OCX控件的简单问题,马上给分!
- 你用什么信用卡?(要去申请信用卡了,但不知道哪个银行的卡比较好,大家提提意见!)
- 关于DELPHI与ORACLE数据库类型匹配问题
- 如何用socket传送图形文件??
- delphi6.0或5.0,甚至4.0的控件能否在7.0中使用?
- 关闭程序
- 比较急的问题,up有分
- 联想笔记本g510换那个固态和内存条比较好!
- 急急急!! 散分了哦! 哈哈
- 有没人会啊~~~~~~~~~~~~~
HMutex: THandle;
Begin
// check if mutex already exists
HMutex := CreateMutex(nil, False, 'This is Budded Single App Demo');
if HMutex <> 0 then
if WaitForSingleObject(HMutex, 0) <> WAIT_TIMEOUT then // Owns the Mutex
try // try }
AppRun; // run your application
finally // wrap up finally
ReleaseMutex(HMutex);
end; // end try finally
end.
AllowedInstances = 1;var
MyAppName : Array[0..255] of Char;
MyClassName : Array[0..255] of Char;
NumFound : Integer;
LastFound : HWnd;
MyPopup : HWnd;function LookAtAllWindows(Handle: HWND; Temp: LongInt): BOOL; stdcall;
var
WindowName : Array[0..255] of Char;
ClassName : Array[0..255] of Char;
begin
// Go get the windows class name
if GetClassName(Handle,ClassName,SizeOf(ClassName)) > 0 then
// Is the window class the same?
if StrComp(ClassName,MyClassName) = 0 then
// Get its window caption
if GetWindowText(Handle,WindowName,SizeOf(WindowName)) > 0 then
// Does this have the same window title?
if StrComp(WindowName,MyAppName)=0 then
begin
inc(NumFound);
// Are the handles different?
if Handle <> Application.Handle then
// Save it so we can bring it to the top later.
LastFound := Handle;
end;
Result:=true;
end;begin
//防止程序多实例运行
NumFound := 0; LastFound := 0;
// First, determine what this application's name is
GetWindowText(Application.Handle,MyAppName,SizeOf(MyAppName));
// Now determine the class name for this application
GetClassName(Application.Handle,MyClassName,SizeOf(MyClassName));
// Now count how many others out there are Delphi apps with this title
EnumWindows(@LookAtAllWindows,0);
if NumFound > AllowedInstances then
// There is another instance running, bring it to the front!
begin
MyPopup := GetLastActivePopup(LastFound);
// Bring it to the top in the ZOrder
BringWindowToTop(LastFound);
// Is the window iconized?
if IsIconic(MyPopup) then
// Restore it to its original position
ShowWindow(MyPopup,SW_RESTORE)
else
// Bring it to the front
SetForegroundWindow(MyPopup);
end
else
// None running - allow this instance to continue
begin
Application.Initialize;
Application.CreateForm(TDataModuleMain, DataModuleMain);
Application.CreateForm(TFrmMain, FrmMain);
Application.Run;
end ; end.
了解否?