脚本VBS到Windows API运行
我有一个播放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 examplecallvbsfunction("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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,您的VBScript有错误。
VBScript无法调用API调用。 vb.net可以
将VBScript转换为vb.net
删除
set set
关键字。因此,只是objplayer = createObject(“ wmplayer.ocx.7”)
。dim
每个变量作为对象
在API调用中以外。将以下行放在文件的顶部。
并且在底部
所有参数都必须在括号中,包括
sub
。在VBScript中,只有函数需要括号,并且潜艇不需要括号。在vb.net中,两个功能和subs都需要支架。将处理VBScript转换。注意
WScript
不可用的顶级对象。对于wscript.arguments
使用命令()
。要调用API调用
您必须使用
声明
关键字。您将声明
在sub main
之前和模块
之后。您可以从文档中获取
声明
的函数原型。 https:/ en-us/windows/win32/api/hinuser/nf-winuser-getSystemmetrics然后在您的代码中
进行编译,输入命令提示符固定文件名,
以下是我的博客,我的博客是关于将VBA移植到vb.net时的博客VBA代码。请记住,法律VBScript是法律VBA。 https://winsourcecode.blogspot.com
Note you VBScript has errors.
VBScript cannot call API calls. VB.Net can
To convert VBScript to VB.Net
Remove the
Set
keyword. So justobjPlayer = createobject("Wmplayer.OCX.7")
.Dim
every variableas Object
except in API calls.Put the following lines at the top of the file.
and at the bottom
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.That will handle VBScript conversion. Note the
WScript
top level object is not available. ForWScript.Arguments
useCommand()
.To Call API Calls
You have to use the
declare
keyword. You putDeclare
beforeSub Main
and afterModule
.You get the function prototype for the
Declare
from the documentation. https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetricsThen in your code
To compile, type in a command prompt fixing filenames
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