script vbs to windows api run

Viewed 214

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...

1 Answers

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://docs.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
Related