• 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);
    
    
  • 相关阅读:
    PHP函数正则表达式函数
    PHP函数变量函数
    Passing Wpf Objects Between Threads (With Source Code)
    分手后,何必再要做朋友?
    享受爱,给予爱
    人生如戏
    深夜电话
    我们也要在不同的事情上面有不同的哲学的情况,才行
    幸 福
    你的平安夜是这样过的吗?
  • 原文地址:https://www.cnblogs.com/jkcx/p/7457339.html
Copyright © 2020-2023  润新知