i never used VBScript.
how can i use win32 functions?

Dani AI

Generated

Quick context: pointed to the Microsoft guidance; the failure reported by usually means either the .NET class was never exported/visible to COM or COM activation failed at runtime. Common causes and checks that reliably find the problem:

  • COM visibility: the assembly or type must be marked ComVisible. Confirm either an assembly-level attribute or a class-level attribute is present so regasm can export the type.
  • Bitness/registry mismatch: on 64-bit Windows register with the 64-bit regasm (Framework64) if the script runs under 64-bit wscript/cscript; use the 32-bit regasm if using the 32-bit host. Registering with the wrong regasm leaves no ProgID for the host being used.
  • Permissions: regasm must be run elevated to write HKCR entries (UAC/admin).
  • Activation errors: if the managed constructor or any module initializer throws an exception, CreateObject fails with the same ActiveX error. Check the Application event log for .NET exceptions or add logging in the constructor.
  • Verification: run regasm with /verbose and inspect HKCR\WindowScriptingObject and HKCR\CLSID{your-guid} to confirm entries exist.

Small VB.NET habits that avoid activation surprises:

<Assembly: System.Runtime.InteropServices.ComVisible(True)>
Public Sub New()
    Try
        ' keep constructor minimal; log any error to a file instead of letting it escape
    Catch ex As Exception
        System.IO.File.AppendAllText("C:\Temp\wso_error.log", ex.ToString())
        Throw
    End Try
End Sub

Rebuild, re-register with the matching regasm.exe (and elevated), confirm registry keys exist, and check the event log if CreateObject still fails.

Recommended Answers

All 2 Replies

i created the cls file:

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
     ActiveWindow=GetForegroundWindow()

            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

and the bat file:

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

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

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

after click on bat file the dll and tlb file was created.
heres the sample:

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

error message:
'the ActiveX component can't create a 'WindowScriptingObject' object'

so what i'm doing wrong?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.