我有一个播放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完成吗?
例如,callvbsfunction("mycodevbs")
并运行它?
当我提到windows时,我指的是类似于这些
MessageBox( 0, "Test", "Title here", MB_OK )
GetSystemMetrics( SM_CXSCREEN )
ShellExecute( 0, "open", "c:\myfile.txt", 0, 0, 1 )
Beep( 1000, 250 )
还有更多。
发布于 2022-06-10 02:29:04
注意,您的VBScript有错误。
VBScript不能调用API调用。VB.Net可以
将VBScript转换为VB.Net
Set
关键字。所以只要objPlayer = createobject("Wmplayer.OCX.7")
.Dim
除API调用之外的每个变量as Object
.导入系统导入System.Runtime.InteropServices公共模块AnythingYouWantToCallIt子主
在底部
End Sub
End Module
Sub
。在VBScript中,只有函数需要括号,Subs不需要括号。在VB.Net中,函数和Subs都需要方括号.WScript
顶层对象不可用。对于Command()
.,使用WScript.Arguments
调用API调用
您必须使用declare
关键字。你把Declare
放在Sub Main
和Module
之后。
Public Declare Function Lib "User32" GetSystemMetrics(ByVal SM as Integer) As Integer
Const SM_CXSCREEN = 0
您可以从文档中获得Declare
的函数原型。https://learn.microsoft.com/en-us/windows/win32/api/winuser/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代码时将VBA移植到VB.Net。记住,合法的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
https://stackoverflow.com/questions/72566154
复制相似问题