• windows 下获取父进程pid


    
            DWORD GetParentProcessID(DWORD dwProcessId)
    	{
    		LONG						status;
    		DWORD						dwParentPID = (DWORD)-1;
    		HANDLE						hProcess;
    		PROCESS_BASIC_INFORMATION	pbi;
    
    		PROCNTQSIP NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(  
    			GetModuleHandle(L"ntdll"), "NtQueryInformationProcess"); 
    
    		if(NULL == NtQueryInformationProcess)
    		{
    			return (DWORD)-1;
    		}
    		// Get process handle
    		hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE, dwProcessId);
    		if (!hProcess)
    		{
    			return (DWORD)-1;
    		}
    
    		// Retrieve information
    		status = NtQueryInformationProcess( hProcess,
    			ProcessBasicInformation,
    			(PVOID)&pbi,
    			sizeof(PROCESS_BASIC_INFORMATION),
    			NULL
    			);
    
    		// Copy parent Id on success
    		if  (!status)
    		{
    			dwParentPID = pbi.InheritedFromUniqueProcessId;
    		}
    
    		CloseHandle (hProcess);
    
    		return dwParentPID;
    		
    	}
    

    控制台中需要加入下面代码

    #include <wtypes.h>:
    
    #define ProcessBasicInformation 0  
    
     typedef struct  
     {  
    	 DWORD ExitStatus;  
    	 DWORD PebBaseAddress;  
    	 DWORD AffinityMask;  
    	 DWORD BasePriority;  
    	 ULONG UniqueProcessId;  
    	 ULONG InheritedFromUniqueProcessId;  
     }   PROCESS_BASIC_INFORMATION;  
    
    
     // ntdll!NtQueryInformationProcess (NT specific!)  
     //  
     // The function copies the process information of the  
     // specified type into a buffer  
     //  
     // NTSYSAPI  
     // NTSTATUS  
     // NTAPI  
     // NtQueryInformationProcess(  
     //    IN HANDLE ProcessHandle,              // handle to process  
     //    IN PROCESSINFOCLASS InformationClass, // information type  
     //    OUT PVOID ProcessInformation,         // pointer to buffer  
     //    IN ULONG ProcessInformationLength,    // buffer size in bytes  
     //    OUT PULONG ReturnLength OPTIONAL      // pointer to a 32-bit  
     //                                          // variable that receives  
     //                                          // the number of bytes  
     //                                          // written to the buffer   
     // ); 
     typedef LONG (__stdcall *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
    
    
  • 相关阅读:
    ios lazying load
    ios 单例模式
    ios 消息推送原理
    C#图片闪烁
    C#使窗体不显示在任务栏
    实时监测鼠标是否按下和鼠标坐标
    winfrom窗体的透明度
    C#获取屏幕的宽度和高度
    HDU 5171 GTY's birthday gift 矩阵快速幂
    HDU 5170 GTY's math problem 水题
  • 原文地址:https://www.cnblogs.com/jkcx/p/7457339.html
Copyright © 2020-2023  润新知