This is how to call WIN32 API calls in vbscript and jscript. Sample Window's API functions. RRS feed

  • Question

  • How to call WIN32 API calls in vbscript and jscript. Sample Window's API functions is a follow up to thesetwo articles.




    You can make a COM object with GetForegroundWindow and GetWindowText.

    Put following lines into wso.cls and store is a folder called wso on your desktop.

    Imports System
        Imports System.Runtime.InteropServices
        Imports Microsoft.Win32
        Namespace WindowScriptingObject
            <Guid("7448E08D-ED0F-4E23-B528-91937BB41756"), _
            InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
           Public Interface _WindowScriptingObject
                <DispId(1)> Function ActiveWindow() As Integer
                <DispId(2)> Function WindowText(ByVal hWnd As Integer) As String
            End Interface
            <Guid("B146BF9E-78FC-4DB0-ABFE-9FF026B43E4D"), _
             ClassInterface(ClassInterfaceType.None), _
             ProgId("WindowScriptingObject")> Public Class WindowScriptingObject
                Implements _WindowScriptingObject
                Public WindowScriptingObject()
                Public Declare Auto Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow"() As Integer
                Public Declare Auto Function GetWindowText Lib "user32.dll" (ByVal hwnd As Int32, <Out()> ByVal lpString As System.Text.StringBuilder, ByVal cch As Int32) As Int32
                Public Function ActiveWindow() As Integer Implements _WindowScriptingObject.ActiveWindow
                End Function
                Public Function WindowText(hwnd as Integer) As String Implements _WindowScriptingObject.WindowText
         on error resume next
         Dim b As New System.Text.StringBuilder(ChrW(0), 512)
                        Dim ret = GetWindowText(hWnd, b, b.Capacity)
         WindowText = b.tostring
                End Function
            End Class
        End Namespace
    Then create a bat file in same folder called wso.bat.

        "C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:library /out:"%userprofile%\desktop\wso\wso.dll" "%userprofile%\desktop\wso\wso.cls" /verbose

        "C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm" /codebase "%userprofile%\desktop\wso\wso.dll" /tlb:"%userprofile%\desktop\wso\wso.tlb" /v

        If /i "%cmdcmdline:~0,6%"=="cmd /c" pause

    To use in vbs after running bat file.

        Set wso=CreateObject("WindowScriptingObject")
        x = wso.ActiveWindow
        msgbox x, , "vbs"
        msgbox wso.windowtext(x), , "vbs"

    The GUIDs used here are specific to this project. Do not use them for other purposes.

    More info on what we are doing


    If you must do a per user install, use regasm to make a regfile instead of registering it. Then change all references to `HKCR` to `HKCU\Software\Classes`. Then merge with `regedit /s regfile.reg`.

    To move the file you need to run Regasm on it in it's new location. See command in bat file.

    David Candy

    • Edited by DavidMCandy Monday, October 13, 2014 10:52 PM Change title
    Monday, October 13, 2014 10:16 PM

All replies

  • Really do appreciate the clarity and completeness David.  This is a great help.


    Wednesday, January 24, 2018 1:46 AM
  • For straight forward option for basic win32 function calls is using WSTools. It's an ActiveX component with additions for VBScript/VBA/JScript. https://wizardsoft.nl/products/wstools  . Fully working trial is available and shows you if its useful for a use case.
    Saturday, February 22, 2020 8:23 AM