Question : Get current active application name

I want to write an application to monitor what software is being used and for how long on a machine.

I intend recording every five minutes to name of the currently active program

How do I get the name of the program?

Thanks

Steve

Answer : Get current active application name

You could do:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
Public Class Form1

    Private WithEvents tmr As New System.Windows.Forms.Timer
    Private Declare Auto Function GetForegroundWindow Lib "user32.dll" () As IntPtr

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tmr.Interval = TimeSpan.FromMinutes(5).TotalMilliseconds
        tmr.Start()
    End Sub

    Private Sub tmr_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr.Tick
        Dim foreWnd As IntPtr = GetForegroundWindow
        For Each P As Process In Process.GetProcesses
            If P.MainWindowHandle.Equals(foreWnd) Then
                Debug.Print("Current Time: " & DateTime.Now)
                Debug.Print("Process Name: " & P.ProcessName)
                Debug.Print("Process Title: " & P.MainWindowTitle)
                Debug.Print("-----------------------------------------------------------")
                Exit For
            End If
        Next
    End Sub

End Class
Random Solutions  
 
programming4us programming4us