using System;using System.Runtime.InteropServices; public class RunAs2 { [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern bool LogonUser(string u, string d, string p, int t, int pr, out IntPtr tok); [DllImport("advapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)] public static extern bool CreateProcessAsUser(IntPtr tok, string app, string cmd, IntPtr pa, IntPtr ta, bool inherit, uint flags, IntPtr env, string cwd, ref STARTUPINFO si, out PROCESS_INFORMATION pi); [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct STARTUPINFO { public int cb; public string lpReserved; public string lpDesktop; public string lpTitle; public int dwX; public int dwY; public int dwXSize; public int dwYSize; public int dwXCountChars; public int dwYCountChars; public int dwFillAttribute; public int dwFlags; public short wShowWindow; public short cbReserved2; public IntPtr lpReserved2; public IntPtr hStdInput; public IntPtr hStdOutput; public IntPtr hStdError; } [StructLayout(LayoutKind.Sequential)] public struct PROCESS_INFORMATION { public IntPtr hProcess; public IntPtr hThread; public int dwProcessId; public int dwThreadId; } public static string Run(string user, string pass, string exe, string args){ IntPtr tok; if(!LogonUser(user, ".", pass, 2, 0, out tok)) return "LOGONERR|" + Marshal.GetLastWin32Error(); STARTUPINFO si = new STARTUPINFO(); si.cb = Marshal.SizeOf(si); si.lpDesktop = "winsta0\\default"; PROCESS_INFORMATION pi; bool ok = CreateProcessAsUser(tok, exe, exe + " " + args, IntPtr.Zero, IntPtr.Zero, false, 0x08000000, IntPtr.Zero, "C:\\Users\\Public", ref si, out pi); if(!ok) return "CREATERR|" + Marshal.GetLastWin32Error(); return "OK|pid=" + pi.dwProcessId; } }