Visual Basic Shell Administrator?

丶灬走出姿态 提交于 2021-02-11 12:35:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!