Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !macro ProcFuncs_
- System::Store "s" ; store registers in System's private stack
- Pop $0 ; process / PID
- Pop $1 ; mode
- Push 0 ; set return value if not found
- ; set mode of operation in $1
- ${Select} $1 ; mode 0 = GetProcessPID, mode 1 = GetProcessPath, mode 2 = GetProcessParent
- ${Case} 0
- StrCpy $2 $0 4 -4
- ${If} $2 == ".exe"
- ; exists from process name
- StrCpy $1 0
- ${Else}
- ; exists from pid
- StrCpy $1 1
- ${EndIf}
- ${Case} 1
- StrCpy $2 $0 4 -4
- ${If} $2 == ".exe"
- ; get path from process name
- StrCpy $1 2
- ${Else}
- ; get path from pid
- StrCpy $1 3
- ${EndIf}
- ${Case} 2
- StrCpy $2 $0 4 -4
- ${If} $2 == ".exe"
- ; get parent from process name
- StrCpy $1 4
- ${Else}
- ; get parent from pid
- StrCpy $1 5
- ${EndIf}
- ${EndSelect}
- System::Call '*(&l4,i,i,i,i,i,i,i,i,&w520)i .r2' ; $2 = PROCESSENTRY32W structure
- ; take system process snapshot in $3
- System::Call 'kernel32::CreateToolhelp32Snapshot(i 2, i 0)i .r3'
- ${Unless} $3 = -1
- !ifdef NSIS_UNICODE
- System::Call 'kernel32::Process32First(i r3, i r2)i .r4'
- !else
- System::Call 'kernel32::Process32FirstW(i r3, i r2)i .r4'
- !endif
- ${Unless} $4 = 0
- ${Do}
- ${Select} $1
- ${Case3} 0 2 4
- ; get process name in $5
- !ifdef NSIS_UNICODE
- System::Call '*$2(i,i,i,i,i,i,i,i,i,&m520 .r5)'
- !else
- System::Call '*$2(i,i,i,i,i,i,i,i,i,&w520 .r5)'
- !endif
- ${Case4} 1 3 5 6
- ; get process PID in $5
- System::Call '*$2(i,i,i .r5)'
- ${EndSelect}
- ; is this process the one we are looking for?
- ${If} $5 == $0 ; string test works ok for numeric PIDs as well
- ${Select} $1 ; mode 0/1 = GetProcessPID, mode 2/3 = GetProcessPath, mode 4/5 = GetProcessParent, mode 6 = GetProcessName
- ${Case2} 0 1
- ; return pid
- Pop $5 ; old return value
- System::Call '*$2(i,i,i .s)'; process pid to stack
- ${Case2} 2 3
- ; return full path
- Pop $5
- ; open process
- System::Call '*$2(i,i,i .s)'; process pid to stack
- System::Call 'kernel32::OpenProcess(i ${PROCESS_QUERY_INFORMATION}|${PROCESS_VM_READ}, i 0, i s)i .r5' ; process handle to $5
- ${Unless} $5 = 0
- ; full path to stack
- System::Call 'psapi::GetModuleFileNameExW(i r5, i 0, w .s, i ${NSIS_MAX_STRLEN})'
- System::Call 'kernel32::CloseHandle(i r5)'
- ${Else}
- Push -1 ; OpenProcess failure return value
- ${EndUnless}
- ${Case2} 4 5
- ; return parent PID
- Pop $5
- System::Call '*$2(i,i,i,i,i,i,i .s)'; parent pid to stack
- ${Case} 6
- ; return base name
- Pop $5
- System::Call '*$2(i,i,i,i,i,i,i,i,i,&w520 .s)'
- ${EndSelect}
- ${Break}
- ${EndIf}
- !ifdef NSIS_UNICODE
- System::Call 'kernel32::Process32Next(i r3, i r2)i .r4'
- !else
- System::Call 'kernel32::Process32NextW(i r3, i r2)i .r4'
- !endif
- ${LoopUntil} $4 = 0
- System::Call 'kernel32::CloseHandle(i r3)' ; close snapshot
- ${EndUnless}
- ${Else}
- Pop $5
- Push -2 ; function failure return value
- ${EndUnless}
- System::Free $2 ; free buffer
- System::Store "l" ; restore registers
- !macroend
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement