public class CallAPIPage: System.Web.UI.Page
{
#region For API
//dwLogonFlags Specifies the logon option
const int LOGON_WITH_PROFILE = 1;
const int LOGON_NETCREDENTIALS_ONLY = 2;
//dwCreationFlags - Specifies how the process is created
const int CREATE_SUSPENDED = 0x00000004;
const int CREATE_NEW_CONSOLE = 0x00000010;
const int CREATE_NEW_PROCESS_GROUP = 0x00000200;
const int CREATE_SEPARATE_WOW_VDM = 0x00000800;
const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
const int CREATE_DEFAULT_ERROR_MODE = 0x04000000;
//dwCreationFlags parameter controls the new process's priority class
const int NORMAL_PRIORITY_CLASS = 0x00000020;
const int IDLE_PRIORITY_CLASS = 0x00000040;
const int HIGH_PRIORITY_CLASS = 0x00000080;
const int REALTIME_PRIORITY_CLASS = 0x00000100;
const int BELOW_NORMAL_PRIORITY_CLASS = 0x00004000;
const int ABOVE_NORMAL_PRIORITY_CLASS = 0x00008000;
//dwFlags
// This is a bit field that determines whether certain STARTUPINFO
// members are used when the process creates a window.
// Any combination of the following values can be specified:
const int STARTF_USESHOWWINDOW = 0x0000000;
const int STARTF_USESIZE = 0x00000002;
const int STARTF_USEPOSITION = 0x00000004;
const int STARTF_USECOUNTCHARS = 0x00000008;
const int STARTF_USEFILLATTRIBUTE = 0x00000010;
const int STARTF_FORCEONFEEDBACK = 0x00000040;
const int STARTF_FORCEOFFFEEDBACK = 0x00000080;
const int STARTF_USESTDHANDLES = 0x00000100;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox curVersion;
protected System.Web.UI.WebControls.TextBox lastVersion;
protected System.Web.UI.WebControls.Label lbl;
protected System.Web.UI.WebControls.PlaceHolder placeHolder1;
const int STARTF_USEHOTKEY = 0x00000200;
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern bool CreateProcessWithLogonW(String lpszUsername,
String lpszDomain, String lpszPassword,int dwLogonFlags,
string applicationName, StringBuilder commandLine,int creationFlags,
IntPtr environment, string currentDirectory,
ref STARTUPINFO sui,
out PROCESS_INFORMATION processInfo);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);
[StructLayout(LayoutKind.Sequential)]
internal struct STARTUPINFO
{
internal int cb;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpReserved;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpDesktop;
[MarshalAs(UnmanagedType.LPTStr)]
internal string lpTitle;
internal int dwX;
internal int dwY;
internal int dwXSize;
internal int dwYSize;
internal int dwXCountChars;
internal int dwYCountChars;
internal int dwFillAttribute;
internal int dwFlags;
internal short wShowWindow;
internal short cbReserved2;
internal IntPtr lpReserved2;
internal IntPtr hStdInput;
internal IntPtr hStdOutput;
internal IntPtr hStdError;
}
[DllImport("kernel32")]
public static extern int GetLastError();
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
internal IntPtr hProcess;
internal IntPtr hThread;
internal int dwProcessId;
internal int dwThreadId;
}
#endregion For API
protected System.Web.UI.WebControls.ImageButton Button1;
private void Button1_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
string cmd = System.Configuration.ConfigurationSettings.AppSettings["cmdLine"];
string logonName = "Administrator";
string domain = System.Environment.MachineName;
string password = System.Configuration.ConfigurationSettings.AppSettings["adminpwd"];
// command to execute
StringBuilder sb = new StringBuilder();
sb.Append(cmd);
PROCESS_INFORMATION processInfo;
STARTUPINFO startInfo = new STARTUPINFO();
startInfo.cb = Marshal.SizeOf(startInfo);
startInfo.lpTitle = "Command Execute Console";
startInfo.dwFlags = STARTF_USECOUNTCHARS;
startInfo.dwYCountChars = cmd.Length;
try
{
// create process similar as "runas" using the logon users profile
bool retcode = CreateProcessWithLogonW(
logonName,
domain,
password,
LOGON_WITH_PROFILE,
null,
sb,
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
IntPtr.Zero,
null,
ref startInfo,
out processInfo);
if(!retcode)
{
Debug.WriteLine("Marshal.GetLastWin32Error().ToString(): "
+ Marshal.GetLastWin32Error().ToString());
}
else
{
Debug.WriteLine("sucessful" );//+processInfo.dwProcessId.ToString();
}
}
catch(Exception exception)
{
Debug.WriteLine("CreateProcessWithLogonW: " + exception.Message);
}
}