static void ParseCmdLine(string[] args) { string ExeToRun = "", Arguments = "", WorkingDir = ""; string toRun = "", tokenProcessName = ""; int tokenPID = -1; bool metTI = false, metSilent = false, metShowWait = false; bool metChangeToActiveSessionID = false; // args[] can't process DirPath and ExeToRun containing '\' // and that will influence the other argument too :( // so I need to do it myself :/ string CmdLine = Environment.CommandLine; int iToRun = CmdLine.ToLower().IndexOf("/run:"); if (iToRun != -1) { toRun = CmdLine.Substring(iToRun + 5).Trim(); // Process toRun int iDQuote1, iDQuote2; iDQuote1 = toRun.IndexOf("\""); // If a pair of double quote is exist if (iDQuote1 != -1) { toRun = toRun.Substring(iDQuote1 + 1); iDQuote2 = toRun.IndexOf("\""); if (iDQuote2 != -1) { // before 2nd double quote is ExeToRun, after is Arguments ExeToRun = toRun.Substring(0, iDQuote2); Arguments = toRun.Substring(iDQuote2 + 1); } } else { // before 1st Space is ExeToRun, after is Arguments int firstSpace = toRun.IndexOf(" "); if (firstSpace == -1) { ExeToRun = toRun; } else { ExeToRun = toRun.Substring(0, firstSpace); Arguments = toRun.Substring(firstSpace + 1); } } } // Process all optional arguments before toRun, '/' as separator if (iToRun != -1) CmdLine = CmdLine.Substring(0, iToRun) + "/"; string cmdline = CmdLine.ToLower(); if (cmdline.Contains("/debug")) { DebugMode = true; Title += " Debug"; } // Only /debug affects GUI mode, others are only for command line usage // So if no '/run' then no need to go on if (iToRun == -1) return; if (cmdline.Contains("/ti")) metTI = true; if (cmdline.Contains("/silent")) metSilent = true; if (cmdline.Contains("/showwait")) metShowWait = true; if (cmdline.Contains("/changetoactivesessionid")) metChangeToActiveSessionID = true; string tmp; int iWithTokenOf, iWithTokenOfPID, iSendLogTo, iDir, iNextSlash; iWithTokenOf = cmdline.IndexOf("/withtokenof:"); if (iWithTokenOf != -1) { tmp = CmdLine.Substring(iWithTokenOf + 13); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); tokenProcessName = tmp.Replace("\"", "").Trim(); } } iWithTokenOfPID = cmdline.IndexOf("/withtokenofpid:"); if (iWithTokenOfPID != -1) { tmp = CmdLine.Substring(iWithTokenOfPID + 16); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); tmp = tmp.Replace("\"", "").Trim(); int.TryParse(tmp, out tokenPID); } } iSendLogTo = cmdline.IndexOf("/sendlogto:"); if (iSendLogTo != -1) { tmp = CmdLine.Substring(iSendLogTo + 11); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != 1) { tmp = tmp.Substring(0, iNextSlash); tmp = tmp.Replace("\"", "").Trim(); long h; if (long.TryParse(tmp, out h)) hLogTarget = new IntPtr(h); } } iDir = cmdline.IndexOf("/dir:"); if (iDir != -1) { tmp = CmdLine.Substring(iDir + 5); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); WorkingDir = tmp.Replace("\"", "").Trim(); } } // Run... SuperCore.ForceTokenUseActiveSessionID = metChangeToActiveSessionID; if (metTI) { frmWait WaitUI = new frmWait(); if (metShowWait) WaitUI.ShowIfNeeded(); if (StartTiService()) { SuperCore.RunWithTokenOf("winlogon.exe", true, Application.ExecutablePath, "/WithTokenOf:TrustedInstaller.exe" + " /ForceUseActiveSessionID" + (metSilent ? " /Silent" : "") + (DebugMode ? " /Debug" : "") + " /Dir:\"" + WorkingDir + "\" " + " /Run:\"" + ExeToRun + "\" " + Arguments); } WaitUI.Hide(); } else { if (tokenProcessName != "") { SuperCore.RunWithTokenOf(tokenProcessName, false, ExeToRun, Arguments, WorkingDir); } else if (tokenPID > 0) { SuperCore.RunWithTokenOf(tokenPID, ExeToRun, Arguments, WorkingDir); } else { SuperCore.RunWithTokenOf("winlogon.exe", true, ExeToRun, Arguments, WorkingDir); } } ComplainAndLog(); Environment.Exit((LastComplain != "") ? 1 : 0); }
static void ParseCmdLine(string[] args) { string ExeToRun = "", Arguments = "", WorkingDir = ""; string toRun = "", tokenProcessName = ""; int tokenPID = -1; bool metTI = false, metSilent = false, metShowWait = false; bool metChangeToActiveSessionID = false; // args[] can't process DirPath and ExeToRun containing '\' // and that will influence the other argument too :( // so I need to do it myself :/ string CmdLine = Environment.CommandLine; int iToRun = CmdLine.ToLower().IndexOf("/run:"); if (iToRun != -1) { toRun = CmdLine.Substring(iToRun + 5).Trim(); // Process toRun int iDQuote1, iDQuote2; iDQuote1 = toRun.IndexOf("\""); // If a pair of double quote is exist if (iDQuote1 != -1) { toRun = toRun.Substring(iDQuote1 + 1); iDQuote2 = toRun.IndexOf("\""); if (iDQuote2 != -1) { // before 2nd double quote is ExeToRun, after is Arguments ExeToRun = toRun.Substring(0, iDQuote2); Arguments = toRun.Substring(iDQuote2 + 1); } } else { // before 1st Space is ExeToRun, after is Arguments int firstSpace = toRun.IndexOf(" "); if (firstSpace == -1) { ExeToRun = toRun; } else { ExeToRun = toRun.Substring(0, firstSpace); Arguments = toRun.Substring(firstSpace + 1); } } } // Process all optional arguments before toRun, '/' as separator if (iToRun != -1) { CmdLine = CmdLine.Substring(0, iToRun) + "/"; } string cmdline = CmdLine.ToLower(); if (cmdline.Contains("/debug")) { DebugMode = true; Title += " Debug"; } // Only /debug affects GUI mode, others are only for command line usage // So if no '/run' then no need to go on if (iToRun == -1) { return; } if (cmdline.Contains("/ti")) { metTI = true; } if (cmdline.Contains("/silent")) { metSilent = true; } if (cmdline.Contains("/showwait")) { metShowWait = true; } if (cmdline.Contains("/changetoactivesessionid")) { metChangeToActiveSessionID = true; } string tmp; int iWithTokenOf, iWithTokenOfPID, iSendLogTo, iDir, iNextSlash; iWithTokenOf = cmdline.IndexOf("/withtokenof:"); if (iWithTokenOf != -1) { tmp = CmdLine.Substring(iWithTokenOf + 13); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); tokenProcessName = tmp.Replace("\"", "").Trim(); } } iWithTokenOfPID = cmdline.IndexOf("/withtokenofpid:"); if (iWithTokenOfPID != -1) { tmp = CmdLine.Substring(iWithTokenOfPID + 16); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); tmp = tmp.Replace("\"", "").Trim(); int.TryParse(tmp, out tokenPID); } } iSendLogTo = cmdline.IndexOf("/sendlogto:"); if (iSendLogTo != -1) { tmp = CmdLine.Substring(iSendLogTo + 11); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != 1) { tmp = tmp.Substring(0, iNextSlash); tmp = tmp.Replace("\"", "").Trim(); long h; if (long.TryParse(tmp, out h)) { hLogTarget = new IntPtr(h); } } } iDir = cmdline.IndexOf("/dir:"); if (iDir != -1) { tmp = CmdLine.Substring(iDir + 5); iNextSlash = tmp.IndexOf("/"); if (iNextSlash != -1) { tmp = tmp.Substring(0, iNextSlash); WorkingDir = tmp.Replace("\"", "").Trim(); } } // Run... SuperCore.ForceTokenUseActiveSessionID = metChangeToActiveSessionID; if (metTI) { frmWait WaitUI = new frmWait(); if (metShowWait) { WaitUI.ShowIfNeeded(); } if (StartTiService()) { SuperCore.RunWithTokenOf("winlogon.exe", true, Application.ExecutablePath, "/WithTokenOf:TrustedInstaller.exe" + " /ForceUseActiveSessionID" + (metSilent ? " /Silent" : "") + (DebugMode ? " /Debug" : "") + " /Dir:\"" + WorkingDir + "\" " + " /Run:\"" + ExeToRun + "\" " + Arguments); } WaitUI.Hide(); } else { if (tokenProcessName != "") { SuperCore.RunWithTokenOf(tokenProcessName, false, ExeToRun, Arguments, WorkingDir); } else if (tokenPID > 0) { SuperCore.RunWithTokenOf(tokenPID, ExeToRun, Arguments, WorkingDir); } else { SuperCore.RunWithTokenOf("winlogon.exe", true, ExeToRun, Arguments, WorkingDir); } } ComplainAndLog(); Environment.Exit((LastComplain != "") ? 1 : 0); }