getProcessInfo.nsh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. ; NSIS PROCESS INFO LIBRARY - GetProcessInfo.nsh
  2. ; Version 1.1 - Mar 28th, 2011
  3. ;
  4. ; Description:
  5. ; Gets process information.
  6. ;
  7. ; Usage example:
  8. ; ${GetProcessInfo} 0 $0 $1 $2 $3 $4
  9. ; DetailPrint "pid=$0 parent_pid=$1 priority=$2 process_name=$3 exe=$4"
  10. ;
  11. ; History:
  12. ; 1.1 - 28/03/2011 - Added uninstall function, include guards, file header. Fixed getting full exe path on pre vista systems. (Sergius)
  13. ;
  14. !ifndef GETPROCESSINFO_INCLUDED
  15. !define GETPROCESSINFO_INCLUDED
  16. !define PROCESSINFO.TH32CS_SNAPPROCESS 2
  17. !define PROCESSINFO.INVALID_HANDLE_VALUE -1
  18. !define GetProcessInfo '!insertmacro GetProcessInfo'
  19. ;@in pid_in - if 0 - get current process info
  20. ;@out pid_out - real process id (may be useful, if pid_in=0)
  21. ;@out ppid - parent process id
  22. ;@out priority
  23. ;@out name - name of process
  24. ;@out fullname - fully-qualified path of process
  25. !macro GetProcessInfo pid_in pid_out ppid priority name fullname
  26. Push ${pid_in}
  27. !ifdef BUILD_UNINSTALLER
  28. Call un._GetProcessInfo
  29. !else
  30. Call _GetProcessInfo
  31. !endif
  32. ;name;pri;ppid;fname;pid;
  33. Pop ${name}
  34. Pop ${priority}
  35. Pop ${ppid}
  36. Pop ${fullname}
  37. Pop ${pid_out}
  38. !macroend
  39. !macro FUNC_GETPROCESSINFO
  40. Exch $R3 ;pid
  41. Push $0
  42. Push $1
  43. Push $2
  44. Push $3
  45. Push $4
  46. Push $5
  47. Push $R0 ;hSnapshot
  48. Push $R1 ;result
  49. Push $R9 ;PROCESSENTRY32;MODULEENTRY32 and so on
  50. Push $R8
  51. ;zero registers to waste trash, if error occurred
  52. StrCpy $0 ""
  53. StrCpy $1 ""
  54. StrCpy $2 ""
  55. StrCpy $3 ""
  56. StrCpy $4 ""
  57. StrCpy $5 ""
  58. IntCmp $R3 0 0 skip_pid_detection skip_pid_detection
  59. System::Call 'kernel32::GetCurrentProcess() i.R0'
  60. System::Call "Kernel32::GetProcessId(i R0) i.R3"
  61. skip_pid_detection:
  62. System::Call 'Kernel32::CreateToolhelp32Snapshot(i ${PROCESSINFO.TH32CS_SNAPPROCESS},i R3) i.R0'
  63. IntCmp $R0 ${PROCESSINFO.INVALID_HANDLE_VALUE} end ;someting wrong
  64. ;$R9=PROCESSENTRY32
  65. ;typedef struct tagPROCESSENTRY32 {
  66. ; DWORD dwSize;
  67. ; DWORD cntUsage;
  68. ; DWORD th32ProcessID;
  69. ; ULONG_PTR th32DefaultHeapID;
  70. ; DWORD th32ModuleID;
  71. ; DWORD cntThreads;
  72. ; DWORD th32ParentProcessID;
  73. ; LONG pcPriClassBase;
  74. ; DWORD dwFlags;
  75. ; TCHAR szExeFile[MAX_PATH];
  76. ;}PROCESSENTRY32, *PPROCESSENTRY32;
  77. ;dwSize=4*9+2*260
  78. System::Alloc 1024
  79. pop $R9
  80. System::Call "*$R9(i 556)"
  81. System::Call 'Kernel32::Process32FirstW(i R0, i $R9) i.R1'
  82. StrCmp $R1 0 end
  83. nnext_iteration:
  84. System::Call "*$R9(i,i,i.R1)" ;get PID
  85. IntCmp $R1 $R3 exitloop
  86. System::Call 'Kernel32::Process32NextW(i R0, i $R9) i.R1'
  87. IntCmp $R1 0 0 nnext_iteration nnext_iteration
  88. exitloop:
  89. ;$0 - pid
  90. ;$1 - threads
  91. ;$2 - ppid
  92. ;$3 - priority
  93. ;$4 - process name
  94. System::Call "*$R9(i,i,i.r0,i,i,i.r1,i.r2,i.r3,i,&w256.r4)" ; Get next module
  95. ;free:
  96. System::Free $R9
  97. System::Call "Kernel32::CloseToolhelp32Snapshot(i R0)"
  98. ;===============
  99. ;now get full path and commandline
  100. System::Call "Kernel32::OpenProcess(i 1040, i 0, i r0)i .R0"
  101. StrCmp $R0 0 end
  102. IntOp $R8 0 + 256
  103. System::Call "psapi::GetModuleFileNameExW(i R0,i 0,t .r5, *i $R8)i .R1"
  104. end:
  105. Pop $R8
  106. Pop $R9
  107. Pop $R1
  108. Pop $R0
  109. Exch $5
  110. Exch 1
  111. Exch $4
  112. Exch 2
  113. Exch $3
  114. Exch 3
  115. Exch $2
  116. Exch 4
  117. Pop $1
  118. Exch 4
  119. Exch $0
  120. Exch 5
  121. Pop $R3
  122. !macroend ;FUNC_GETPROCESSINFO
  123. !ifndef BUILD_UNINSTALLER
  124. Function _GetProcessInfo
  125. !insertmacro FUNC_GETPROCESSINFO
  126. FunctionEnd
  127. !endif
  128. !ifdef BUILD_UNINSTALLER
  129. Function un._GetProcessInfo
  130. !insertmacro FUNC_GETPROCESSINFO
  131. FunctionEnd
  132. !endif
  133. !endif ;GETPROCESSINFO_INCLUDED