Advertisement
Guest User

Untitled

a guest
Mar 25th, 2010
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. !macro ProcFuncs_
  2.     System::Store "s" ; store registers in System's private stack
  3.     Pop $0 ; process / PID
  4.     Pop $1 ; mode
  5.    
  6.     Push 0 ; set return value if not found
  7.    
  8.     ; set mode of operation in $1
  9.     ${Select} $1 ; mode 0 = GetProcessPID, mode 1 = GetProcessPath, mode 2 = GetProcessParent
  10.         ${Case} 0
  11.             StrCpy $2 $0 4 -4
  12.             ${If} $2 == ".exe"
  13.                 ; exists from process name
  14.                 StrCpy $1 0
  15.             ${Else}
  16.                 ; exists from pid
  17.                 StrCpy $1 1
  18.             ${EndIf}
  19.         ${Case} 1
  20.             StrCpy $2 $0 4 -4
  21.             ${If} $2 == ".exe"
  22.                 ; get path from process name
  23.                 StrCpy $1 2
  24.             ${Else}
  25.                 ; get path from pid
  26.                 StrCpy $1 3
  27.             ${EndIf}
  28.         ${Case} 2
  29.             StrCpy $2 $0 4 -4
  30.             ${If} $2 == ".exe"
  31.                 ; get parent from process name
  32.                 StrCpy $1 4
  33.             ${Else}
  34.                 ; get parent from pid
  35.                 StrCpy $1 5
  36.             ${EndIf}
  37.     ${EndSelect}
  38.    
  39.     System::Call '*(&l4,i,i,i,i,i,i,i,i,&w520)i .r2' ; $2 = PROCESSENTRY32W structure
  40.     ; take system process snapshot in $3
  41.     System::Call 'kernel32::CreateToolhelp32Snapshot(i 2, i 0)i .r3'
  42.     ${Unless} $3 = -1
  43. !ifdef NSIS_UNICODE
  44.         System::Call 'kernel32::Process32First(i r3, i r2)i .r4'
  45. !else
  46.         System::Call 'kernel32::Process32FirstW(i r3, i r2)i .r4'
  47. !endif
  48.         ${Unless} $4 = 0
  49.             ${Do}
  50.                 ${Select} $1
  51.                     ${Case3} 0 2 4
  52.                         ; get process name in $5
  53. !ifdef NSIS_UNICODE
  54.                         System::Call '*$2(i,i,i,i,i,i,i,i,i,&m520 .r5)'
  55. !else
  56.                         System::Call '*$2(i,i,i,i,i,i,i,i,i,&w520 .r5)'
  57. !endif
  58.                     ${Case4} 1 3 5 6
  59.                         ; get process PID in $5
  60.                         System::Call '*$2(i,i,i .r5)'
  61.                 ${EndSelect}
  62.                 ; is this process the one we are looking for?
  63.                 ${If} $5 == $0 ; string test works ok for numeric PIDs as well
  64.                     ${Select} $1 ; mode 0/1 = GetProcessPID, mode 2/3 = GetProcessPath, mode 4/5 = GetProcessParent, mode 6 = GetProcessName
  65.                         ${Case2} 0 1
  66.                             ; return pid
  67.                             Pop $5 ; old return value
  68.                             System::Call '*$2(i,i,i .s)'; process pid to stack
  69.                         ${Case2} 2 3
  70.                             ; return full path
  71.                             Pop $5
  72.                             ; open process
  73.                             System::Call '*$2(i,i,i .s)'; process pid to stack
  74.                             System::Call 'kernel32::OpenProcess(i ${PROCESS_QUERY_INFORMATION}|${PROCESS_VM_READ}, i 0, i s)i .r5' ; process handle to $5
  75.                             ${Unless} $5 = 0
  76.                                 ; full path to stack
  77.                                 System::Call 'psapi::GetModuleFileNameExW(i r5, i 0, w .s, i ${NSIS_MAX_STRLEN})'
  78.                                 System::Call 'kernel32::CloseHandle(i r5)'
  79.                             ${Else}
  80.                                 Push -1 ; OpenProcess failure return value
  81.                             ${EndUnless}
  82.                         ${Case2} 4 5
  83.                             ; return parent PID
  84.                             Pop $5
  85.                             System::Call '*$2(i,i,i,i,i,i,i .s)'; parent pid to stack
  86.                         ${Case} 6
  87.                             ; return base name
  88.                             Pop $5
  89.                             System::Call '*$2(i,i,i,i,i,i,i,i,i,&w520 .s)'
  90.                     ${EndSelect}
  91.                     ${Break}
  92.                 ${EndIf}
  93. !ifdef NSIS_UNICODE
  94.                 System::Call 'kernel32::Process32Next(i r3, i r2)i .r4'
  95. !else
  96.                 System::Call 'kernel32::Process32NextW(i r3, i r2)i .r4'
  97. !endif
  98.             ${LoopUntil} $4 = 0
  99.             System::Call 'kernel32::CloseHandle(i r3)' ; close snapshot
  100.         ${EndUnless}
  101.     ${Else}
  102.         Pop $5
  103.         Push -2 ; function failure return value
  104.     ${EndUnless}
  105.     System::Free $2 ; free buffer
  106.    
  107.     System::Store "l" ; restore registers
  108. !macroend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement