关于防止程序重复运行的问题(30分)

  • 主题发起人 主题发起人 stuwei
  • 开始时间 开始时间
S

stuwei

Unregistered / Unconfirmed
GUEST, unregistred user!
以下列方式防止程序重复运行,但即使第一次运行,程序就退出了,是什么原因?<br>请大家帮我看看<br>program ExcelApp;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; shellapi,<br>&nbsp; windows,<br>&nbsp; sysutils,<br>&nbsp; messages,<br>&nbsp; QDialogs,<br>&nbsp; Exmain in 'Exmain.pas' {formmain},<br>&nbsp; Excel2000 in '../Ocx/Servers/Excel2000.pas';<br><br>{$R *.res}<br><br>var<br>&nbsp; &nbsp; h:Thandle;<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; h:=findwindow('Tformmain','Excel设置工具');<br>&nbsp; if &nbsp; h&lt;&gt;0<br>&nbsp; &nbsp; then<br>&nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp;showmessage(' 程序不允许重复运行');<br>&nbsp; &nbsp; &nbsp; &nbsp;Application.Terminate;<br>&nbsp; &nbsp; &nbsp; End;<br><br>&nbsp; Application.CreateForm(Tformmain, formmain);<br>&nbsp; Application.Icon:=formmain.Icon;<br>&nbsp; Application.Run;<br>end.
 
你在Delphi里跟踪调试吧,直接运行执行文件不会出现上述问题
 
编译后,关闭你的开发环境,单独运行试试看。<br>因为你的开发环境中有这一类窗口。
 
是这么回事,谢谢各位
 
procedure TMainForm.FormCreate(Sender: TObject);<br>begin<br>if doiexist(self.Caption)then<br>&nbsp; begin<br>&nbsp; &nbsp; //已開啟<br>&nbsp; &nbsp; halt;<br>&nbsp; end;<br>end;<br><br>function TMainform.doiexist(wndtitle:string):boolean;<br>var<br>&nbsp; hsem:thandle;<br>&nbsp; hwndme:hwnd;<br>&nbsp; semnm,wtt1:array[0..256]of char;<br>begin<br>&nbsp; result:=false;<br>&nbsp; strpcopy(semnm,'abc');<br>&nbsp; strpcopy(wtt1,wndtitle);<br>&nbsp; hsem:=createsemaphore(nil,0,1,semnm);<br>&nbsp; if ((hsem&lt;&gt;0)and(getlasterror()=error_already_exists))then<br>&nbsp; begin<br>&nbsp; &nbsp; closehandle(hsem);<br>&nbsp; &nbsp; hwndme:=findwindow(nil,wtt1);<br>&nbsp; &nbsp; setwindowtext(hwndme,'zzzzzzz');<br>&nbsp; &nbsp; hwndme:=findwindow(nil,wtt1);<br>&nbsp; &nbsp; if(hwndme&lt;&gt;0)then begin<br>&nbsp; &nbsp; &nbsp; if isiconic(hwndme)then<br>&nbsp; &nbsp; &nbsp; &nbsp; showwindow(hwndme,sw_shownormal)<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; setforegroundwindow(hwndme);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; result:=true;<br>&nbsp; end;<br>end;<br><br>這個是我寫的,很正常,你用這個試試看吧!
 
h&lt;&gt;0<br>应该是h&lt;&gt;nil,另外要避免有同样标题的窗口存在。
 
后退
顶部