Can I get the return value of Shell.Application

2019-08-13 18:51发布

问题:

I'm using VBScript to create a UAC prompt for a batch file. I don't know how to get the return value of the UAC prompt. For example if I try to UAC a file that doesn't exist I should get an error, right?

For example:

Dim rc
Set UAC = CreateObject("Shell.Application") 
rc = UAC.ShellExecute("thisdoesntexist.exe", "", "", "runas", 1)
WScript.Echo rc

rc doesn't contain a code. Also, is there any way I can get the error code of whatever I'm executing? Is ShellExecute asynchronous in VBScript?

回答1:

IShellDispatch2.ShellExecute method

Performs a specified operation on a specified file.

Syntax

IShellDispatch2.ShellExecute(sFile [, vArguments] [, vDirectory] [, vOperation] [, vShow]) Parameters

sFile Required. String that contains the name of the file on which ShellExecute will perform the action specified by vOperation.

vArguments Optional. Variant that contains the parameter values for the operation.

vDirectory Optional. Variant that contains the fully qualified path of the directory that contains the file specified by sFile. If this parameter is not specified, the current working directory is used.

vOperation Optional. Variant that specifies the operation to be performed. It should be set to one of the verb strings that is supported by the file. For a discussion of verbs, see the Remarks section. If this parameter is not specified, the default operation is performed.

vShow Optional. Variant that recommends how the window that belongs to the application that performs the operation should be displayed initially. The application can ignore this recommendation. vShow can take one of the following values. If this parameter is not specified, the application uses its default value.0

Open the application with a hidden window.

1 Open the application with a normal window. If the window is minimized or maximized, the system restores it to its original size and position.

2 Open the application with a minimized window.

3 Open the application with a maximized window.

4 Open the application with its window at its most recent size and position. The active window remains active.

5 Open the application with its window at its current size and position.

7 Open the application with a minimized window. The active window remains active.

10 Open the application with its window in the default state specified by the application.


Return Value

No return value.


Remarks

This method is equivalent to launching one of the commands associated with a file's shortcut menu. Each command is identified by a verb string. The supported verbs vary from file to file. The most commonly supported verb is "open", which is also usually the default verb. Others might be supported only by certain types of files. For further discussion of Shell verbs, see Launching Applications or Extending Shortcut Menus.

This method is not currently available in Microsoft Visual Basic.

Examples

The following example uses ShellExecute to open Microsoft Notepad. Proper usage is shown for Microsoft JScript and Visual Basic Scripting Edition (VBScript).

<script language="VBScript">
    function fnShellExecuteVB()
        dim objShell

        set objShell = CreateObject("Shell.Application")

        objShell.ShellExecute "notepad.exe", "", "", "open", 1

        set objShell = nothing
    end function
</script>

Now all COM calls look like this HResult = methodcall(param1, param2, ..., paramn, OUTPARAM).

VB pretends it OUTPARAM = methodcall(param1, Param2, ..., paramn) with HResult appearing in the err.object.

So errors will still fire, it's just that it doesn't wait to find out.



标签: vbscript wsh