Results 1 to 3 of 3

Thread: NOT FOR THE FAINTHEARTED

  1. #1

    Thread Starter
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I'm not sure whether this is going to work in NT or not, i had problems with it in Win2k. Try it
    Code:
    Option Explicit
    
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const MAX_PATH As Long = 260
    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
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlgas As Long, ByVal lProcessID As Long) As Long
    Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
    
    Enum wmGetIconConstants
      ICON_SMALL = 0
      ICON_BIG = 1
    End Enum
    
    Public Const WM_GETICON = &H7F&
    
    'Get active window exe
    Public Function GetExeFromHandle(hwnd As Long) As String
    Dim threadID As Long, processID As Long, hSnapshot As Long, uProcess As PROCESSENTRY32, rProcessFound As Long, i As Integer, szExename As String
        threadID = GetWindowThreadProcessId(hwnd, processID)
        If threadID = 0 Or processID = 0 Then Exit Function
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        If hSnapshot = -1 Then Exit Function
        uProcess.dwSize = Len(uProcess)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        Do While rProcessFound
            If uProcess.th32ProcessID = processID Then
                i = InStr(1, uProcess.szexeFile, Chr(0))
                If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
                Exit Do
            Else
                rProcessFound = ProcessNext(hSnapshot, uProcess)
            End If
        Loop
        Call CloseHandle(hSnapshot)
        GetExeFromHandle = szExename
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  2. #2
    Hyperactive Member
    Join Date
    Jul 2000
    Posts
    352

    Unhappy Unfortunately No

    Unfortunately,it is running into a problem with the snapshot. So I take it there is no other way to trace it. Perhaps do you know how to tell what file is selected in a window. Example: If I click once on the WinNT folder it is the selected file. There must be API code for it because it has to change the display to the highlighted form of the icon. Do you know how to do any of that?

    Thank you for your help,
    Joe

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    This one should work:
    Code:
    '------------------------------------------------------------------
    ' GetHwndEXE()
    '
    ' Written by Aaron Young, 08/10/2000
    '
    ' Extract the EXE Name and Path from a Given Window Handle
    '
    '------------------------------------------------------------------
    '
    'In a Module...
    Private 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 * 260
    End Type
    
    Private Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long
        szCSDVersion As String * 128
    End Type
    
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
    Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
    
    Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    Private Const PROCESS_QUERY_INFORMATION = 1024
    Private Const PROCESS_VM_READ = 16
    Private Const TH32CS_SNAPPROCESS = &H2
    
    Private Function CheckVersion() As Long
        Dim tOS As OSVERSIONINFO
        tOS.dwOSVersionInfoSize = Len(tOS)
        Call GetVersionEx(tOS)
        CheckVersion = tOS.dwPlatformId
    End Function
    
    Public Function GetHwndEXE(ByVal hWnd As Long) As String
        Dim lProcessID As Long, lThread As Long
        Dim lProcessHandle As Long
        Dim sName As String, lModule As Long
        Dim bMore As Boolean, tPROCESS As PROCESSENTRY32
        Dim lSnapShot As Long
        
        lThread = GetWindowThreadProcessId(hWnd, lProcessID)
        
        If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
            'Windows 9x
            'Create a SnapShot of the Currently Running Processes
            lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
            If lSnapShot < 0 Then Exit Function
            
            tPROCESS.dwSize = Len(tPROCESS)
            
            'Enumerate those processes until we find a match
            bMore = Process32First(lSnapShot, tPROCESS)
            While bMore And tPROCESS.th32ProcessID <> lProcessID
                bMore = Process32Next(lSnapShot, tPROCESS)
            Wend
            
            'If a match was found, get the EXE Path and Filename
            If tPROCESS.th32ProcessID = lProcessID Then
                sName = Left$(tPROCESS.szExeFile, InStr(tPROCESS.szExeFile, Chr(0)) - 1)
                GetHwndEXE = sName
            End If
        
        Else
            'Win NT
            'Create an Instance of the Process
            lProcessHandle = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0&, lProcessID)
            
            'If the Process was successfully created, get the EXE
            If lProcessHandle Then
                'Just get the First Module, all we need is the Handle to get the Filename..
                If EnumProcessModules(lProcessHandle, lModule, 4, 0&) Then
                    sName = Space(260)
                    Call GetModuleFileNameExA(lProcessHandle, lModule, sName, Len(sName))
                    GetHwndEXE = sName
                End If
                'Close the Process Handle
                Call CloseHandle(lProcessHandle)
            End If
        End If
    
    End Function

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