2

I have a vbs script that plays an mp3, the normal thing would be to call it by dragging the mp3 file to the vbs or specify the mp3 with the command line.

Set objArgs = Wscript.Arguments
if (objArgs.Count = 0) then
    Wscript.echo "Necesito un archivo MP3"
    WScript.Quit 123
end if
'Wscript.echo "Playing: " & objArgs(0) & "..."
Set objPlayer = createobject("Wmplayer.OCX.7")
With objPlayer  ' saves typing
    .settings.autoStart = True
    .settings.volume = 100  ' 0 - 100
    .settings.balance = 0  ' -100 to 100
    .settings.enableErrorDialogs = False
    .enableContextMenu = False
    .URL = objArgs(0)
    WScript.Sleep(10000)  ' time to load and start playing
    '.Controls.Pause()  ' stop
End With
WScript.Quit 0
' MsgBox "if WMP is still playing, clicking OK will end it", _
' vbInformation, "WMP Demo finished"

I need to call this code without creating the vbs file to keep things simple. Can this be done with Windows APIs?

For example callvbsfunction("mycodevbs") and run it?

When I refer to windows api I mean functions similar to these

MessageBox( 0, "Test", "Title here", MB_OK )
GetSystemMetrics( SM_CXSCREEN )
ShellExecute( 0, "open", "c:\myfile.txt", 0, 0, 1 )
Beep( 1000, 250 )

and more...

Club
  • 111
  • 1
  • 8
  • You can utilise Windows API using a [custom COM capable DLL](https://stackoverflow.com/a/9846377/692942). Unfortunately accessing the Windows API out of the box with VBScript isn’t an option. – user692942 Jun 10 '22 at 07:18
  • @user692942 Are you going to write this COM capable DLL even though .NET makes is difficult to register COM capable DLLs. – user18521918 Jun 10 '22 at 08:42
  • @user18521918 If they want to use Windows APIs in VBScript, that is the process. – user692942 Jun 10 '22 at 12:45

1 Answers1

0

Note you VBScript has errors.

VBScript cannot call API calls. VB.Net can

To convert VBScript to VB.Net

  1. Remove the Set keyword. So just objPlayer = createobject("Wmplayer.OCX.7").

  2. Dim every variable as Object except in API calls.

  3. Put the following lines at the top of the file.

    Imports System
    Imports System.Runtime.InteropServices
    Public Module AnythingYouWantToCallIt
    Sub Main
    

and at the bottom

End Sub
End Module
  1. All parameters must be in brackets including Sub. In VBScript only Functions require brackets and Subs require no brackets. In VB.Net both Functions and Subs require brackets.

  2. That will handle VBScript conversion. Note the WScript top level object is not available. For WScript.Arguments use Command().

To Call API Calls

You have to use the declare keyword. You put Declare before Sub Main and after Module.

Public Declare Function Lib "User32" GetSystemMetrics(ByVal SM as Integer) As Integer
Const SM_CXSCREEN = 0

You get the function prototype for the Declare from the documentation. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics

Then in your code

x = GetSystemMetrics(SM_CXSCREEN)

To compile, type in a command prompt fixing filenames

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:winexe /out:"%~dp0\DeDup.exe" "%~dp0\DeDup.vb" 

Here is my blog about porting VBA to VB.Net while writing in a VBA code. Remember legal VBScript is legal VBA. https://winsourcecode.blogspot.com

   Imports System
   Imports System.Runtime.InteropServices
   Public Module AnythingYouWantToCallIt

   Public Declare Function GetSystemMetrics Lib "User32" (ByVal SM as Integer) As Integer
   Const SM_CXSCREEN = 0

   Sub Main
       Dim X as Object
       MsgBox( "Test", 0, "Title here")
       x = GetSystemMetrics( SM_CXSCREEN )
       Msgbox(X) 
   End Sub
   End Module
user18521918
  • 102
  • 1
  • 1
  • 3
  • 1
    VBScript can call APIs via COM capable DLLs through the`CreateObject()` method. I’m not sure how someone asking how to do this in VBScript translates to rewriting their script in VB.Net? What if they don’t have access to VB.Net? VBScript is baked into the OS, bit of a leap to suggest converting their code to VB.Net. Why not Python, C++? If they used C++ they’d have complete access to the Windows API. – user692942 Jun 10 '22 at 07:25
  • VB.Net is baked into the OS. Everyone has access to the compilers built in to Windows. VBScript is 99% VB.Net. So **1** Search and Replace `Set` with nothing. **2** Put brackets around `Sub` calls. **3** Dim all variables `as Object`. **4** Add the standard 6 lines to the file. **Then a VBScript is an VB.Net program**. VB.Net was designed that way. @user692942 – user18521918 Jun 10 '22 at 07:44
  • @user692942 `"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe"` There are 4 VB.Net (VBC.exe), 4 C# (CSC.exe), and 4 JScript.Net (JSC.exe) compilers built in to Windows. – user18521918 Jun 10 '22 at 07:54
  • Here is my blog about porting VBA to VB.Net while writing VBA.. Remember legal VBScript is legal VBA. https://winsourcecode.blogspot.com/ – user18521918 Jun 10 '22 at 07:58
  • @user692942 In .Net Ver 1 Beta both VB.Net and C# were also scripting languages. – user18521918 Jun 10 '22 at 08:10
  • Just don't see the argument for porting everything to VB.Net. – user692942 Jun 10 '22 at 08:12
  • He wants to call API calls so either VB.Net or VBA/VB6. Only one is installed by default. – user18521918 Jun 10 '22 at 08:26