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.
Printable View
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.
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
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.
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?
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
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.
:) :) :)
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?
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.