realbasic-nug
[Top] [All Lists]

Re: Managing Processes

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Managing Processes
From: Aaron Ballman <aaron at realsoftware dot com>
Date: Thu, 30 Dec 2004 15:52:41 -0600
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <B93E178541BB684CAFCDFD7436019C8C4636 at buexch01 dot intra dot kri dot com>
Moore, Willie wrote:

I'll see if I can whip something up, assuming that this is what you are
trying to do.  Just to make sure -- you want to know what process owns
the frontmost window, just the name of the process or something?

This is exactly what I am trying to do, and any help you can provide would be 
greatly appreciated.

Here ya go (also added for the next release of the Windows Functionality Suite).

Function GetFrontmostWindowHandle() as Integer
  #if TargetWin32
    Declare Function GetForegroundWindow Lib "User32" () as Integer

    return GetForegroundWindow
  #endif
End Function

Function GetActiveProcesses() as ProcessInformation()
  #if TargetWin32
Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (flags as Integer, id as Integer ) as Integer
    Declare Sub CloseHandle Lib "Kernel32" ( handle as Integer )
Declare Sub Process32First Lib "Kernel32" ( handle as Integer, entry as Ptr ) Declare Function Process32Next Lib "Kernel32" ( handle as Integer, entry as Ptr ) as Boolean

    dim snapHandle as Integer
    snapHandle = CreateToolhelp32Snapshot( 2, 0 )

    dim mb as new MemoryBlock( 260 + 36 )
    dim entry as ProcessInformation
    dim ret() as ProcessInformation

    mb.Long( 0 ) = mb.Size
    Process32First( snapHandle, mb )

    dim err as Integer = GetLastError

    do
      entry = new ProcessInformation( mb )

      ret.Append( entry )

      Call Process32Next( snapHandle, mb )
    loop until GetLastError() = 18
    CloseHandle( snapHandle )

    return ret
  #endif
End Function

Function GetFrontmostWindowProcessInformation() as ProcessInformation
  #if TargetWin32
    // We want to get the frontmost window based
    dim frontWindowHWND as Integer
    frontWindowHWND = GetFrontmostWindowHandle

    if frontWindowHWND <= 0 then return nil

    // Now figure out what process ID owns the window
Declare Sub GetWindowThreadProcessId Lib "User32" ( hwnd as Integer, ByRef procId as Integer )
    dim processID as Integer
    GetWindowThreadProcessId( frontWindowHWND, processId )

    // Now we get a list of all the processes, and see if we can find
    // one with a match.
    dim processes(-1) as ProcessInformation
    processes = GetActiveProcesses

    dim ret as ProcessInformation
    for each ret in processes
      if ret.ProcessID = processID then
        return ret
      end if
    next
  #endif
End Function

And finally, the class ProcessInformation. Here's the constructor, you can figure out the public class properties from it as well.

Constructor( mb as MemoryBlock )
  // Construct one from a memory block

  // First four bytes are the size of the structure, so
  // just ignore them.

  // Four bytes of reference count
  ReferenceCount = mb.Long( 4 )

  // Four bytes of process id
  ProcessID = mb.Long( 8 )

  // Pointer to the default heap id
  if mb.Long( 12 ) <> 0 then
    DefaultHeapID = mb.Ptr( 12 ).Long( 0 )
  end if

  // Module ID
  ModuleID = mb.Long( 16 )

  // Number of threads
  NumberOfThreads = mb.Long( 20 )

  // Parent id
  ParentProcessID = mb.Long( 24 )

  // The base priority for threads
  BaseThreadPriority = mb.Long( 28 )

  // Ignore the next four bytes, they're
  // reserved.

  // And finally, the name
  Name = mb.CString( 36 )
  if Name = "[System Process]" then Name = "System Idle Process"
End Sub

Enjoy!

~Aaron
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>