Get PID from process or filename

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2677
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Get PID from process or filename

Post by Michael Vogel »

I am using a program which shows a popup unimportant information from time to time. I kill the process by checking regularly if a certain processname (for example PROCESS.EXE) is present, but this needs to scan all windows handles and so some CPU time. Does anyone know a quick and reliable possibility to get the process ID from its name? I found some snippets here in the forum but all of them are doing a loop which needs some time...

Code: Select all

					ProcessHandle=GetWindow_(FindWindow_(0,0),#GW_HWNDFIRST)
					While ProcessHandle
						ProcessName=Space(128)
						GetWindowText_(ProcessHandle,@ProcessName,127)
						GetWindowThreadProcessId_(ProcessHandle,@ProcessID)
						
						If ProcessName="WINDOWTITLE"
							ProcessName=ProcessNameFromHwnd(ProcessHandle)
							If ProcessName
								If LCase(GetFilePart(ProcessName))="PROCESS.exe"
									PostMessage_(ProcessHandle,#WM_CLOSE,0,0);	schnelles Schließen...
									quit=OpenProcess_(#PROCESS_TERMINATE,#False,ProcessID)
									If quit
										TerminateProcess_(quit,#True)
										CloseHandle_(quit)
									EndIf
									Break
								EndIf
							EndIf
						EndIf
						ProcessHandle=GetWindow_(ProcessHandle,#GW_HWNDNEXT)
					Wend
fryquez
Enthusiast
Enthusiast
Posts: 367
Joined: Mon Dec 21, 2015 8:12 pm

Re: Get PID from process or filename

Post by fryquez »

Code: Select all

EnableExplicit

#SystemProcessInformation = $0005

Structure _UNICODE_STRING Align #PB_Structure_AlignC
  usLength.w 
  usMaximumLength.w   
  usBuffer.i
EndStructure

Structure _SYSTEM_PROCESS_INFO Align #PB_Structure_AlignC
  NextEntryOffset.l
  NumberOfThreads.l
  Reserved.q[3]
  CreateTime.q
  UserTime.q
  KernelTime.q
  ImageName._UNICODE_STRING
  BasePriority.l
  ProcessId.i
  InheritedFromProcessId.i
EndStructure

Define dwlen, *Buffer, *SPI._SYSTEM_PROCESS_INFO
NtQuerySystemInformation_(#SystemProcessInformation, 0, 0, @dwlen)
If dwlen
  dwlen * 2
  *Buffer = AllocateMemory(dwlen)
  If *Buffer
    If NtQuerySystemInformation_(#SystemProcessInformation, *Buffer, dwlen, @dwlen) = #ERROR_SUCCESS
      
      *SPI = *Buffer
      
      While *SPI\NextEntryOffset
        
        If *SPI\ImageName\usBuffer
          
          Debug RSet(Str(*SPI\ProcessId), 4, "0") + #TAB$ + PeekS(*SPI\ImageName\usBuffer, -1, #PB_Unicode)
          
        EndIf
        *SPI + *SPI\NextEntryOffset
      Wend
      
    EndIf
    FreeMemory(*Buffer)
  EndIf
EndIf
User avatar
skywalk
Addict
Addict
Posts: 3997
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Get PID from process or filename

Post by skywalk »

Code: Select all

Define.i hW, pid, thrid
hW = FindWindow_(#Null$, "Your App Name Here")
If hW and IsWindow_(hW)
  thrid = GetWindowThreadProcessId_(hW, @pid)
  Debug "pid = " + Str(pid)
EndIf
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply