Click here to Skip to main content
Click here to Skip to main content
Go to top

DestroyWindow in VBScript

, 28 Nov 2006
Rate this:
Please Sign up or sign in to vote.
Destroys or Kills a Window using VBScript

Sample Image - gopal_code.gif

Introduction

Destroying or killing a window using VBScript on Microsoft Windows platforms is a bit tricky. The Windows APIs CloseWindow and DestroyWindow are misleading. CloseWindow just minimizes a window and DestroyWindow works only when the DestroyWindow API is called from the same thread which has created the window you are trying to close. As I want to destroy or kill a window from VBScript, the DestroyWindow thread will not be the same as of the Window I am trying to destroy or kill. So, I cannot use the DestroyWindow API. MSDN documents it as "A thread cannot use DestroyWindow to destroy a window created by a different thread". On the web, I found some articles which suggest to use DestroyWindow from VB or VBScript. At least the DestroyWindow did not work for me. The presented code is supposed to work on all Windows platforms such as Windows 95, 98, 2000, XP, 2003, Vista, but I have tested on XP only. Of course, Windows API cannot be used directly in VBScript, so I embedded the API in a small COM DLL which I developed using VB 6. Rather than using DestroyWindow, I used SendMessage with WM_CLOSE, which works.

Objective

How to destroy or kill a window using VBScript on Microsoft Windows platforms.

How To Do It With the Demo Project

Step 1: Download extract the demo project in any folder on your machine.

Step 2: If MSVBVM60.dll is not present in the system32 folder of your machine, download from this link, and then copy the file msvbvm60.dll into the system32 folder of your machine.

Step 3: Double click the file reg.bat from the location where you extracted the demo project.

Step 4: Double click on the test.txt file.

Step 5. Double click on the file destroy_window.vbs.

You will find that the open window of test.txt will be closed and destroyed.

Source Code for the COM DLL

Create a new ActiveX DLL project. Copy and paste the following code in code view of the module:

Option Explicit
Private Declare Function FxFindWindow Lib "user32" Alias "FindWindowA" _
(ByVal szClassName As String, ByVal szWindow As String) As Long

Private Declare Function FxSendMessage Lib "user32" Alias "SendMessageA" _
   (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
   As Long, ByVal lParam As Long) As Long

Private Const WM_CLOSE = &H10

Public Function FindWindow(ByVal CaptionString As String) As Long
    FindWindow = FxFindWindow(vbNullString, CaptionString)
End Function

Public Function KillWindow(ByVal winHandle As Long) As Long
    KillWindow = FxSendMessage(winHandle, WM_CLOSE, 0, 0)
End Function

VBScript to Destroy or Kill the Window

Set obj = CreateObject("APIWrapperCOM.APIWrapper")
winHandle = obj.FindWindow("test.txt - Notepad")
obj.KillWindow(winHandle)

Registration of the COM DLL and Dependency

As mentioned above, the Visual Basic generated COM DLL has dependency on MSVBVM60.dll which can be freely downloaded from the above link. Once the MSVBVM60.dll file is present in the system32 folder of your machine, you can use the regsvr32 command to register the DLL. This command is included in the reg.bat file in the demo project.

History

  • 28th November, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gopal Singh - avlands.com
Software Developer (Senior)
United States United States
Education: PMP, MCP/MCTS, SCPM(advanced project management, Stanford University - continued), MBA (Global Management), B.S. (Electronics & Tele-communication).
 
Professional skill: Project management, Software development, and software deployment.
 
Interest: Music composition, lyric writing, and singing.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140921.1 | Last Updated 28 Nov 2006
Article Copyright 2006 by Gopal Singh - avlands.com
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid