WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Process UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Current Process Parent Process id


Keywords: current processes parent process id proc procid

This script makes use of one of the Win32 Kernel API functions to obtain the current process' parent process id number. The parent process' id can then be used to get information about the parent process.
#DefineFunction udfGetMyParentProcId()

   ; Get a pseudo-handle to our own process.
   hMyProc = DllCall('KERNEL32.DLL',long:'GetCurrentProcess')

   ; Set up the input parameters for ZwQueryInformationProcess().
   nProcInfoBufSize = 6 * 4
   hProcInfoBuf = BinaryAlloc(nProcInfoBufSize)
   ProcessBasicInformation = 0
   Result = DllCall('NTDLL.DLL',long:'ZwQueryInformationProcess',long:hMyProc,long:ProcessBasicInformation,lpbinary:hProcInfoBuf,long:nProcInfoBufSize,lpnull)
   BinaryEodSet(hProcInfoBuf,nProcInfoBufSize)
   nMyProcId = BinaryPeek4(hProcInfoBuf,16)
   nMyParentProcId = BinaryPeek4(hProcInfoBuf,20)

   ; Sanity check - get our own process id by another method.
   nMyProcId2 = DllCall('KERNEL32.DLL',long:'GetCurrentProcessId')
   hProcInfoBuf = BinaryFree(hProcInfoBuf)

   Return nMyParentProcId

#EndFunction


#DefineFunction IsMyParentAConsole()

  MyParentProcId = udfGetMyParentProcId()
  MyParentMods = tListMod(MyParentProcId,1)
  ConsoleModName = StrLower(StrCat(DirWindows(1),'CMD.EXE'))
  bResult = @FALSE
  nCount = ItemCount(MyParentMods,@TAB)
  For nIndex = 1 To nCount
    ModName = StrLower(ItemExtract(nIndex,MyParentMods,@TAB))
    If (ModName == ConsoleModName)
    bResult = @TRUE
    Break
    EndIf
  Next

  Return bResult

#EndFunction

AddExtender('wwprc44I.dll')
Title01 = 'Get Parent Process Id'
Result = IsMyParentAConsole()
Message(Title01,StrCat('Is my parent process a console? Result = ',Result))

Article ID:   W16732
File Created: 2013:04:01:09:21:44
Last Updated: 2013:04:01:09:21:44