这个代码实际上是打开了一个HTML页面(我们看不到),这个页面里面会有背景声音;
而网页没关闭的时候 mshta.exe 就存在,声音停止了网页也不会自动关闭,于是就会残留进程;
因此我们只能手动将其关闭,
或者,由于没有事件,只能在代码里面加上一个固定的等待时间,之后自动关闭。
因为 vbscript 的 wscript 对象在 html 中不可用,所以不能用 wscript.sleep 来延时,改用 window 对象的
setTimeout
以我那首歌为例,长度是 3分06秒 ,即 186 秒,假设我要播放两遍之后结束 mshta.exe 进程,那么就是要在 372000 毫秒之后关闭,可以适当地等几秒,就取 375000 吧
mshta vbscript:execute("document.write ""<bgsound src='E:\audio\曹方我是认真的.mp3' loop='2'>"":window.setTimeout ""window.close()"",375000")
用 javascript 写起来更容易些:
mshta javascript:document.write("<bgsound src='E:\audio\曹方我是认真的.mp3' loop='2'>");window.setTimeout("window.close()",375000)
如果你不知道歌曲有多长,就把时间设的长一些,一般流行歌曲不会超过 300000 ms,即5分钟吧。
----------------------
2010.09.03 修改以适应有空格的路径
代码效果:
后台播放 "C:\Documents and Settings\issuser\桌面\music\incoming\EMINEM ft.NATE DOGG - Shake That.mp3" 两遍,但本程序 mshta.exe 会准时在 360000ms,即 6 分钟后自动结束。
mshta vbscript:execute("document.write ""<bgsound src='file:///"+Replace("C:\Documents and Settings\issuser\桌面\music\incoming\EMINEM ft.NATE DOGG - Shake That.mp3","\","/")+"' loop='2'>"":window.setTimeout ""window.close()"",360000")
Last edited by qinchun36 on 2010-9-3 at 18:39 ]
This code actually opens an HTML page (which we can't see), and there will be background sound in this page; while the mshta.exe exists when the web page is not closed, and the sound stops but the web page doesn't close automatically, so the process will remain; therefore, we can only close it manually, or because there is no event, we can only add a fixed waiting time in the code and then close it automatically. Because the wscript object in vbscript is not available in HTML, we can't use wscript.sleep for delay, so we use the
setTimeout of the window object instead. Taking my song as an example, the length is 3 minutes and 06 seconds, that is 186 seconds. Assuming I want to play it twice and then end the mshta.exe process, then it needs to be closed after 372000 milliseconds, and we can wait a few seconds appropriately, so we take 375000.
mshta vbscript:execute("document.write ""<bgsound src='E:\audio\曹方我是认真的.mp3' loop='2'>"":window.setTimeout ""window.close()"",375000")
It's easier to write with javascript:
mshta javascript:document.write("<bgsound src='E:\audio\曹方我是认真的.mp3' loop='2'>");window.setTimeout("window.close()",375000)
If you don't know the length of the song, set the time to be longer. Generally, popular songs won't exceed 300000 ms, that is 5 minutes.
----------------------
Modified on 2010.09.03 to adapt to paths with spaces
Code effect:
Play "C:\Documents and Settings\issuser\Desktop\music\incoming\EMINEM ft.NATE DOGG - Shake That.mp3" twice in the background, but this program mshta.exe will automatically end exactly after 360000ms, that is 6 minutes.
mshta vbscript:execute("document.write ""<bgsound src='file:///"+Replace("C:\Documents and Settings\issuser\桌面\music\incoming\EMINEM ft.NATE DOGG - Shake That.mp3","\","/")+"' loop='2'>"":window.setTimeout ""window.close()"",360000")
Last edited by qinchun36 on 2010-9-3 at 18:39 ]