Vbscript VBS未等待。请运行命令以完成
我有一个代码,将自动打开个性化窗口,将主题更改为Windows7Basic,然后关闭窗口。问题是,run命令完成后窗口不会关闭。我使用的代码是:Vbscript VBS未等待。请运行命令以完成,vbscript,cmd,Vbscript,Cmd,我有一个代码,将自动打开个性化窗口,将主题更改为Windows7Basic,然后关闭窗口。问题是,run命令完成后窗口不会关闭。我使用的代码是: Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Ac
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\basic.theme""",1,true
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
现在,有了结尾的“true”语句,它是否应该基本上等待这个命令完成,然后再继续?因为如果我删除“true”语句以及结尾的“1”,而是添加一个计时器,例如:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\basic.theme"""
Wscript.Sleep 5000
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
只有在这里,它才会等待任务完成,然后关闭窗口。我做错了什么!还有,有人能确切地解释一下“%FC”和“{F4}”是做什么的吗?我知道其中一个关上了窗户,但我很难找到它们的确切含义。提前谢谢你 我希望我知道它的真正原因,但似乎脚本是设计好的。RunDll32.exe必须将处理传递给另一个进程,这就是为什么脚本似乎可以继续而不等待的原因。我更新了脚本以证明发生了什么
Set WshShell = WScript.CreateObject("WScript.Shell")
msgbox (IsProcessRunning("rundll32.exe"))
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\basic.theme""",1,true
msgbox (IsProcessRunning("rundll32.exe"))
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Function IsProcessRunning(pProcessName)
' Function will do a WMI query to determine if a the process pProcessName is currently
' running on the local computer. Returns True if detected.
Dim objWMIService
Dim strWMIQuery
strWMIQuery = "Select * From Win32_Process Where name Like '" & pProcessName & "'"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
' Run The query against the WMI for the local machine
If (objWMIService.ExecQuery(strWMIQuery).Count > 0) Then
IsProcessRunning = True
Else
IsProcessRunning = False
End If
End Function
我添加了一个检查进程是否正在运行的函数。在您的命令之前一次,以证明它尚未运行。在rundll32.exe调用之后,也要证明它仍然没有运行。虽然这似乎是多余的,显示第二个错误消息框将在主题更改处理完成之前出现
来自旧MS KB文章的片段:
Rundll的工作原理
Rundll执行以下步骤:
1.它解析命令行。
2.它通过LoadLibrary()加载指定的DLL。
3.它通过GetProcAddress()获取函数的地址。
4.它调用函数,传递命令行尾部,该尾部是。
5.当函数返回时,Rundll.exe卸载DLL并退出
希望有人能对此加以修饰 如果将
Run
方法的第三个参数设置为True
,则该方法将仅在rundll32.exe
终止后返回,这将使脚本的其余部分变得毫无意义。您需要确定一个指示任务已完成的条件,并在继续之前检查该条件。我明白您的意思,但如何终止它?因为它所做的只是将主题更改为某个文件,所以它基本上就是单击一个文件并等待加载。但我确实发现这是可行的:Set WshShell=WScript.CreateObject(“WScript.Shell”)WshShell.Run“rundll32.exe%SystemRoot%\system32\shell32.dll,Control\u RunDLL%SystemRoot%\system32\desk.cpl desk,@Themes/Action:OpenTheme/file:“C:\Windows\Resources\Ease of Access Themes\basic.theme”“,1,真正的WScript.CreateObject(“WScript.Shell”)。运行“taskkill/fi”“WINDOWTITLE eq Personalization”“,0,true
为什么会这样?因为它等待rundll32
完成,然后窗口在完成后立即关闭。我不确定。%FC转换为按Alt F C键。%是Alt的字符。F和C代表文件的快捷方式,并在特定窗口中关闭。花括号用于SendKeys代码,在您的示例中,花括号用于F4键WshShell.Sendkeys“%FC”
WshShell.Sendkeys“{F4}”
更多阅读:嗨,马特,谢谢你的回复。我应该在发现后说,但我完全忘记了,但事实上我确实发现了。谢谢你!顺便问一下,你知道我最初的问题吗?只是想知道。如果没有,没问题,谢谢你的帮助!这有助于理解它为什么会这样工作。很遗憾,我不能更新你的帖子,但谢谢你发布这篇文章,它确实澄清了很多。我会继续调查的。