07-11-2005, 09:20 PM
RunAs function.
;/
function $prog $user [$domain] [$password] [int&hprocess]
;Runs program as specified user.
;On failure throws error.
;For Windows 2000, XP and later.
;prog - program name or full path, optionally followed by command line parameters. If program path contains spaces, enclose into "". Examples: "prog.exe", "prog.exe /params", "c:\progra~1\prog\prog.exe /params", "''c:\program files\prog\prog.exe'' /params". Must be executable. Documents and shortcuts are not supported.
;user - user name.
;domain - domain or computer name. Can be omitted or "" for local computer.
;password - password. If omitted or "", asks at run time. Using password in macro is unsafe, even if that macro is encrypted (password can be intercepted by replacing RunAs).
;hprocess - if used, receives process handle.
;EXAMPLES
;RunAs "notepad.exe" "Admin" ;;will ask for password
;
;int hproc
;RunAs "c:\windows\notepad.exe" "Power User" "" "ttr09knn" hproc
;wait 0 H hproc
;CloseHandle hproc
#if (WINNT>=5)
def CREATE_DEFAULT_ERROR_MODE 0x04000000
def LOGON_WITH_PROFILE 0x00000001
type STARTUPINFOW cb @*lpReserved @*lpDesktop @*lpTitle dwX dwY dwXSize dwYSize dwXCountChars dwYCountChars dwFillAttribute dwFlags @wShowWindow @cbReserved2 !*lpReserved2 hStdInput hStdOutput hStdError
type PROCESS_INFORMATION hProcess hThread dwProcessId dwThreadId
dll advapi32 #CreateProcessWithLogonW @*lpUsername @*lpDomain @*lpPassword dwLogonFlags @*lpApplicationName @*lpCommandLine dwCreationFlags !*lpEnvironment @*lpCurrentDirectory STARTUPINFOW*lpStartupInfo PROCESS_INFORMATION*lpProcessInformation
str s ss
if(&hprocess) hprocess=0
if(!len(domain)) domain="." ;;local computer
if(!len(password)) if(inp(s "" "Password" "*")) password=s; else ret
BSTR s1(user) s2(domain) s3(password) s4(ss.expandpath(prog))
STARTUPINFOW si; si.cb=sizeof(si)
PROCESS_INFORMATION pi
if(CreateProcessWithLogonW(+s1 +s2 +s3 LOGON_WITH_PROFILE 0 +s4 CREATE_DEFAULT_ERROR_MODE 0 0 &si &pi))
,if(pi.hThread != -1) CloseHandle(pi.hThread)
,if(pi.hProcess != -1) if(&hprocess) hprocess=pi.hProcess; else CloseHandle(pi.hProcess)
else end s.dllerror