如何获取本程序在本电脑上打开过(100分)

  • 主题发起人 主题发起人 萧军
  • 开始时间 开始时间

萧军

Unregistered / Unconfirmed
GUEST, unregistred user!
比如我的电脑打开了一个用delphi开发的软件,但是我可能不知道再次打开,我就要防止不能再次打开。并告知用户你已经打开过了,不能再打开。
 
注册表作标记
 
有很多种办法<br>比如用互斥,还可以写注册表 等等
 
同意wp231957,如果未重启电脑,互斥的更好(单例)
 
写注册表的有弊端<br>我的一个软件就是用这个办法实现只启动一个例程的<br>但是<br>如果非法关闭这个软件,则下次无法开启,必须手工去改注册表<br>比如有其他进程向该进程发出了终止进程的命令或者非正常关机等等
 
用互斥的办法是最好的啦。<br>先在全局变量单元里面定义: hMutex: Boolean;<br><br>var<br> &nbsp;Res: DWORD;<br> &nbsp;Wnd: HWND;<br><br>begin<br> &nbsp;Application.Initialize;<br> &nbsp;hMutex := CreateMutex(nil, False, Title);<br> &nbsp;try<br> &nbsp; &nbsp;Res := GetLastError;<br> &nbsp; &nbsp;if Res &lt;&gt; ERROR_ALREADY_EXISTS then begin<br> &nbsp; &nbsp; &nbsp;Application.Initialize;<br> &nbsp; &nbsp; &nbsp;Application.Title := '登录';<br><br><br> &nbsp; &nbsp; frmLogon := TfrmLogon.Create(Application);<br> &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp;if Not frmLogon.AutoLogon then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if frmLogon.ShowModal &lt;&gt; mrOk then Exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;LogonID := frmLogon.LogonID;<br> &nbsp; &nbsp; &nbsp; &nbsp;LogonName := frmLogon.LogonName;<br> &nbsp; &nbsp; &nbsp; &nbsp;LogonUser := frmLogon.LogonUser;<br> &nbsp; &nbsp; &nbsp; &nbsp;LogonIP := GetIPAddress;<br> &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp;frmLogon.Free;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;Application.CreateForm(TfrmMain, frmMain);<br> &nbsp; &nbsp; &nbsp;Application.HintPause := 10;<br> &nbsp; &nbsp; &nbsp;Application.HintHidePause := 120000;<br> &nbsp; &nbsp; &nbsp;Application.Run;<br> &nbsp; &nbsp;end else begin<br> &nbsp; &nbsp; &nbsp;Wnd := FindWindow('TApplication', PChar(Title));<br> &nbsp; &nbsp; &nbsp;if Wnd &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(Wnd, WM_SYSCOMMAND, SC_RESTORE, 0);<br> &nbsp; &nbsp; &nbsp;Application.MessageBox('系统已经在运行!', '启动失败',<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MB_ICONEXCLAMATION);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;ReleaseMutex(hMutex);<br> &nbsp;end;<br><br>end.
 
标题跟内容不一样,还以为是记录打开次数之类的。
 
在formshow里做写入ini<br>在引用里<br>uses inifiles<br>form.show(....)//formshow事件代码下面的<br>var<br> ini:Tinifile;<br> num:integer;<br>begin<br> &nbsp;ini:=Tinifile.create('usenum.ini');<br> &nbsp;num:=ini.readinteger('num','打开次数',0); //读原来记录的<br> &nbsp;ini.writeinteger('num','打开次数',num+1);//写并加1<br>end;
 
打开之前在进程中搜索一下
 
很多方法的, 一般采用的是进程排斥。
 
后退
顶部