Results 1 to 8 of 8

Thread: [Resolved] Launch Program and Get hWnd

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    [Resolved] Launch Program and Get hWnd

    How can I launch an application and get it's hWnd? I can't simply use FindWindow(), because there is more than one open window with the the caption.

    Is there a function to launch an application that returns the hWnd? Or any other method?
    Last edited by The Hobo; May 20th, 2003 at 12:04 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    the app youre launching doesnt have a hWnd by itself. apps are identified by their process id's, but their windows (and other objects inside them) are identified by hWnd's.

    it's not very clear which hWnd you are trying to get: do you need a hWnd in your own application? (that should be simple, because the forms and most of the controls expose their hWnd as a property). or your application starts another application (with Shell or something) and you need to capture a hWnd from that started app?
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Launch Program and Get hWnd

    Originally posted by The Hobo
    How can I launch an application and get it's hWnd? I can't simply use FindWindow(), because there is more than one open window with the the caption.

    Is there a function to launch an application that returns the hWnd? Or any other method?
    you could enum the windows and get each hwnd for each window of the same caption... but as like radum said.. each window will have its own handle.. so there is no way to get an hwnd for a multiple window application... process ID may be the way to go.. or are you trying to manipulate certain windows in this application?

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Okay...

    I want to launch a window/application/box with a title. Such as Shell(filename).

    What I then want to do is get the hWnd of the window, so I can later CloseWindow() said window.

    However, since there is more than one application/window running with that title (I see three when I press alt+ctrl+del), FindWindow() does not always return the one that I launched.

    Sorry if my terms are not correct, but I'm hoping this helps explain.

    It is not a multiple window application, there just happens to be two programs running with the same caption.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    you could always just end task on it via code.. as long as it is not processing anything at the time.. it will close like normal (otherwise you will get the not responding message)

    OR

    you can enum all the desktop windows and send a close message to each that match the caption you are looking for

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by kleinma
    you could always just end task on it via code.. as long as it is not processing anything at the time.. it will close like normal (otherwise you will get the not responding message)
    How do I "end task on it via code?"

    Originally posted by kleinma
    you can enum all the desktop windows and send a close message to each that match the caption you are looking for
    But I don't want to close the other ones. Just the one that I launched.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    this is more or less a brutal method to close an application already running. just copied-pasted it from one of my apps, but i have it from elsewhere... dont remember where. let me know if you need the API declarations part as well. you would call this sub with the process ID returned by the vb Shell() function. Also, take care of the GetWindow() API function call: you would have to substitute UserControl.ContainerHwnd with some hWnd of the application that attempts to close the shelled app (should be a Me.hWnd or something).
    VB Code:
    1. Sub StopApplication(ByVal procID As Long)
    2. 'shuts down a shelled application
    3. 'procID is the value obtained from the Shell() function
    4. Dim hwCurr As Long
    5. Dim getPid As Long
    6. Dim lPid As Long
    7. Dim lHp As Long
    8.  
    9. 'Start enumeration of top level windows, get first handle
    10. hwCurr = GetWindow(UserControl.ContainerHwnd, GW_HWNDFIRST)
    11. Do While hwCurr
    12.     'Get the Pid from the window handle
    13.     GetWindowThreadProcessId hwCurr, getPid
    14.     If getPid = procID And IsWindowVisible(hwCurr) Then
    15.         'If the Pid's match and the window is visible, close it down
    16.         SendMessageTimeout hwCurr, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    17.         'If the window doesn't like gentle persuasion,
    18.         'bring out the nipple clamps to force it to close
    19.         If IsWindow(hwCurr) Then
    20.             Call GetWindowThreadProcessId(hwCurr, lPid)
    21.             lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    22.             TerminateProcess lHp&, 0&
    23.             CloseHandle lHp
    24.         End If
    25.     Else
    26.         'Continue enumeration
    27.         hwCurr = GetWindow(hwCurr, GW_HWNDNEXT)
    28.     End If
    29. Loop
    30.  
    31. End Sub
    there are 2 reasons why i leave my work unfinished:
    (1) i'm getting old.

  8. #8

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Perfect. Thanks.
    My evil laugh has a squeak in it.

    kristopherwilson.com

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