VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Process Info"
   ClientHeight    =   3915
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   5295
   BeginProperty Font 
      Name            =   "Tahoma"
      Size            =   8.25
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3915
   ScaleWidth      =   5295
   StartUpPosition =   3  'Windows Default
   Begin VB.Timer Timer1 
      Interval        =   2000
      Left            =   4800
      Top             =   120
   End
   Begin VB.Label Label1 
      Height          =   855
      Index           =   5
      Left            =   240
      TabIndex        =   5
      Top             =   2880
      Width           =   4935
   End
   Begin VB.Label Label1 
      Height          =   255
      Index           =   4
      Left            =   240
      TabIndex        =   4
      Top             =   2520
      Width           =   4935
   End
   Begin VB.Label Label1 
      Height          =   255
      Index           =   3
      Left            =   240
      TabIndex        =   3
      Top             =   2160
      Width           =   4935
   End
   Begin VB.Label Label1 
      Height          =   855
      Index           =   2
      Left            =   240
      TabIndex        =   2
      Top             =   480
      Width           =   4935
   End
   Begin VB.Line Line1 
      BorderWidth     =   2
      X1              =   120
      X2              =   5160
      Y1              =   1560
      Y2              =   1560
   End
   Begin VB.Label Label1 
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   1
      Top             =   120
      Width           =   5055
   End
   Begin VB.Label Label1 
      BeginProperty Font 
         Name            =   "Tahoma"
         Size            =   8.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Index           =   1
      Left            =   120
      TabIndex        =   0
      Top             =   1800
      Width           =   5055
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
    (ByVal hWnd As Long, _
    ByVal lpString As String, _
    ByVal cch As Long) As Long
    
Private Declare Function CloseHandle Lib "kernel32.dll" _
    (ByVal hObject As Long) As Long
   
Private Declare Function OpenHandle Lib "kernel32.dll" Alias "OpenProcess" _
    (ByVal dwDesiredAccess As Long, _
    ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
Private Const PROCESS_QUERY_INFORMATION = &H400&
Private Const PROCESS_VM_READ = &H10&

Private Declare Function GetWindowThreadProcessId Lib "user32.dll" _
    (ByVal hWnd As Long, _
    lpdwProcessId As Long) As Long

Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" _
    (ByVal hProcess As Long, _
    ByVal hModule As Long, _
    ByVal ModuleName As String, _
    ByVal nSize As Long) As Long
    
Private Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
    (ByVal hWnd As Long) As Long
    
Private Declare Function EnumProcessModules Lib "psapi.dll" _
    (ByVal hProcess As Long, _
    ByRef lphModule As Long, _
    ByVal cb As Long, _
    ByRef cbNeeded As Long) As Long
        
Private Declare Function GetForegroundWindow Lib "user32.dll" () As Long

Private Sub Form_Load()
    'From my understanding of Windows and processes in Windows
    'I believe process IDs exists as an integer pointing directly to a process.
    'Other processes can use the process ID to request a process handle.
    'This handle is then a direct 'link' to the process.
    'Modules are stupid and confuse me, but I know they are executeables like DLLs and OCXs
        'that are ran from within the base module?
    'Windows have their own handles which are global throughout the OS, like modules?

    'Variables for foreground window
    Dim hFGWnd As Long, sTitle As String, hWnd As Long, iLen As Integer
    
    'Variables for process of foreground window
    Dim lProcID As Long, hProcess As Long, hModule As Long
    
    'Variables for base module
    Dim sFilename As String, bReturned As Long
    
    hFGWnd = GetForegroundWindow 'Get hWnd of foreground window
    iLen = GetWindowTextLength(hFGWnd) 'Get caption length of foreground window
    sTitle = Space$(iLen) 'Set up buffer for caption
    Call GetWindowText(hFGWnd, sTitle, iLen + 1) 'Get caption from foreground window
    
    Call GetWindowThreadProcessId(hFGWnd, lProcID) 'Get process ID of foreground window
       
    'Get a process handle using process ID
    hProcess = OpenHandle(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcID)

    'Get a handle to the base module of process
    Call EnumProcessModules(hProcess, hModule, 4&, bReturned)

    'Use the base module handle to return the filename of the process!
    sFilename = Space$(255) 'Create buffer
    Call GetModuleFileNameEx(hProcess, hModule, sFilename, 255)
    
    'Close our handle to the process
    Call CloseHandle(hProcess)
    
    Label1(0) = "Foreground window handle: " & hFGWnd
    Label1(2) = "- Window caption: " & sTitle
    Label1(1) = "Process ID: " & lProcID
    Label1(3) = "- Base module handle: " & hModule
    Label1(4) = "- Process handle used: " & hProcess
    Label1(5) = " - Process filename: " & sFilename
End Sub

Private Sub Timer1_Timer()
    Call Form_Load
End Sub
