Results 1 to 7 of 7

Thread: Simple SendMessage prob or killing a window

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2001
    Posts
    1

    Simple SendMessage prob or killing a window

    Ok i am writing a script that basically terminates a program and restarts it anyway the problem is i cannot terminate it. I have tried:
    SendMessage lhwnd, &H2, 0, 0
    where lhwnd is the hwnd of the window and in theory H2 should be the correct hex for WM_DESTROY but that doesnt work. It gets the right hwnd and the program does respond since if i do:
    SendMessage lhwnd, &H10, 0, 0
    (H10 being WM_CLOSE which is a weaker form of destroy) it prompts me if i want to exit while connected. Anyway it works if i do it in C++ but unfortunately their are parts of this app that would be a ***** to do in C++ anyway any suggestions or better ways to kill a window

  2. #2
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    I think the best way is to simulate what happens when the user closes the window - send a WM_SYSCOMMAND message with SC_CLOSE flag ....

    Dr Memory
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  3. #3
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    To close a Window

    SendMessage wHandler, WM_CLOSE,0,0

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Use PostMessage To Close The Window, and you need to know its caption

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    3.  
    4. Private Const WM_CLOSE = &H10
    5.  
    6. Private Sub cmdCloseApp_Click()
    7. Dim CloseIt As Long
    8. CloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    9. PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    10. End Sub

  5. #5
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    It's also a good idea to specify a classname for a the window. It narrows the search down a little more.

  6. #6
    Addicted Member MathImagics's Avatar
    Join Date
    Jun 2002
    Location
    Uki, NSW Australia
    Posts
    169
    I'd still suggest that using WM_SYSCOMMAND/SC_CLOSE is more likely to close an application down cleanly than WM_CLOSE. In automation contexts I've found that sometimes WM_CLOSE just ain't enough, but if it works in specific cases and that's all you want, nyet problyema!!
    "He's got a B.A. (in be-bop), a Ph.D. (in swing), he's a Master of Rhythm, he's the Rock'n'Roll king" ("The Rock'n'Roll Doctor", Lowell George)

    "If you push something hard enough, it will fall over" (Fudd's Third Law of Opposition)

  7. #7
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    WM_SYSCOMMAND/SC_CLOSE is the default method of closing an app. WM_CLOSE is more of a brute-force method of closing. But if that doesn't work, try WM_DESTROY and WM_QUIT. If those don't work, use TerminateProcess! It's a little different approach (you're dealing with the process now, not windows) but you know it's a sure shot.

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