脚本VBS到Windows API运行

发布于 2025-02-06 07:05:06 字数 1105 浏览 1 评论 0原文

我有一个播放MP3的VBS脚本,正常的事情是通过将MP3文件拖动到VBS或使用命令行指定MP3来调用它。

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"

我需要在不创建VBS文件的情况下调用此代码以使事情变得简单。 可以使用Windows API完成吗?

例如 callvbsfunction(“ mycodevbs”) 并运行吗?

当我指的是Windows API时,我的意思是与这些功能类似

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

...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-02-13 07:05:06

请注意,您的VBScript有错误。

VBScript无法调用API调用。 vb.net可以

将VBScript转换为vb.net

  1. 删除set set关键字。因此,只是objplayer = createObject(“ wmplayer.ocx.7”)

  2. dim每个变量作为对象在API调用中以外。

  3. 将以下行放在文件的顶部。

     进口系统
    进口System.Runtime.InterOpservices
    公共模块任何您的tocallit
    子主
     

并且在底部

End Sub
End Module
  1. 所有参数都必须在括号中,包括sub。在VBScript中,只有函数需要括号,并且潜艇不需要括号。在vb.net中,两个功能和subs都需要支架。

  2. 将处理VBScript转换。注意WScript不可用的顶级对象。对于wscript.arguments使用命令()

要调用API调用

您必须使用声明关键字。您将声明sub main之前和模块之后。

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

您可以从文档中获取声明的函数原型。 https:/ en-us/windows/win32/api/hinuser/nf-winuser-getSystemmetrics

然后在您的代码中

x = GetSystemMetrics(SM_CXSCREEN)

进行编译,输入命令提示符固定文件名,

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

以下是我的博客,我的博客是关于将VBA移植到vb.net时的博客VBA代码。请记住,法律VBScript是法律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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文