Results 1 to 16 of 16

Thread: [Partly Resolved] Executing & getting hwnd

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    [Partly Resolved] Executing & getting hwnd

    Hey i know how to manipulate windows, but my problem is getting the hWnd for a window. I'm working on a program that executes other outside programs and then manipulates the size & the window state.

    I need to know how to launch a program using the Shell command (with out knowing the caption, or at least find it when it's opened) so that i can manipulate the window. The program can either launch ontop or behind, minimized, normal, maximized or full screen. But my only problem is finding the hWnd.

    I've checked for code on here for ages and i can't find any that works. Can some one please shed some light for me?
    Last edited by Slyke; Apr 30th, 2006 at 10:14 AM.

  2. #2
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    But there's no way to get the hWnd by executing it, or the caption after it's been executed (since it may go behind or ontop).

  4. #4
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Executing & getting hwnd

    It doesnt matter what the ZOrder of the window is or even if its visible or not. Using the FindWindow API you can get the windows handle after its created.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    How? Where's the hWnd stored? How can i find that exact exe that i executed?

  6. #6
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Executing & getting hwnd

    Use FindWindow and pass the window classname of your desired window (does not change and can be found with Spy++) and the window caption if you want. If you have multiple windows of the same class name then you will need to enumerate them and choose the desired one.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    umm, Is it possible to get the hWnd automatically? I'm remaking a program that stops games going full screen (like CS or what ever other games do) by grabbing the hWnd of it & stopping it. The only problem is that i don't know the caption of every game & moving the mouse onto it with that Spy++ thing won't work because i'd need to give it out & tell people how to use it. I know it is possible DxWnd is where i got the idea from, but i'm making my own version of it. Is somthing like this even possible in VB? I wouldn't know...

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Executing & getting hwnd

    here's a Shell2hWnd function:

    VB Code:
    1. Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
    2.                 ByVal hwnd As Long, _
    3.                 lpdwProcessId As Long) As Long
    4.                
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    6.                 ByVal lpClassName As String, _
    7.                 ByVal lpWindowName As String) As Long
    8.                
    9. Private Declare Function GetWindow Lib "user32" ( _
    10.                 ByVal hwnd As Long, _
    11.                 ByVal wCmd As Long) As Long
    12.                
    13. Private Declare Function GetParent Lib "user32" ( _
    14.                 ByVal hwnd As Long) As Long
    15.  
    16. Private Const GW_HWNDNEXT = 2
    17.  
    18. Public Function Shell2hWnd(ByVal sFileName As String, Optional ByVal Mode As VbAppWinStyle) As Long
    19.     Dim lTaskID As Long, lProcID As Long
    20.    
    21.     lTaskID = Shell(sFileName, Mode)
    22.     If lTaskID = 0 Then Exit Function
    23.    
    24.     Shell2hWnd = FindWindow(vbNullString, vbNullString)
    25.     Do While Shell2hWnd
    26.         If GetParent(Shell2hWnd) = 0 Then
    27.             GetWindowThreadProcessId Shell2hWnd, lProcID
    28.             If lProcID = lTaskID Then Exit Function
    29.         End If
    30.         Shell2hWnd = GetWindow(Shell2hWnd, GW_HWNDNEXT)
    31.     Loop
    32.     Shell2hWnd = 0
    33. End Function
    34.  
    35. Private Sub Form_Load()
    36.     Dim lhWnd As Long
    37.     lhWnd = Shell2hWnd("notepad.exe", vbNormalFocus)
    38. End Sub

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    Yes, this works, but only somtimes! I used that code on a dud program i made for testing (just nothing in the program) and somtimes it gets the correct hWnd, but other times it doesn't. I have code checking the hWnd of the window from it's caption (because i'm testing) sometimes the hWnds match, other times they don't. Sometimes that code works with that hWnd, somtimes the other hWnd works, sometimes none of them work. The code is still in early development so there's nothing screwing around with any processes or memory.

    Why does this happen?

    Thank you for your help by the way.

  10. #10
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,713

    Re: Executing & getting hwnd

    If you checking the window caption for verification then why not use code like the second link I posted earlier?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    Because i was testing it (sometimes it worked & sometimes it didn't) so i used a different code so check what wasn't working, which needed a window caption.

    I don't know the window captoin, but while i'm testing it i do it in controled enviroment so that i can find out what's wrong.

  12. #12
    Addicted Member
    Join Date
    Apr 2006
    Posts
    155

    Re: Executing & getting hwnd

    You could do it like this:, Use a timer and get the mouse position then get the hWnd with API Function WindowFromPoint.

    ex:

    Code:
    Private Declare Function WindowFromPoint Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetCursor Lib "user32" Alias "GetCursor" () As Long
    Private Type POINTAPI
    x As Long
    y As Long
    End Type
    
    Private Sub Timer1_Timer()
      Dim lp As POINTAPI, value As Long
        GetCursorPos lp
          value& = WindowFromPoint(lp.x,lp.y)
            Label1.Caption = value&
    End Sub

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Executing & getting hwnd

    So like make the mouse jump onto the program once it's started? ok, that will have to do, thank you!

  14. #14
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Executing & getting hwnd

    Quote Originally Posted by bushmobile
    here's a Shell2hWnd function:

    VB Code:
    1. Private Declare Function GetWindowThreadProcessId Lib "user32" ( _
    2.                 ByVal hwnd As Long, _
    3.                 lpdwProcessId As Long) As Long
    4.                
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    6.                 ByVal lpClassName As String, _
    7.                 ByVal lpWindowName As String) As Long
    8.                
    9. Private Declare Function GetWindow Lib "user32" ( _
    10.                 ByVal hwnd As Long, _
    11.                 ByVal wCmd As Long) As Long
    12.                
    13. Private Declare Function GetParent Lib "user32" ( _
    14.                 ByVal hwnd As Long) As Long
    15.  
    16. Private Const GW_HWNDNEXT = 2
    17.  
    18. Public Function Shell2hWnd(ByVal sFileName As String, Optional ByVal Mode As VbAppWinStyle) As Long
    19.     Dim lTaskID As Long, lProcID As Long
    20.    
    21.     lTaskID = Shell(sFileName, Mode)
    22.     If lTaskID = 0 Then Exit Function
    23.    
    24.     Shell2hWnd = FindWindow(vbNullString, vbNullString)
    25.     Do While Shell2hWnd
    26.         If GetParent(Shell2hWnd) = 0 Then
    27.             GetWindowThreadProcessId Shell2hWnd, lProcID
    28.             If lProcID = lTaskID Then Exit Function
    29.         End If
    30.         Shell2hWnd = GetWindow(Shell2hWnd, GW_HWNDNEXT)
    31.     Loop
    32.     Shell2hWnd = 0
    33. End Function
    34.  
    35. Private Sub Form_Load()
    36.     Dim lhWnd As Long
    37.     lhWnd = Shell2hWnd("notepad.exe", vbNormalFocus)
    38. End Sub
    Its cool to see code you write, appear months later with a different name

    http://vbforums.com/showthread.php?t...t=Shellandfind

  15. #15
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: [Partly Resolved] Executing & getting hwnd

    man, people can be bitchy, this was where i modified it from: http://www.digital-inn.de/visual-bas...s-steuern.html

  16. #16
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [Partly Resolved] Executing & getting hwnd

    Quote Originally Posted by bushmobile
    man, people can be bitchy, this was where i modified it from: http://www.digital-inn.de/visual-bas...s-steuern.html
    Man you know I was only kidding

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