Results 1 to 8 of 8

Thread: Getting application name via window handle???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91

    Getting application name via window handle???

    Hi,

    Is is possible to get the application name using the windows handle???

    For example, if I have a running instance of word and I want find the application name (which would be Microsoft Word), using its hWnd!!!

    Subhendu.

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I think this is what you want:

    VB Code:
    1. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    2. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
    3.  
    4. Public Function GetWindowTitle(ByVal hWnd As Long) As String
    5.     GetWindowTitle = String(GetWindowTextLength(hWnd) + 1, Chr$(0))
    6.     Call GetWindowText(hWnd, GetWindowTitle, Len(GetWindowTitle))
    7. End Function

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Not exactly!

    See the code returns the application window caption, which would be like - 'Document1 - Microsoft Word', if MS Word were open.

    What I need is - 'Microsoft Word' part only.

    But, the problem is that all applications do not follow the same pattern as that of 'Document Name - Application Name'.

    Some follow the format of 'Application Name - Document Name'.

    So, I want to know if there is a way to find the application name, (and not the caption) using the hWnd!!!

    Thanks.

  4. #4
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Oh right I see. Sorry I'm not sure how to do that then. I'm sure there's a way. Have you checked in the AllAPI guide thing?

  5. #5
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    This may not be exactly what you were asking for, but it gets the name of the exe (I think I saw that called the application name somewhere so that's why I decided to mention it). Even if this isn't what you want, you may find it useful for something else.
    VB Code:
    1. 'in a module
    2. Option Explicit
    3.  
    4. Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    5. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    6. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    7. Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    8. Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    9. Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    10.  
    11. Public Const TH32CS_SNAPPROCESS As Long = &H2
    12. Public Const MAX_PATH As Integer = 260
    13. Public Const SYNCHRONIZE As Long = &H100000
    14. Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
    15. Public Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
    16.  
    17. Public Type PROCESSENTRY32
    18.    dwSize As Long
    19.    cntUsage As Long
    20.    th32ProcessID As Long
    21.    th32DefaultHeapID As Long
    22.    th32ModuleID As Long
    23.    cntThreads As Long
    24.    th32ParentProcessID As Long
    25.    pcPriClassBase As Long
    26.    dwFlags As Long
    27.    szExeFile As String * MAX_PATH
    28. End Type
    29.  
    30. 'returns the exe name of the hWnd's process, or "" if it can't be determined
    31. Public Function GetEXEName(ByVal hWnd As Long) As String
    32.    Dim PID As Long
    33.    Dim hProcess As Long
    34.    Dim hSnapshot As Long
    35.    Dim peInfo As PROCESSENTRY32
    36.    Dim lngReturn As Long
    37.    Dim blnFound As Boolean
    38.    'if the hWnd is valid
    39.    If hWnd Then
    40.       'get the PID of the window's thread
    41.       If GetWindowThreadProcessId(hWnd, PID) Then
    42.          'attempt to get a handle to the process
    43.          hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, PID)
    44.          If hProcess Then
    45.             'make a snapshot of the processes
    46.             hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, PID)
    47.             If hSnapshot Then
    48.                'set the size of the structure
    49.                peInfo.dwSize = Len(peInfo)
    50.                'get the first process in the snapshot
    51.                lngReturn = Process32First(hSnapshot, peInfo)
    52.                'loop until there are no more
    53.                Do While lngReturn
    54.                   'if the current PID matches the PID we have already, stop the loop
    55.                   If peInfo.th32ProcessID = PID Then
    56.                      blnFound = True
    57.                      Exit Do
    58.                   End If
    59.                   'get the next process
    60.                   lngReturn = Process32Next(hSnapshot, peInfo)
    61.                Loop
    62.                If blnFound Then
    63.                   'trim off the null terminator and return it
    64.                   GetEXEName = Left$(peInfo.szExeFile, InStr(peInfo.szExeFile, vbNullChar) - 1)
    65.                End If
    66.                'close the snapshot handle
    67.                CloseHandle hSnapshot
    68.             End If
    69.             'close the process handle
    70.             CloseHandle hProcess
    71.          End If
    72.       End If
    73.    End If
    74. End Function
    Last edited by Kaverin; Sep 19th, 2002 at 12:31 AM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2002
    Posts
    91
    Hi Kaverin,

    Appreciate your help!

    But I am afraid that wouldn't solve my problem.

    You seen functions like 'GetWindowText' allows us to get the caption of the application, which includes the application name + the document (if any).

    I want to get the application name only!

    Thanks any way!!!

    Subhendu.

  7. #7
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    Maybe you can use another API that will extract the app name from the EXE file that Kaverin provided? I'm sure there must be an API call for that, as if you choose the properties of a file in Exploere it tells you the app name doesn't it?

  8. #8
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    It took me a while, but I managed to eke out a bit more. Though it still may not be what you're looking for, it's pretty close and worth looking at. This function just accepts an exe name to make it general, but you can use the other function I gave you on a hWnd to get the exe that created the window, and then give that to this function to get a more descriptive title of the application.
    VB Code:
    1. Private Declare Function GetFileVersionInfo Lib "version" Alias "GetFileVersionInfoA" (ByVal lptstrFilename As String, ByVal dwIgnored As Long, ByVal dwLength As Long, ByVal lpData As Long) As Long
    2. Private Declare Function GetFileVersionInfoSize Lib "version" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, ByRef lpdwZero As Long) As Long
    3.  
    4. 'returns a more detailed description of an application, or "" if not found or an error occurs
    5. Private Function GetApplicationName(ByVal strEXEName As String) As String
    6.    Dim lngSize As Long, lngPointer As Long
    7.    Dim abyteBuffer() As Byte, strBuffer As String
    8.    Dim lngStartPos As Long, lngEndPos As Long
    9.    Dim lngZero As Long
    10.    'bail if there's no exe name
    11.    If strEXEName <> "" Then
    12.       'retrieve the size of the file version info
    13.       lngSize = GetFileVersionInfoSize(strEXEName, lngZero)
    14.       'if there is info there...
    15.       If lngSize Then
    16.          'make a buffer
    17.          ReDim abyteBuffer(0 To (lngSize - 1))
    18.          'if the info is retrieved
    19.          If GetFileVersionInfo(strEXEName, 0, lngSize, VarPtr(abyteBuffer(0))) Then
    20.             'convert the data into a string
    21.             strBuffer = StrConv(abyteBuffer, vbUnicode)
    22.             'look for the entry starting with "FileDescription"
    23.             lngStartPos = InStr(strBuffer, "FileDescription")
    24.             'if it was in the data...
    25.             If lngStartPos Then
    26.                'move to a point beyond it (where the data we want is), len("FileDescription")+1=16
    27.                lngStartPos = lngStartPos + 16
    28.                'find the position where that data ends (it is null terminated in the data)
    29.                lngEndPos = InStr(lngStartPos, strBuffer, vbNullChar)
    30.                'pull out that section of the string and return it
    31.                GetApplicationName = Mid$(strBuffer, lngStartPos, lngEndPos - lngStartPos)
    32.             End If
    33.          End If
    34.       End If
    35.    End If
    36. End Function

    *Edit* I forgot to include the two other API declares that this function needs. Didn't realize it until just a few minutes ago.
    Last edited by Kaverin; Sep 21st, 2002 at 02:08 AM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width