Get Current Process ID within calling EXE

Just starting out? Need help? Post your questions and find answers here.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Get Current Process ID within calling EXE

Post by Straker »

Ok, I wrote this following function to return the current process id from within the calling EXE, however, I don't like having to open a window to do it since it is in a non-windowed app. Granted, it does work, but does anyone have a non-window method to do this?

Code: Select all

Procedure.l CurrentProcessId()
  Protected lRetVal.l, lWindowId.l, lWinId.l, lProcId.l, lThreadId.l

  lRetVal.l = 0
  lWindowId = OpenWindow(#PB_Any, 100, 200, 195, 260, "_CURRENT_PROCESS_WINDOW_", #PB_Window_Invisible)

  If (lWindowId)
    lWinId = WindowID(lWindowId)
    lThreadId = GetWindowThreadProcessId_(lWinId,@lProcId)

    lRetVal.l = lProcId
    
    CloseWindow(lWindowId.l)
  EndIf
  
  ProcedureReturn lRetVal.l
EndProcedure

Debug CurrentProcessId()
Image Image
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Code: Select all

Debug GetCurrentProcessId_()
;)
quidquid Latine dictum sit altum videtur
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Thanks freak. I knew it would be something simple.

:oops:


[Edit]

Now with cross-platform goodness:

Code: Select all

Procedure.l CurrentProcessId()
  Protected lPID.l
  
  lPID.l = 0
  
  CompilerSelect #PB_Compiler_OS
  
    CompilerCase #PB_OS_Windows
    
      lPID.l = GetCurrentProcessId_()
      
    CompilerCase #PB_OS_Linux
  
      lPID.l = getpid_()
  
  CompilerEndSelect
  
  ProcedureReturn lPID.l
EndProcedure
Image Image
Post Reply