Get process info: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(Created page with '== Description == Get process info by process id [*]Parent pid [*]Priority [*]process name == Usage == <highlight-nsis> !include GetProcessInfo.nsh OutFile GetProcessInfo.exe …')
 
mNo edit summary
Line 1: Line 1:
== Description ==
== Description ==
Get process info by process id
Get process info by process id
[*]Parent pid
* Parent pid
[*]Priority
* Priority
[*]process name
* process name


== Usage ==
== Usage ==

Revision as of 06:06, 25 November 2009

Description

Get process info by process id

  • Parent pid
  • Priority
  • process name

Usage

!include GetProcessInfo.nsh
 
OutFile GetProcessInfo.exe
 
Section
${GetProcessInfo} 0 $0 $1 $2 $3
MessageBox MB_OK "r0=$0 r1=$1 r2=$2 r3=$3" 
;return r0=pid, r1=parent pid, r2=priority, r3=process name
SectionEnd

The Script

File:GetProcessInfo.nsh

File content:

;get process information
;
;Usage example:
;${GetProcessInfo} 0 $0 $1 $2 $3
;DetailPrint "r0=$0 r1=$1 r2=$2 r3=$3" ;return r0=pid, r1=parent pid, r2=priority, r3=process name
;
 
!define GetProcessInfo '!insertmacro GetProcessInfo'
 
;@in pid_in - if 0 - get current process info
;@out pid_out - real process id (may be useful, if pid_in=0)
;@out ppid - parent process id
;@out priority
;@out name - name of process
!macro GetProcessInfo pid_in pid_out ppid priority name
Push ${pid_in}
Call _GetProcessInfo
Pop ${pid_out}
Pop ${ppid}
Pop ${priority}
Pop ${name}
!macroend
 
Function _GetProcessInfo
 Exch $R3 ;pid
 Push $R0 ;hSnapshot
 Push $R1 ;result
 Push $R9 ;PROCESSENTRY32
 Push $0
 Push $1
 Push $2
 Push $3
 Push $4
 
 !define TH32CS_SNAPPROCESS 2
 !define INVALID_HANDLE_VALUE -1
 
 IntCmp $R3 0 0 skip_pid_detection
 System::Call 'kernel32::GetCurrentProcess() i.R0'
 System::Call "Kernel32::GetProcessId(i R0) i.R3"
 
skip_pid_detection:
 System::Call 'Kernel32::CreateToolhelp32Snapshot(i ${TH32CS_SNAPPROCESS},i $R3) i.R0'
 
 IntCmp $R0 ${INVALID_HANDLE_VALUE} end ;someting wrong
 
;$R9=PROCESSENTRY32
;typedef struct tagPROCESSENTRY32 {
;  DWORD     dwSize;
;  DWORD     cntUsage;
;  DWORD     th32ProcessID;
;  ULONG_PTR th32DefaultHeapID;
;  DWORD     th32ModuleID;
;  DWORD     cntThreads;
;  DWORD     th32ParentProcessID;
;  LONG      pcPriClassBase;
;  DWORD     dwFlags;
;  TCHAR     szExeFile[MAX_PATH];
;}PROCESSENTRY32, *PPROCESSENTRY32;
;dwSize=4*9+260
 
 System::Alloc 296
 pop $R9
 System::Call "*$R9(i 296)"
 
 System::Call 'Kernel32::Process32First(i R0, i $R9) i.R1'
 
nnext_iteration:
  System::Call "*$R9(i,i,i.R1)" ;get PID
  IntCmp $R1 $R3 exitloop
 
  System::Call 'Kernel32::Process32Next(i R0, i $R9) i.R1'
  IntCmp $R1 0 0 nnext_iteration nnext_iteration
 
exitloop:
 ;$0 - pid
 ;$1 - threads
 ;$2 - ppid
 ;$3 - priority
 ;$4 - process name
 System::Call "*$R9(i,i,i.r0,i,i,i.r1,i.r2,i.r3,i,&t256.r4)" ; Get next module
 
free:
 System::Free $R9
 
end:
 
 Exch $4 ;name;3;2;1;0
 Exch 1  ;3;name;2;1;0
 Exch $3 ;pri;name;2;1;0
 Exch 2 ;2;pri;name;1;0
 Exch $2 ;ppid;pri;name;1;0
 Exch 3  ;1;ppid;pri;name;0
 Pop $1  ;ppid;pri;name;0
 Exch 3
 Exch $0 ;pid;ppid;pri;name;R9;R1;R0;R3
 Exch 4
 Pop $R9 ;pid;ppid;pri;name;R1;R0;R3
 Exch 4
 Pop $R1
 Exch 4
 Pop $R0 ;pid;ppid;pri;name;R3
 Exch 4
 Pop $R3
FunctionEnd