-
There is a function like this: public static unsafe bool CreateProcess(string applicationName, int targetSessionId, bool forceConsoleSession, string desktopName, out PROCESS_INFORMATION procInfo)
{
uint winlogonPid = 0;
SafeFileHandle hProcess;
SafeFileHandle hPToken;
SafeFileHandle hUserTokenDup;
procInfo = new PROCESS_INFORMATION();
var dwSessionId = WTSGetActiveConsoleSessionId();
if (!forceConsoleSession)
{
var activeSessions = GetActiveSessions();
if (activeSessions.Any(x => x.ID == targetSessionId))
{
dwSessionId = (uint)targetSessionId;
}
else
{
dwSessionId = activeSessions.Last().ID;
}
}
foreach (var process in Process.GetProcessesByName("winlogon"))
{
if ((uint)process.SessionId == dwSessionId)
{
winlogonPid = (uint)process.Id;
}
}
hProcess = OpenProcess_SafeHandle(PROCESS_ACCESS_RIGHTS.PROCESS_ALL_ACCESS, false, winlogonPid);
if (!OpenProcessToken(hProcess, TOKEN_ACCESS_MASK.TOKEN_DUPLICATE, out hPToken))
{
return false;
}
var sa = new SECURITY_ATTRIBUTES();
sa.nLength = (uint)Marshal.SizeOf(sa);
if (!DuplicateTokenEx(hPToken, TOKEN_ACCESS_MASK.TOKEN_ALL_ACCESS, sa, SECURITY_IMPERSONATION_LEVEL.SecurityIdentification, TOKEN_TYPE.TokenPrimary, out hUserTokenDup))
{
return false;
}
fixed (char* pdesktopName = @"winsta0\" + desktopName)
{
STARTUPINFOW si = default;
si.cb = (uint)Marshal.SizeOf(si);
si.lpDesktop = pdesktopName;
var dwCreationFlags = PROCESS_CREATION_FLAGS.NORMAL_PRIORITY_CLASS | PROCESS_CREATION_FLAGS.CREATE_UNICODE_ENVIRONMENT | PROCESS_CREATION_FLAGS.CREATE_NO_WINDOW;
si.dwFlags = STARTUPINFOW_FLAGS.STARTF_USESHOWWINDOW;
si.wShowWindow = 0;
var appName = new Span<char>(applicationName.ToCharArray());
return CreateProcessAsUser(hUserTokenDup, null, ref appName, sa, sa, false, (uint)dwCreationFlags, null, null, si, out procInfo);
}
} This code throws an exception: I call it like this: CreateProcess(@"C:\test.exe", -1, true, "default", out _) |
Beta Was this translation helpful? Give feedback.
Answered by
vitkuz573
Dec 6, 2022
Replies: 1 comment 2 replies
-
Sorry. Problem solved by adding |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
vitkuz573
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry. Problem solved by adding
char.MinValue
toapplicationName