Get process info: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
mNo edit summary
mNo edit summary
Line 13: Line 13:


Section
Section
${GetProcessInfo} 0 $0 $1 $2 $3
;get own process info
MessageBox MB_OK "r0=$0 r1=$1 r2=$2 r3=$3"  
${GetProcessInfo} 0 $0 $1 $2 $3
;return r0=pid, r1=parent pid, r2=priority, r3=process name
DetailPrint "pid=$0 parent_pid=$1 priority=$2 name=$3"
 
;and now get parent process info!
${GetProcessInfo} $1 $0 $1 $2 $3
DetailPrint "pid=$0 parent_pid=$1 priority=$2 name=$3"
;woila
SectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>
Line 31: Line 36:
;DetailPrint "r0=$0 r1=$1 r2=$2 r3=$3" ;return r0=pid, r1=parent pid, r2=priority, r3=process name
;DetailPrint "r0=$0 r1=$1 r2=$2 r3=$3" ;return r0=pid, r1=parent pid, r2=priority, r3=process name
;
;


!define GetProcessInfo '!insertmacro GetProcessInfo'
!define GetProcessInfo '!insertmacro GetProcessInfo'
Line 43: Line 49:
Call _GetProcessInfo
Call _GetProcessInfo
Pop ${pid_out}
Pop ${pid_out}
Pop ${priority}
Pop ${ppid}
Pop ${ppid}
Pop ${priority}
Pop ${name}
Pop ${name}
!macroend
!macroend
Line 59: Line 65:
  Push $4
  Push $4
   
   
  !define TH32CS_SNAPPROCESS 2
  !define TH32CS_SNAPPROCESS 2
  !define INVALID_HANDLE_VALUE -1
  !define INVALID_HANDLE_VALUE -1


  IntCmp $R3 0 0 skip_pid_detection
  IntCmp $R3 0 0 skip_pid_detection skip_pid_detection
  System::Call 'kernel32::GetCurrentProcess() i.R0'
  System::Call 'kernel32::GetCurrentProcess() i.R0'
  System::Call "Kernel32::GetProcessId(i R0) i.R3"
  System::Call "Kernel32::GetProcessId(i R0) i.R3"
 
skip_pid_detection:
skip_pid_detection:
  System::Call 'Kernel32::CreateToolhelp32Snapshot(i ${TH32CS_SNAPPROCESS},i $R3) i.R0'
  System::Call 'Kernel32::CreateToolhelp32Snapshot(i ${TH32CS_SNAPPROCESS},i $R3) i.R0'
Line 112: Line 119:
end:
end:


  Exch $4 ;name;3;2;1;0
  Exch $4
  Exch 1 ;3;name;2;1;0
  Exch 1
  Exch $3 ;pri;name;2;1;0
  Exch $3
  Exch 2 ;2;pri;name;1;0
  Exch 2
  Exch $2 ;ppid;pri;name;1;0
  Exch $2
  Exch 3 ;1;ppid;pri;name;0
  Exch 3
  Pop $1 ;ppid;pri;name;0
  Pop $1
  Exch 3
  Exch 3
  Exch $0 ;pid;ppid;pri;name;R9;R1;R0;R3
  Exch $0
  Exch 4
  Exch 4
  Pop $R9 ;pid;ppid;pri;name;R1;R0;R3
  Pop $R9
  Exch 4
  Exch 4
  Pop $R1
  Pop $R1
  Exch 4
  Exch 4
  Pop $R0 ;pid;ppid;pri;name;R3
  Pop $R0
  Exch 4
  Exch 4
  Pop $R3
  Pop $R3

Revision as of 06:25, 25 November 2009

Description

Get process info by process id

  • Parent pid
  • Priority
  • process name

Usage

!include GetProcessInfo.nsh
 
OutFile GetProcessInfo.exe
 
Section
 ;get own process info
 ${GetProcessInfo} 0 $0 $1 $2 $3
 DetailPrint "pid=$0 parent_pid=$1 priority=$2 name=$3"
 
 ;and now get parent process info!
 ${GetProcessInfo} $1 $0 $1 $2 $3
 DetailPrint "pid=$0 parent_pid=$1 priority=$2 name=$3"
 ;woila
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 ${priority}
Pop ${ppid}
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 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
 Exch 1
 Exch $3
 Exch 2
 Exch $2
 Exch 3
 Pop $1
 Exch 3
 Exch $0
 Exch 4
 Pop $R9
 Exch 4
 Pop $R1
 Exch 4
 Pop $R0
 Exch 4
 Pop $R3
FunctionEnd