新写了一个程序,想实现如下功能:
当程序运行时,检查是否有该程序的实例在运行,如果有,则激活此实例;否则运行程序!
当程序运行时,检查是否有该程序的实例在运行,如果有,则激活此实例;否则运行程序!
解决方案 »
- delphi2010 TSQLConnection控件的使用方法
- 百思不得其解,菜单弹出后, 点击非菜单区域, 弹出的菜单不能自动消失? 高分求解!!
- Tdbgrid为什么不能提交修改.
- 各位高手,有谁用Com实现过MDI窗体
- 问个简单问题,大家不要笑话
- BDE连接ACCESS,怎么去掉提示输入登陆数据库的用户名和密码??
- IE 的工具栏~~知道的请进~~~~~~~~~~~
- 如何设置动态报表?
- 我们写的系统,每次到了业务繁忙的时候,服务器就停住,客户端出现‘dead lock’,也就是1—2分钟,求救!
- 如何成为编程高手
- 再请帮忙测试网站
- 急!接口问题(IDispatch)!发分一百,只给一个人!如不够可再加!
这个上面有说明
还有可以在GOOGLE里搜索啊,比问要快多了
CreateMutex
FindWindow
SetForgroundWindow
自己写写,我写的api函数不一定名字全正确
MI_QUERYWINDOWHANDLE = 1;
MI_RESPONDWINDOWHANDLE = 2; MI_ERROR_NONE = 0;
MI_ERROR_FAILSUBCLASS = 1;
MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup.
// Value will be one or more of the MI_ERROR_* error flags.
function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const
UniqueAppStr = 'DDG.I_am_the_Eggman!';var
MessageId: Integer;
WProc: TFNWndProc;
MutHandle: THandle;
MIError: Integer;function GetMIError: Integer;
begin
Result := MIError;
end;function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
Longint; stdcall;
begin
Result := 0;
// If this is the registered message...
if Msg = MessageID then
begin
case wParam of
MI_QUERYWINDOWHANDLE:
// A new instance is asking for main window handle in order
// to focus the main window, so normalize app and send back
// message with main window handle.
begin
if IsIconic(Application.Handle) then
begin
Application.MainForm.WindowState := wsNormal;
Application.Restore;
end;
PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE,
Application.MainForm.Handle);
end;
MI_RESPONDWINDOWHANDLE:
// The running instance has returned its main window handle,
// so we need to focus it and go away.
begin
SetForegroundWindow(HWND(lParam));
Application.Terminate;
end;
end;
end
// Otherwise, pass message on to old window proc
else
Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
end;procedure SubClassApplication;
begin
// We subclass Application window procedure so that
// Application.OnMessage remains available for user.
WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
Longint(@NewWndProc)));
// Set appropriate error flag if error condition occurred
if WProc = nil then
MIError := MIError or MI_ERROR_FAILSUBCLASS;
end;procedure DoFirstInstance;
// This is called only for the first instance of the application
begin
// Create the mutex with the (hopefully) unique string
MutHandle := CreateMutex(nil, False, UniqueAppStr);
if MutHandle = 0 then
MIError := MIError or MI_ERROR_CREATINGMUTEX;
end;procedure BroadcastFocusMessage;
// This is called when there is already an instance running.
var
BSMRecipients: DWORD;
begin
// Prevent main form from flashing
Application.ShowMainForm := False;
// Post message to try to establish a dialogue with previous instance
BSMRecipients := BSM_APPLICATIONS;
BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
@BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
Application.Handle);
end;procedure InitInstance;
begin
SubClassApplication; // hook application message loop
MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
if MutHandle = 0 then
// Mutex object has not yet been created, meaning that no previous
// instance has been created.
DoFirstInstance
else
BroadcastFocusMessage;
end;initialization
MessageID := RegisterWindowMessage(UniqueAppStr);
InitInstance;
finalization
// Restore old application window procedure
if WProc <> Nil then
SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
if MutHandle <> 0 then CloseHandle(MutHandle); // Free mutex
end.
begin
Application.CreateForm(TFormMain, FormMain);
Application.CreateForm(TForm2, Form2);
Application.CreateForm(TFormOnLine, FormOnLine);
Application.CreateForm(TFormReturn, FormReturn);
Application.CreateForm(TFormAlarm, FormAlarm);
Application.Run;
end
else
begin
ShowMessage('該系統已經運行,不能再打開另外一個了');
ReleaseMutex(hMutex);
end;
我們程式里自己寫的一個.
forms,windows,
Unit1 in 'Unit1.pas' {Form1};
{$R *.RES}
begin
CreateMutex(nil, True, 'MyAppClass');
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, '不理你了!', '你运行两次了啦!' ,MB_ICONERROR);
Halt;
end; Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
“最好用CreateMutex”
var
mutex: THandle;
begin
result := true;
mutex := CreateMutex(nil, true, PCHAR(GUID));
if GetLastError = ERROR_ALREADY_EXISTS then
result := false;
if mutex <> 0 then ReleaseMutex(mutex);
end;