得到父进程的PID
来源:互联网 发布:div加name属性 js 编辑:程序博客网 时间:2024/06/04 09:05
Introduction
Sometimes, you might be in a situation when your application islaunched by another program, which cannot be modified to change itsbehavior.
If you can not grab any COM interface or a meaningful Windowshandle, then the only thing left is the process handle to deal with.One thing you can do with such a handle is to try to do someinterprocess synchronization.
There are two programs included in the workspace, a startup MFCapplication that will merely launch the child, and the childapplication called GetParentProcID.exe that will latch onto its parent and wait for its termination.
Background
Unfortunately, Win32 does not have an API that gives you the PID ofthe parent process, and you have to create it yourself. We will usefunctions found in Psapi.dll and in the Kernel32.DLL, that will be loaded programmatically.
Using the code
In short, we need to iterate through the processes running on themachine, identify the current process entry, and use it to retrieve theparent’s PID.
We begin by getting the handle to a Toolhelp snapshot of the systems.
hSnapShot = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 );
This handle is used to iterate through the process entries and compare them against the current process ID:
bContinue = lpfProcess32First( hSnapShot, &procentry ) ;
DWORD pid = 0;
// While there are processes, keep looping.
DWORD crtpid= GetCurrentProcessId();
while( bContinue )
{
if(crtpid == procentry.th32ProcessID)
pid = procentry.th32ParentProcessID;
procentry.dwSize = sizeof(PROCESSENTRY32) ;
bContinue = !pid && lpfProcess32Next( hSnapShot, &procentry );
}//while ends
The PROCESSENTRY32
structure we got from our GetParentProcID
function is passed down to the WaitForParentToFinish
, that does the synchronization:
The parent process PID is the th32ParentProcessID
data member of this structure.
There are two things to note about this last function:
- We need to get a process handle that can be used for synchronization (hence the
SYNCHRONIZE
flag):CollapseHANDLE hProcess = OpenProcess(
SYNCHRONIZE | PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, procentry.th32ParentProcessID ) ; - The parent’s file name can be retrieved using
EnumProcessModules
and grabbing the name from the first module by callingGetModuleFileNameEx
:Collapseif( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &dwSize2 ) )
{
if( !GetModuleFileNameEx( hProcess, hMod,
szFileName, sizeof( szFileName ) ) )
{.......
Once we have all the info about the parent, we can wait for its termination by calling WaitForSingleObject
:
while(dwres = WaitForSingleObject(hProcess,
TIMEOUT) == WAIT_TIMEOUT)//wait up to TIMEOUT ms
{
printf("TimedOut waiting for %s after %d ms/n", pszName,TIMEOUT);
}
printf((WAIT_OBJECT_0 == dwres)?
"waiting for parent termination SUCCEEDED/n" :
"parent waiting FAILED/n");
License
Thisarticle has no explicit license attached to it but may contain usageterms in the article text or the download files themselves. If in doubtplease contact the author via the discussion board below.
A list of licenses authors might use can be found here
- 得到父进程的PID
- 由PID得到进程名
- 启动java并得到当前进程的pid
- 读proc/pid得到进程名称
- linux C 获取pid终极父进程的PID
- 后台进程的PID
- 查找进程的pid
- bat 批处理 开始进程 得到pid 杀死进程 计算器
- 子进程和父进程的pid一定不同吗?
- [Win32]获取指定进程的父进程PID
- 1.4 打印进程的PID
- 查看java进程的PID
- 已知进程pid获取其父进程pid
- VC获取父进程PID
- VC获取父进程PID
- 得到进程的父进程的另一种办法
- 父进程产生一系列子进程,每个子进程打印自己的PID然后退出。要求父进程最后打印PID。
- ubuntu中孤儿进程的父进程pid并不是1??
- oracle 中的 CONCAT,substring ,MINUS 用法
- C++的四种类型转换
- 电话交换机
- 设置Foxmail收Hotmail邮件
- 获取最大重复子串
- 得到父进程的PID
- memcpy
- 网站(B/S开发中)cache应用
- 什么是RIA?介绍几种RIA客户端开发技术
- 交换机概论
- Spring JdbcTemplate 批量插入或更新操作
- 指针与引用的区别
- 内联函数与宏的区别
- Java多线程程序设计1