1/1页1 跳转到查看:7279
发新话题 回复该主题

如何检查外部调用程序的状态

如何检查外部调用程序的状态

我通过ShellExecute打开一个程序,我希望能知道这个我打开的程序是否已经关闭,如果关闭我要做一些处理.....

现在我想到的是打开后,将消息循环死掉,然后一直检查这个进程是否完成,完成的话就退出消息循环.

现在不知道如何检查这个程序的运行状态...

TOP

 

回复: 如何检查外部调用程序的状态



CreateProcess
The CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file.

BOOL CreateProcess(  LPCTSTR lpApplicationName,// pointer to name of executable module 
        LPTSTR lpCommandLine, // pointer to command line string 
        LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes 
        LPSECURITY_ATTRIBUTES lpThreadAttributes,  // thread security attributes 
        BOOL bInheritHandles,  // handle inheritance flag 
        DWORD dwCreationFlags, // creation flags 
        LPVOID lpEnvironment,  // pointer to new environment block 
        LPCTSTR lpCurrentDirectory,  // pointer to current directory name 
        LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO 
        LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION);

先用这个函数打开程序,再用最后一个参数中的 dwProcessId 作为参数,调用OpenProcess 就可以知道进程是否退出。
typedef struct _PROCESS_INFORMATION {
        // pi   
        HANDLE hProcess;   
        HANDLE hThread;   
        DWORD dwProcessId;   
        DWORD dwThreadId;
} PROCESS_INFORMATION;

另外,CreateProcess 的返回值如下

Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call [url=mkMSITStore:C:\Program%20Files\Microsoft%20Visual%20Studio\MSDN98\98VS\2052\winbase.chm::/devdoc/live/pdwbase/errors_3uwi.htm]GetLastError[/url].

当返回值为0的时候,证明函数执行错误,可以使用GetLastError获得更多的信息。


TOP

 

回复:如何检查外部调用程序的状态

如果你还是想用ShellExecute,又想在程序外面检测程序状态,则需要使用 ShellExecuteEx。
例如:
  m_localFilePath="C:\DocExc006926.doc";
  SHELLEXECUTEINFO ShExecInfo ;
  memset(&ShExecInfo,0,sizeof(SHELLEXECUTEINFO));
  ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  ShExecInfo.hwnd = NULL;
  ShExecInfo.lpVerb = "open";
  ShExecInfo.lpFile =(LPCTSTR)m_localFilePath;
  ShExecInfo.lpParameters = NULL;
  ShExecInfo.lpDirectory = NULL;
  ShExecInfo.nShow = SW_NORMAL;
  ShExecInfo.hInstApp = NULL;
  ShellExecuteEx(&ShExecInfo);
  DWORD exCode;
  GetExitCodeProcess(ShExecInfo.hProcess,&exCode);
  while(exCode==STILL_ACTIVE)
  {
  Sleep(10);
  MSG msg;
  memset(&msg,0,sizeof(MSG));
  if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
  }
  GetExitCodeProcess(ShExecInfo.hProcess,&exCode);
  }

备注: 以上代码为VC代码 。。。

TOP

 

回复:如何检查外部调用程序的状态

顺便说一声,上面文章的原始地址为

http://wking.yculblog.com/post.680483.html

思路相当明确。

TOP

 

回复:如何检查外部调用程序的状态

能不能给个DELPHI的解决方案,这个我看了有点晕,呵呵.

TOP

 

回复:如何检查外部调用程序的状态

我试一下 不过最近时间比较少 不知道什么时候才能发上来。

TOP

 

回复: 如何检查外部调用程序的状态

看看这个能否解决你的问题。

附件

ShellExecute.rar ()

2007-7-9 10:34:56


ShellExec_source.rar ()

2007-7-9 10:34:56


本帖被评分 1 次

TOP

 

回复:如何检查外部调用程序的状态

谢谢老兄,确实可以解决问题。。。

TOP

 

回复:如何检查外部调用程序的状态

:)

TOP

 
1/1页1 跳转到
发表新主题 回复该主题