要实现这个功能:运行程序时,现在判断这个程序是否已经运行,是:就显示那个程序的窗口为当前窗口,没有就继续运行。 我做了一个代码如下:(但有个错误提示:Error

  • 主题发起人 主题发起人 ansili
  • 开始时间 开始时间
A

ansili

Unregistered / Unconfirmed
GUEST, unregistred user!
要实现这个功能:运行程序时,现在判断这个程序是否已经运行,是:就显示那个程序的窗口为当前窗口,没有就继续运行。 我做了一个代码如下:(但有个错误提示:Error in module XXX:Call to Application createForm is Missing or Incorre) (50分)<br />在工程文件里:<br>var<br>&nbsp; &nbsp; hMutex: HWND;<br>&nbsp; &nbsp; Ret: Integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>{$R *.res}<br><br>begin<br>&nbsp; &nbsp; Application.Initialize;<br>&nbsp; &nbsp; Application.Title := 'XXXX';<br>&nbsp; hMutex:=CreateMutex(nil,False,'XXXX');<br>&nbsp; Ret:=GetLastError;<br>&nbsp; If Ret=ERROR_ALREADY_EXISTS Then<br>&nbsp; &nbsp; ReleaseMutex(hMutex)<br>&nbsp; else<br>&nbsp; begin <br>&nbsp; &nbsp; Application.CreateForm(TMainForm, MainForm);<br>&nbsp; Application.Run;<br>&nbsp; end;<br>end.<br>或者是下面的代码:<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; if ShowWindow(FindWindow(nil,PChar(Application.Title),SW_SHOW)then<br>&nbsp; &nbsp; application.Terminate;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.<br>请问什么原因?如何修改。在线等待!!!!!!!!!!<br>
 
看看我的程序:<br>program AppServer;<br><br>uses<br>&nbsp; SvcMgr,<br>&nbsp; Forms,<br>&nbsp; Windows,<br>&nbsp; SysUtils,<br>&nbsp; WinSvc,<br>&nbsp; PipeAppServer_TLB in 'PipeAppServer_TLB.pas',<br>&nbsp; RDM_GDgh_svr in 'Replace/RDM_GDgh_svr.pas' {RDM_gdgh: TRemoteDataModule},<br>&nbsp; DM_PIPEMIS in 'Overhaul/DM_PIPEMIS.pas' {DM_PipeServer: TRemoteDataModule},<br>&nbsp; U_rdm_jxjh_print in 'Overhaul/U_rdm_jxjh_print.pas' {RDM_Jxjh_Print: TRemoteDataModule},<br>&nbsp; U_datamodule in 'BaseInfo/data/U_datamodule.pas' {DM_LINFU: TRemoteDataModule} {DM_LINFU: CoClass},<br>&nbsp; RDM_GDCBFX in 'Lacuna/RDM_GDCBFX.pas' {RDM_cbqxfx: TRemoteDataModule} {RDM: CoClass},<br>&nbsp; U_rdm_zxjc in 'OnLine/Check/U_rdm_zxjc.pas' {Rdm_zxjc: TRemoteDataModule} {Rdm_zxjc: CoClass},<br>&nbsp; U_DMJBXXCX in 'BaseInfo/Query/U_DMJBXXCX.pas' {DM_jbxxcx: TRemoteDataModule},<br>&nbsp; DM_chfx in 'OnLine/MeasureThickNess/DM_chfx.pas' {Mid_CH: TRemoteDataModule} {Mid_CH: CoClass},<br>&nbsp; U_RDM_AcdntRgdt in 'AcdntRgdt/U_RDM_AcdntRgdt.pas' {RDM_AcdntRgdt: TRemoteDataModule} {RDM_AcdntRgdt: CoClass},<br>&nbsp; U_RDMLnJxjlcx in 'Overhaul/U_RDMLnJxjlcx.pas' {RDMLnJxjlcx: TRemoteDataModule} {RDMLnJxjlcx: CoClass},<br>&nbsp; ScktMain in 'SocketServer/ScktMain.pas' {SocketForm},<br>&nbsp; ScktCnst in 'SocketServer/ScktCnst.pas';<br><br>{$R *.TLB}<br><br>{$R *.RES}<br><br>function Installing: Boolean;<br>begin<br>&nbsp; Result := FindCmdLineSwitch('INSTALL',['-','/','/'], True) or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FindCmdLineSwitch('UNINSTALL',['-','/','/'], True);<br>end;<br><br>function StartService: Boolean;<br>var<br>&nbsp; Mgr, Svc: Integer;<br>&nbsp; UserName, ServiceStartName: string;<br>&nbsp; Config: Pointer;<br>&nbsp; Size: DWord;<br>begin<br>&nbsp; Result := False;<br>&nbsp; Mgr := OpenSCManager(nil, nil, SC_MANAGER_ALL_ACCESS);<br>&nbsp; if Mgr &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; Svc := OpenService(Mgr, PChar(SServiceName), SERVICE_ALL_ACCESS);<br>&nbsp; &nbsp; Result := Svc &lt;&gt; 0;<br>&nbsp; &nbsp; if Result then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; QueryServiceConfig(Svc, nil, 0, Size);<br>&nbsp; &nbsp; &nbsp; Config := AllocMem(Size);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; QueryServiceConfig(Svc, Config, Size, Size);<br>&nbsp; &nbsp; &nbsp; &nbsp; ServiceStartName := PQueryServiceConfig(Config)^.lpServiceStartName;<br>&nbsp; &nbsp; &nbsp; &nbsp; if CompareText(ServiceStartName, 'LocalSystem') = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ServiceStartName := 'SYSTEM';<br>&nbsp; &nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; &nbsp; Dispose(Config);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseServiceHandle(Svc);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; CloseServiceHandle(Mgr);<br>&nbsp; end;<br>&nbsp; if Result then<br>&nbsp; begin<br>&nbsp; &nbsp; Size := 256;<br>&nbsp; &nbsp; SetLength(UserName, Size);<br>&nbsp; &nbsp; GetUserName(PChar(UserName), Size);<br>&nbsp; &nbsp; SetLength(UserName, StrLen(PChar(UserName)));<br>&nbsp; &nbsp; Result := CompareText(UserName, ServiceStartName) = 0;<br>&nbsp; end;<br>end;<br><br>begin<br>&nbsp; if not Installing then<br>&nbsp; begin<br>&nbsp; &nbsp; CreateMutex(nil, True, 'SCKTSRVR');<br>&nbsp; &nbsp; if GetLastError = ERROR_ALREADY_EXISTS then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('服务器已经运行!','错误',MB_OK+MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp; Halt;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; if Installing or StartService then<br>&nbsp; begin<br>&nbsp; &nbsp; SvcMgr.Application.Initialize;<br>&nbsp; &nbsp; SocketService := TSocketService.CreateNew(SvcMgr.Application, 0);<br>&nbsp; &nbsp; SvcMgr.Application.Title := '应用程序服务器';<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; SvcMgr.Application.Run;<br>&nbsp; end else<br>&nbsp; begin<br>&nbsp; &nbsp; Forms.Application.ShowMainForm := False;<br>&nbsp; &nbsp; Forms.Application.Initialize;<br>&nbsp; &nbsp; Forms.Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; SocketForm.Initialize(False);<br>&nbsp; &nbsp; Forms.Application.Run;<br>&nbsp; end; <br>end.
 
var<br>&nbsp; HMutex: HWND;<br>&nbsp; Ret: Integer;<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; Application.Title:='干部多媒体信息综合查询';<br>&nbsp; HMutex:=CreateMutex(nil,False,'干部多媒体信息综合查询');<br>&nbsp; Ret:=GetLastError;<br>&nbsp; if Ret&lt;&gt;ERROR_ALREADY_EXISTS then begin<br>&nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; &nbsp; Application.Run;<br>&nbsp; end;<br>&nbsp; ReleaseMutex(HMutex);<br>end.
 
能不能说说为什么会有那个错误?怎么解决?
 
If Ret=ERROR_ALREADY_EXISTS Then<br>&nbsp; &nbsp; ReleaseMutex(hMutex)<br>这句话不对。<br>因为CreateMutex函数返回的是一个原子(ATOM),当你检测到它存在时,其实<br>是你程序的前一个实例向操作系统注册的,所以ReleaseMutex(hMutex)语句应该<br>放在程序销毁前。
 
多人接受答案了。
 
后退
顶部