问题
If I'm using Visual Basic to run an executable using the Shell() command, how would I run that executable as an administrator? My method works, at least in practice, but one executable won't run properly and I think that's the issue.
Here's an example of the command I'm trying to run.
Shell("%temp%\ninite.exe")
Edit: Sorry, this is VB.net, not VBA. I put a bad tag on there.
回答1:
To stay strictly in VBA with no .NET dependencies, you should be able to use the ShellExecute Win32 function.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
ByVal hWnd As Long, _
ByVal lpOperation As String,
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long
You can pass "runas" to lpOperation instead of the more usual "open" (commonly called the verb). Note that this may cause a dialog box to come up prompting the user for credentials.
A better way might be to use the CreateProcess or CreateProcessAsUser function which is probably the most powerful way to launch anything in Windows, but that is certainly more complex and I cannot tell you the details of how to get it to work right.
来源:https://stackoverflow.com/questions/26892803/visual-basic-shell-administrator