5

I need to know how to get application name like this when I have process name:

enter image description here

My progress so far:

Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpdwProcessId As Int32) As Int32
Private Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As IntPtr
'Private Declare Auto Function GetWindowText Lib "user32" (ByVal hWnd As System.IntPtr, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
Private makel As String
Private Function GetActiveAppProcess() As Process
    Dim activeProcessID As IntPtr
    GetWindowThreadProcessId(GetForegroundWindow(), activeProcessID)
    Return Process.GetProcessById(activeProcessID)
End Function
Sub GetProcInfo()
    Dim activeProcess As Process = GetActiveAppProcess()
    With activeProcess
        ProcessName = .ProcessName
        Windowtitle = .MainWindowTitle
        'Application name = ?
    End With
End Sub

I'm almost there except that Process.ProcessName returns "explorer". How can I get the value "Windows Explorer"? Any help is appreciated.

Ravi Kiran
  • 565
  • 1
  • 8
  • 22
  • 4
    Use Process.MainModule.Filename to get the path to the exe. Then FileVersionInfo.GetVersionInfo(path).FileDescription to get the description. – Hans Passant Nov 06 '15 at 10:59
  • Awesome! It worked :) Thanks Hans! ApplicationName = FileVersionInfo.GetVersionInfo(activeProcess.MainModule.FileName).FileDescription – Ravi Kiran Nov 06 '15 at 13:23
  • @HansPassant can you add the answer to get this question closed? – benni_mac_b Nov 06 '15 at 20:35

1 Answers1

4

Since Hans hasn't posted an answer yet, here's the answer as he suggested in the comments on the question. I hope everyone doesn't mind that I've taken the liberty to go ahead and write an answer.

Sub GetProcInfo()
   Dim activeProcess As Process = GetActiveAppProcess()
   With activeProcess
        ApplicationName = .MainModule.FileVersionInfo.FileDescription
        ProcessName = .ProcessName
        WindowTitle = .MainWindowTitle
   End With
End Sub
Jeff B
  • 8,572
  • 17
  • 61
  • 140