Guest User

Untitled

a guest
Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. #if _WIN32_WINNT < 0x0500
  2. # error "should be NT"
  3. #endif
  4. #include <windows.h>
  5. #include <tlhelp32.h>
  6. #include <winternl.h>
  7. #include <stdio.h>
  8.  
  9. DWORD getppid()
  10. {
  11. HANDLE hSnapshot = INVALID_HANDLE_VALUE;
  12. PROCESSENTRY32 pe32;
  13. DWORD ppid = 0, pid = GetCurrentProcessId();
  14.  
  15. hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
  16. __try{
  17. if( hSnapshot == INVALID_HANDLE_VALUE ) __leave;
  18.  
  19. ZeroMemory( &pe32, sizeof( pe32 ) );
  20. pe32.dwSize = sizeof( pe32 );
  21. if( !Process32First( hSnapshot, &pe32 ) ) __leave;
  22.  
  23. do{
  24. if( pe32.th32ProcessID == pid ){
  25. ppid = pe32.th32ParentProcessID;
  26. break;
  27. }
  28. }while( Process32Next( hSnapshot, &pe32 ) );
  29.  
  30. }
  31. __finally{
  32. if( hSnapshot != INVALID_HANDLE_VALUE ) CloseHandle( hSnapshot );
  33. }
  34. return ppid;
  35. }
  36.  
  37. DWORD getppid_nt() {
  38. NTSTATUS status;
  39. DWORD parent_pid = (DWORD)-1;
  40. HANDLE process;
  41. PROCESS_BASIC_INFORMATION pbi;
  42. ULONG retsize;
  43. typedef NTSTATUS (__stdcall *DefNtQueryInformationProcess)
  44. (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
  45. DefNtQueryInformationProcess NtQueryInformationProcess;
  46.  
  47. process = OpenProcess(
  48. PROCESS_QUERY_INFORMATION,
  49. FALSE,
  50. GetCurrentProcessId());
  51. if (!process)
  52. return (DWORD)-1;
  53. NtQueryInformationProcess = (DefNtQueryInformationProcess)
  54. GetProcAddress(GetModuleHandleA("ntdll"),
  55. "NtQueryInformationProcess");
  56. status = NtQueryInformationProcess(
  57. process,
  58. ProcessBasicInformation,
  59. (void*) &pbi,
  60. sizeof(PROCESS_BASIC_INFORMATION),
  61. &retsize
  62. );
  63. if (!status)
  64. parent_pid = (DWORD)pbi.Reserved3;
  65. CloseHandle(process);
  66. return parent_pid;
  67. }
  68.  
  69. int main(){
  70. printf( "%lx\n", getppid() );
  71. printf( "%lx\n", getppid_nt() );
  72.  
  73. return 0;
  74.  
  75. }
Add Comment
Please, Sign In to add comment