在VBScript中预加载程序以实现更高效的执行

时间:2017-02-10 01:20:27

标签: vbscript

我有一个VBS脚本(实际编译为.exe),它使用VBS 运行指令调用另一个程序(.exe)。我的大型机心态告诉我,在我的脚本开始时预加载第二个程序是有益的,这样在需要的时候可以立即使用它。通常在大型机上,在适当的时候,可以在某个时刻将程序加载到内存中,然后再转移到内存中。

这个概念在VBS中是否存在?

感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

在暂停状态下启动程序很容易,但是我还没有办法在没有外部工具的情况下稍后恢复执行(我们需要调用ResumeThread API)。对于此示例,我使用了Windows SysInternal的PsSuspend工具来恢复该过程。

Option Explicit

Const SW_NORMAL = 1
Const CF_CREATE_SUSPENDED = 4

Const PROCESS_NAME = "Notepad.exe"

' Instantiate required objects
Dim wmi, shell
    Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    Set shell = WScript.CreateObject("WScript.Shell")

' Prepare the startup configuration for the process
' https://msdn.microsoft.com/en-us/library/aa394375%28v=vs.85%29.aspx
Dim startUp
    Set startUp = wmi.Get("Win32_ProcessStartup").SpawnInstance_
    With startUp
        .ShowWindow = SW_NORMAL
        .CreateFlags = CF_CREATE_SUSPENDED
    End With 

' Start the process
' https://msdn.microsoft.com/en-us/library/aa394372%28v=vs.85%29.aspx    
Dim retCode, processID
    retCode = wmi.Get("Win32_Process").Create( PROCESS_NAME, Null, startUp, processID )

    If retCode <> 0 Then
        Wscript.Echo "Process creation failed: " & retCode 
        WScript.Quit 1
    End If 
    WScript.Echo "Process created with PID: " & processID

' Ask the OS to check for presence of our process    
    WScript.Echo shell.Exec("tasklist /fo:list /v /fi ""imagename eq " & PROCESS_NAME & """").StdOut.ReadAll()

' Wait (not required, just for testing) 
    WScript.Sleep 5000

' Resume the process - SysInternals pssuspend required
' https://technet.microsoft.com/en-us/sysinternals/pssuspend.aspx
    Call shell.Run("pssuspend64.exe /accepteula -r " & processID, 0, False)

' Wait for the process to resume and show again the task list 
    WScript.Sleep 2000
    WScript.Echo shell.Exec("tasklist /fo:list /v /fi ""imagename eq " & PROCESS_NAME & """").StdOut.ReadAll()