F
Fenix
Unregistered / Unconfirmed
GUEST, unregistred user!
为了解决重复运行的问题,从网上找到一段代码MultInst.pas,加入project后,编译通过,运行也有效,但当我从Project中去掉MultInst.pas,再进行编译,却总出现这个错误:[Fatal Error] :File not found:'MultInst.dcu',我明明已经把MultInst从project中移掉了,而且没有在任何地方调用啊......在线请教各位大虾,同行,如何解决这个问题.先谢了!运行环境:WINXP,DELPHI7附:MultInst.pas代码//==============================================================================// Unit Name: MultInst// Author : ysai// Date : 2003-05-20// Purpose : 解决应用程序多实例问题// History ://==============================================================================//==============================================================================// 工作流程// 程序运行先取代原有向所有消息处理过程,然后广播一个消息.// 如果有其它实例运行,收到广播消息会回发消息给发送程序,并传回它自己的句柄// 发送程序接收到此消息,激活收到消息的程序,然后关闭自己//==============================================================================unit MultInst;interfaceuses Windows ,Messages, SysUtils, Classes, Forms;implementationconst STR_UNIQUE = '{2BE6D96E-827F-4BF9-B33E-8740412CDE96}'; MI_ACTIVEAPP = 1; //激活应用程序 MI_GETHANDLE = 2; //取得句柄var iMessageID : Integer; OldWProc : TFNWndProc; MutHandle : THandle; BSMRecipients : DWORD;function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint): Longint; stdcall;begin Result := 0; if Msg = iMessageID then begin case wParam of MI_ACTIVEAPP: //激活应用程序 if lParam<>0 then begin //收到消息的激活前一个实例 //为什么要在另一个程序中激活? //因为在同一个进程中SetForegroundWindow并不能把窗体提到最前 if IsIconic(lParam) then OpenIcon(lParam) else SetForegroundWindow(lParam); //终止本实例 Application.Terminate; end; MI_GETHANDLE: //取得程序句柄 begin PostMessage(HWND(lParam), iMessageID, MI_ACTIVEAPP, Application.Handle); end; end; end else Result := CallWindowProc(OldWProc, Handle, Msg, wParam, lParam);end;procedure InitInstance;begin //取代应用程序的消息处理 OldWProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC, Longint(@NewWndProc))); //打开互斥对象 MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, STR_UNIQUE); if MutHandle = 0 then begin //建立互斥对象 MutHandle := CreateMutex(nil, False, STR_UNIQUE); end else begin Application.ShowMainForm := False; //已经有程序实例,广播消息取得实例句柄 BSMRecipients := BSM_APPLICATIONS; BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE, @BSMRecipients, iMessageID, MI_GETHANDLE,Application.Handle); end;end;initialization //注册消息 iMessageID := RegisterWindowMessage(STR_UNIQUE); InitInstance;finalization //还原消息处理过程 if OldWProc <> Nil then SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(OldWProc)); //关闭互斥对象 if MutHandle <> 0 then CloseHandle(MutHandle);end. //使用方法很简单,只要把此单元加入工程就可以了.