Results 1 to 4 of 4

Thread: Error in closing another application...

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Malaysia
    Posts
    43

    Error in closing another application...

    I tried to open Norton Anti Virus using ShellExecute.
    After a few seconds, I tried to close it using SendMessage as below.
    But it didn't work. I've done the declaration for the WM_CLOSE and all
    the Windows API but still didn't work. Before this, I used PostMessage but
    still not working. The return value was 0.
    Can anyone guide me? Any other method of closing another application effectively?
    Maybe an example or two will be greatly appreaciated... Thanks..

    '##################################################################'
    '***'in module
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

    Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Public Declare Function GetActiveWindow Lib "user32" () As Long
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Public Const WM_CLOSE = &H10
    Public Const WM_DESTROY = &H2

    '***'in form

    Dim retval As Long
    Dim hWnd2 As Long

    hWnd2 = GetActiveWindow
    retval = ShellExecute(hWnd2, "open", "C:\Program Files\Norton SystemWorks\Norton AntiVirus\navw32.exe", "c:\myTemp.txt", 0&, SW_MINIMIZE)
    Debug.Print "ShellExecute = " & retval

    Sleep 12000

    hWnd2 = FindWindow(vbNullString, "Norton AntiVirus")
    Debug.Print "findwindow = " & hWnd2

    z = SendMessage(hWnd2, WM_CLOSE, 0, 0 )
    Debug.Print "z = " & z
    z = SendMessage(hWnd2, WM_DESTROY, 0, 0)
    Debug.Print "z = " & z
    '####################################################################
    Ben Chin

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    WM_CLOSE has the same effect as clicking the close icon (X). Does Norton Anti Virus normaly quit if you click that icon?

    There are other ways (but that's not how you should do it) like
    TerminateProcess(handle,returncode)

  3. #3

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Malaysia
    Posts
    43

    Talking

    Thanks a lot for ur opinion.. I think I found out the reason behind this.. There is another Norton running in the background - NortonAntiVirusAutoProtect.. So, each time I send the WM_CLOSE message, it will close the background process and not the Norton Window that I just opened...
    Thanks a lot, anyway...

    Ben Chin

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    For future reference, when closing an running application I would suggest using PostMessage rather than SendMessage. SendMessage function sends the specified message to a window or windows. The function calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately. As a result, PostMessage will react to your request much more quickly.

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