我有一个需要以提升的权限运行的应用程序。我正在创建一个WScript.Shell对象,使用管理员用户名调用“runas”,然后使用SendKeys发送密码。
由于一些奇怪的原因,这不起作用,我收到“未知的用户名或密码”消息。密码绝对是正确的。当我尝试从应用程序启动窗口但手动输入密码时 - 一切正常。
以下是代码:
Public Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
Private Function RunExecutableAs(executableName As String, userName As String, password As String) As Boolean
Dim shell As Object
Dim result As Long
Set shell = CreateObject("Wscript.shell")
result = shell.run("C:\Windows\System32\cmd.exe /k runas /user:""" + userName + """ """ + executableName + """", 2, False)
If result = 0
Sleep 1000
shell.SendKeys password + "{ENTER}"
Set shell = Nothing
RunExecutableAs = True
End If
End Function