Results 1 to 7 of 7

Thread: Hwnd?

  1. #1
    Guest
    Is there anyway to get a programs hwnd just by knowing its file path and extension (Ex: C:\Windows\Calc.exe)? I dought there is, but I'm just wondering.

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Lightbulb

    Window Handle (hWnd) is being assign by windows when you run the program. So, if the program is not running, then it won't have any hWnd.

  3. #3
    Guest
    Well, say if i wanted to get the running tasks..which I know how to do, how could i tell what their handles are? Like when i get the running tasks, it only returns their file path. Anyway to determine it then?

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    You can get the running exe file from the wnd, i have the code it you're looking for it
    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.

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Lightbulb

    Here is how to get running processes:

    Module Code
    Code:
    Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Public Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
    Public Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
    Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Long
    Public Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long           ' This process
        th32DefaultHeapID As Long
        th32ModuleID As Long            ' Associated exe
        cntThreads As Long
        th32ParentProcessID As Long     ' This process's parent process
        pcPriClassBase As Long          ' Base priority of process threads
        dwFlags As Long
        szExeFile As String * 260       ' MAX_PATH
    End Type
    Public Type OSVERSIONINFO
        dwOSVersionInfoSize As Long
        dwMajorVersion As Long
        dwMinorVersion As Long
        dwBuildNumber As Long
        dwPlatformId As Long           '1 = Windows 95, 2 = Windows NT
        szCSDVersion As String * 128
    End Type
    Public Const PROCESS_QUERY_INFORMATION = 1024
    Public Const PROCESS_VM_READ = 16
    Public Const MAX_PATH = 260
    Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Public Const SYNCHRONIZE = &H100000
    'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
    Public Const PROCESS_ALL_ACCESS = &H1F0FFF
    Public Const TH32CS_SNAPPROCESS = &H2&
    Public Const hNull = 0
    Public Function GetRunningProcesses() As Variant
        Dim arrProc()
        Dim intIndex As Integer
        Dim lRet As Long
        
        Select Case getVersion()
            Case 1 'Windows 95/98
                Dim strProcess As String
                Dim hSnap As Long, udtProc As PROCESSENTRY32
            
                hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
                If hSnap = hNull Then Exit Function
                udtProc.dwSize = Len(udtProc)
                ' Iterate through the processes
                lRet = Process32First(hSnap, udtProc)
                Do While lRet
                    strProcess = StripNulls(udtProc.szExeFile)
                    ReDim Preserve arrProc(intIndex)
                    arrProc(intIndex) = strProcess
                    intIndex = intIndex + 1
                    lRet = Process32Next(hSnap, udtProc)
                Loop
            Case 2 'Windows NT
                Dim lcb As Long, lcbNeeded As Long, lNumElements As Long
                Dim lProcessIDs() As Long, lcbNeeded2 As Long
                Dim lModules(1 To 200) As Long
                Dim strModuleName As String
                Dim lSize As Long, lhProcess As Long, i As Long
                
                'Get the array containing the process id's for each process object
                lcb = 8
                lcbNeeded = 96
                Do While lcb <= lcbNeeded
                    lcb = lcb * 2
                    ReDim lProcessIDs(lcb / 4) As Long
                    lRet = EnumProcesses(lProcessIDs(1), lcb, lcbNeeded)
                Loop
                lNumElements = lcbNeeded / 4
                For i = 1 To lNumElements
                    'Get a handle to the Process
                    lhProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, lProcessIDs(i))
                    'Got a Process handle
                    If lhProcess <> 0 Then
                        'Get an array of the module handles for the specified
                        'process
                        lRet = EnumProcessModules(lhProcess, lModules(1), 200, _
                        lcbNeeded2)
                        'If the Module Array is retrieved, Get the ModuleFileName
                        If lRet <> 0 Then
                            strModuleName = Space(MAX_PATH)
                            lSize = 500
                            lRet = GetModuleFileNameExA(lhProcess, lModules(1), _
                            strModuleName, lSize)
                            ReDim Preserve arrProc(intIndex)
                            arrProc(intIndex) = Left(strModuleName, lRet)
                            intIndex = intIndex + 1
                        End If
                    End If
                    'Close the handle to the process
                    lRet = CloseHandle(lhProcess)
                Next
        End Select
        GetRunningProcesses = arrProc
    End Function
    
    
    Function StripNulls(pstrString As String) As String
             StripNulls = Left(pstrString, InStr(pstrString, vbNullChar) - 1)
    End Function
    
    Public Function getVersion() As Long
        Dim udtOSVersion As OSVERSIONINFO
        Dim lRet As Integer
        
        udtOSVersion.dwOSVersionInfoSize = 148
        udtOSVersion.szCSDVersion = Space$(128)
        lRet = GetVersionExA(udtOSVersion)
        getVersion = udtOSVersion.dwPlatformId
    End Function

    Form Code (Requires Listbox - List1 and Commandb button - Command1)
    Code:
    Private Sub Command1_Click()
        Dim i As Integer
        Dim arrProc()
        
        List1.Clear
        arrProc = GetRunningProcesses
        For i = 0 To UBound(arrProc)
            List1.AddItem arrProc(i)
        Next
    End Sub

  6. #6
    Guest
    Ok Kedaman, post it up...I'll take it. Serge, I know how to get the running processes..but I guess there's no way to determine the hwnd by knowing the file path. But I am still interested in Kedaman's code. Thanks all anyway.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    OK, here comes the code:
    Code:
    'Get active window caption
    Public Declare Function GetForegroundWindow Lib "user32" () As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    'Get active window exe
    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)
    
    'Get active window caption
    Public Function GetWindowCaption(ByVal hwnd As Long, ByVal lParam As Long) As String
        Dim lWin As Long
        Dim strWinText As String
        strWinText = Space(255)
        lWin = GetWindowText(hwnd, strWinText, Len(strWinText))
        If lWin Then strWinText = Left(strWinText, lWin)
        GetWindowCaption = strWinText
    End Function
    
    'Get active window exe
    Public Function GetExeFromHandle(hwnd As Long) As String
        Dim threadID As Long, processID As Long, hSnapshot As Long
        Dim uProcess As PROCESSENTRY32, rProcessFound As Long
        Dim i As Integer, szExename As String
        ' Get ID for window thread
        threadID = GetWindowThreadProcessId(hwnd, processID)
        ' Check if valid
        If threadID = 0 Or processID = 0 Then Exit Function
        ' Create snapshot of current processes
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        ' Check if snapshot is valid
        If hSnapshot = -1 Then Exit Function
        'Initialize uProcess with correct size
        uProcess.dwSize = Len(uProcess)
        'Start looping through processes
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        Do While rProcessFound
            If uProcess.th32ProcessID = processID Then
                'Found it, now get name of exefile
                i = InStr(1, uProcess.szexeFile, Chr(0))
                If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
                Exit Do
            Else
                'Wrong ID, so continue looping
                rProcessFound = ProcessNext(hSnapshot, uProcess)
            End If
        Loop
        Call CloseHandle(hSnapshot)
        GetExeFromHandle = szExename
    End Function
    And to use it:
    Code:
    GetExeFromHandle mywnd&
    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.

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