判断程序是否已运行的新思路,求其实现的办法?(100分)

  • 主题发起人 主题发起人 panzichan
  • 开始时间 开始时间
P

panzichan

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;对于防止例程运行多个实例,我们一般采用如下办法:<br>var<br>&nbsp; &nbsp;hmutex:HWnd;<br>&nbsp; &nbsp;IntRet:integer;<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; hmutex:=CreateMutex(nil,false,pchar('MsChost'));<br>&nbsp; IntRet:=GetLastError;<br>&nbsp; if IntRet&lt;&gt;ERROR_ALREADY_EXISTS then<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp; ReleaseMutex(hmutex);<br>end.<br>&nbsp; <br>&nbsp; &nbsp;而对于判断某程序是否运行,我们一般用以下方法:<br>1.用getmodulefilenameex(openprocess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ,FALSE,PROCESSSTRUCT.th32ProcessID),0,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @MODNAME[0],sizeof(modname))<br>&nbsp; 函数来查找进程返回的文件路径名,是否符合条件.<br>2.或者用函数GetExitCodeProcess(FAppHandle, FAppState);判断<br><br>&nbsp; &nbsp;对于以上判断某程序是否运行两方法我都觉得繁琐,是否有API函数在程序中求出由<br>CreateMutex(nil,false,pchar('MsChost'));建立的互斥对像'MsChost',这样我们就可<br>知存在'MsChost',从而判断含有'MsChost'的程序已运行,是否有这样的函数?<br>请各位大虾多多指教....<br>
 
不明白,使用CreateMutex只要一步,而用OpenMutex判断是否有已经打开的互斥对象,没有<br>还是得用CreateMutex建立,要用两步呀<br>The OpenMutex function returns a handle of an existing named mutex object. <br>HANDLE OpenMutex(<br>&nbsp; &nbsp; DWORD dwDesiredAccess, // access flag <br>&nbsp; &nbsp; BOOL bInheritHandle, // inherit flag <br>&nbsp; &nbsp; LPCTSTR lpName // pointer to mutex-object name <br>&nbsp; &nbsp;); <br>Parameters<br>dwDesiredAccess<br>Specifies the requested access to the mutex object. For systems that support object security, the function fails if the security descriptor of the specified object does not permit the requested access for the calling process. <br>This parameter can be any combination of the following values: <br>Access Description<br>MUTEX_ALL_ACCESS Specifies all possible access flags for the mutex object.<br>SYNCHRONIZE Windows NT only: Enables use of the mutex handle in any of the wait functions to acquire ownership of the mutex, or in the ReleaseMutex function to release ownership.<br>bInheritHandle<br>Specifies whether the returned handle is inheritable. If TRUE, a process created by the CreateProcess function can inherit the handle; otherwise, the handle cannot be inherited. <br>lpName<br>Points to a null-terminated string that names the mutex to be opened. Name comparisons are case sensitive. <br>Return Values<br>If the function succeeds, the return value is a handle of the mutex object.<br>If the function fails, the return value is NULL. To get extended error information, call GetLastError. <br>Remarks<br>The OpenMutex function enables multiple processes to open handles of the same mutex object. The function succeeds only if some process has already created the mutex by using the CreateMutex function. The calling process can use the returned handle in any function that requires a handle of a mutex object, such as the wait functions, subject to the limitations of the access specified in the dwDesiredAccess parameter. <br>The handle can be duplicated by using the DuplicateHandle function. Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.
 
ysai:<br>&nbsp; &nbsp; 兄弟,能给个实例吗,我的E文很差 :(<br>&nbsp; &nbsp; 我想实现的是,我的一个程序A在判断我的另一个程序B,是否已运行,快点给吧!!<br>兄弟,我想给分你呢....
 
给你一个链接:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1685788
 
小雨哥:<br>&nbsp; &nbsp; 但我想用判断是否有已经打开的互斥对象的方法,有吗?
 
互斥对象在建立时给个名字,执行时先试着 Open ,不能 Open 就 Create 就行了。<br>离线库里有很多介绍。
 
工程1,只能运行一个实例<br>program Project1;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; windows,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br><br>var<br>&nbsp; hMutex:HWND;<br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; hMutex:=CreateMutex(nil,False,'{ECAAA829-31D8-400E-A697-5704137303B7}');<br>&nbsp; If GetLastError=ERROR_ALREADY_EXISTS Then<br>&nbsp; begin<br>&nbsp; &nbsp; ReleaseMutex(hMutex);<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>&nbsp; ReleaseMutex(hMutex);<br>end.<br><br>工程2,如果工程1没有运行则提示并退出<br>program Project2;<br><br>uses<br>&nbsp; Forms,<br>&nbsp; windows,<br>&nbsp; Dialogs,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1};<br><br>{$R *.res}<br><br>begin<br>&nbsp; Application.Initialize;<br>&nbsp; if OpenMutex(MUTEX_ALL_ACCESS,False,'{ECAAA829-31D8-400E-A697-5704137303B7}')=0 then<br>&nbsp; begin<br>&nbsp; &nbsp; showmessage('请运行工程1');<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br>&nbsp; Application.CreateForm(TForm1, Form1);<br>&nbsp; Application.Run;<br>end.<br><br>为了不与其它程序冲突,使用了GUID作标识,在DELPHI编辑器中按Ctrl+Shift+G可以生成,<br>注意两个GUID是相同的.
 
对于自己写的程序,主窗口的Caption是已知的,如果这个Caption是不变的,那么是不是可以用FindWindow查找窗口,如果找到了,那么就说明已经运行了一个实例。
 
后退
顶部