1

I am getting both the currently active window title and exe filepath with the code below

hwnd = win32gui.GetForegroundWindow()
    _, pid = win32process.GetWindowThreadProcessId(hwnd)
    if hwnd != 0 or pid != 0:
        try:
            hndl =     win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, pid)
            self.newExe = win32process.GetModuleFileNameEx(hndl, 0)
            self.newWindowTitle = win32gui.GetWindowText(hwnd)
        except:
            self.newExe = ''
            self.newWindowTitle = ''

the issue is that although it often is, the window title is not always the application name (the name the users understand as the main part of an application) and this is what I need. for example from calc.exe get Calculator withiout relying on the window title.

the purpose is to create a script that will log in an xml comparative use of any software on a computer

Is this possible?

user2145312
  • 896
  • 3
  • 10
  • 33
  • First of all you need to define "application name". – David Heffernan Jun 29 '15 at 14:54
  • in this case it means the name users know an application by. for example chrome.exe -> Google Chrome – user2145312 Jun 29 '15 at 14:55
  • In that case what you ask for is in general impossible. There's no a priori reason for an executable to contain its official product name in a single well defined place. – David Heffernan Jun 29 '15 at 14:56
  • is there possibly a way to compare the exe to a list of running programs on the computer and get information about the software from the system? – user2145312 Jun 29 '15 at 14:58
  • 1
    What are you actually trying to do? I guess that the best you can do is to read the version info resource from the executable and report the `FileDescription` field. That's what tools like Process Explorer do. – David Heffernan Jun 29 '15 at 15:03
  • So try the code from this answer: http://stackoverflow.com/a/7993095/505088 – David Heffernan Jun 29 '15 at 15:19
  • Note: Binary images are not required to contain a [VERSIONINFO resource](https://msdn.microsoft.com/en-us/library/windows/desktop/aa381058.aspx). – IInspectable Jun 29 '15 at 15:32

1 Answers1

2

Most Windows applications store information such as this inside their resource tables. There are API calls that can be used to extract this.

The following extracts the file description from a given application:

import win32api

def getFileDescription(windows_exe):
    try:
        language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
        stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
        description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
    except:
        description = "unknown"
        
    return description
    
    
print(getFileDescription(r"C:\Program Files\Internet Explorer\iexplore.exe"))

The output is:

Internet Explorer

You could therefore pass the result of your call to win32process.GetModuleFileNameEx() to this function.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97