|
-
Sep 16th, 2002, 01:35 AM
#1
Thread Starter
Lively Member
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.
-
Sep 16th, 2002, 05:34 AM
#2
Frenzied Member
I think this is what you want:
VB Code:
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Public Function GetWindowTitle(ByVal hWnd As Long) As String
GetWindowTitle = String(GetWindowTextLength(hWnd) + 1, Chr$(0))
Call GetWindowText(hWnd, GetWindowTitle, Len(GetWindowTitle))
End Function
-
Sep 17th, 2002, 12:05 AM
#3
Thread Starter
Lively Member
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.
-
Sep 17th, 2002, 05:29 AM
#4
Frenzied Member
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?
-
Sep 19th, 2002, 12:22 AM
#5
Fanatic Member
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:
'in a module
Option Explicit
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Public Const TH32CS_SNAPPROCESS As Long = &H2
Public Const MAX_PATH As Integer = 260
Public Const SYNCHRONIZE As Long = &H100000
Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Public Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
'returns the exe name of the hWnd's process, or "" if it can't be determined
Public Function GetEXEName(ByVal hWnd As Long) As String
Dim PID As Long
Dim hProcess As Long
Dim hSnapshot As Long
Dim peInfo As PROCESSENTRY32
Dim lngReturn As Long
Dim blnFound As Boolean
'if the hWnd is valid
If hWnd Then
'get the PID of the window's thread
If GetWindowThreadProcessId(hWnd, PID) Then
'attempt to get a handle to the process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, PID)
If hProcess Then
'make a snapshot of the processes
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, PID)
If hSnapshot Then
'set the size of the structure
peInfo.dwSize = Len(peInfo)
'get the first process in the snapshot
lngReturn = Process32First(hSnapshot, peInfo)
'loop until there are no more
Do While lngReturn
'if the current PID matches the PID we have already, stop the loop
If peInfo.th32ProcessID = PID Then
blnFound = True
Exit Do
End If
'get the next process
lngReturn = Process32Next(hSnapshot, peInfo)
Loop
If blnFound Then
'trim off the null terminator and return it
GetEXEName = Left$(peInfo.szExeFile, InStr(peInfo.szExeFile, vbNullChar) - 1)
End If
'close the snapshot handle
CloseHandle hSnapshot
End If
'close the process handle
CloseHandle hProcess
End If
End If
End If
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 
-
Sep 19th, 2002, 03:50 AM
#6
Thread Starter
Lively Member
-
Sep 19th, 2002, 04:44 AM
#7
Frenzied Member
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?
-
Sep 20th, 2002, 03:16 AM
#8
Fanatic Member
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:
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
Private Declare Function GetFileVersionInfoSize Lib "version" Alias "GetFileVersionInfoSizeA" (ByVal lptstrFilename As String, ByRef lpdwZero As Long) As Long
'returns a more detailed description of an application, or "" if not found or an error occurs
Private Function GetApplicationName(ByVal strEXEName As String) As String
Dim lngSize As Long, lngPointer As Long
Dim abyteBuffer() As Byte, strBuffer As String
Dim lngStartPos As Long, lngEndPos As Long
Dim lngZero As Long
'bail if there's no exe name
If strEXEName <> "" Then
'retrieve the size of the file version info
lngSize = GetFileVersionInfoSize(strEXEName, lngZero)
'if there is info there...
If lngSize Then
'make a buffer
ReDim abyteBuffer(0 To (lngSize - 1))
'if the info is retrieved
If GetFileVersionInfo(strEXEName, 0, lngSize, VarPtr(abyteBuffer(0))) Then
'convert the data into a string
strBuffer = StrConv(abyteBuffer, vbUnicode)
'look for the entry starting with "FileDescription"
lngStartPos = InStr(strBuffer, "FileDescription")
'if it was in the data...
If lngStartPos Then
'move to a point beyond it (where the data we want is), len("FileDescription")+1=16
lngStartPos = lngStartPos + 16
'find the position where that data ends (it is null terminated in the data)
lngEndPos = InStr(lngStartPos, strBuffer, vbNullChar)
'pull out that section of the string and return it
GetApplicationName = Mid$(strBuffer, lngStartPos, lngEndPos - lngStartPos)
End If
End If
End If
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|