Note that there are some explanatory texts on larger screens.

plurals
  1. POCreateProcessasuser - AccessViolationError
    text
    copied!<p>I am trying to start a Gui tray application from a windows service (LocalSystem) using createProcessasUser - like so:</p> <pre><code> public static System.Diagnostics.Process StartProcessInSession(int sessionID, String commandLine) { IntPtr userToken; if (WTSQueryUserToken(sessionID, out userToken)) { //note that WTSQueryUserToken only works when in context of local system account with SE_TCB_NAME IntPtr lpEnvironment; if (CreateEnvironmentBlock(out lpEnvironment, userToken, false)) { StartupInfo si = new StartupInfo(); si.cb = Marshal.SizeOf(si); si.lpDesktop = "winsta0\\default"; si.dwFlags = STARTF.STARTF_USESHOWWINDOW; si.wShowWindow = ShowWindow.SW_SHOW; ProcessInformation pi; if (CreateProcessAsUser(userToken, null, new StringBuilder(commandLine), IntPtr.Zero, IntPtr.Zero, false, CreationFlags.CREATE_NEW_CONSOLE | CreationFlags.CREATE_UNICODE_ENVIRONMENT, lpEnvironment, null, ref si, out pi)) { CloseHandle(pi.hThread); CloseHandle(pi.hProcess); //context.Undo(); try { return System.Diagnostics.Process.GetProcessById(pi.dwProcessId); } catch (ArgumentException e) { //The process ID couldn't be found - which is what always happens because it has closed return null; } } else { int err = Marshal.GetLastWin32Error(); throw new System.ComponentModel.Win32Exception(err, "Could not create process.\nWin32 error: " + err.ToString()); } } else { int err = Marshal.GetLastWin32Error(); throw new System.ComponentModel.Win32Exception(err, "Could not create environment block.\nWin32 error: " + err.ToString()); } } else { int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); if (err == 1008) return null; //There is no token throw new System.ComponentModel.Win32Exception(err, "Could not get the user token from session " + sessionID.ToString() + " - Error: " + err.ToString()); } } </code></pre> <p>I am using the function as so:</p> <pre><code> protected override void OnStart(string[] args) { _agentProcess = StartProcessInSession(WTSGetActiveConsoleSessionId(), "Some_correct_path"); } </code></pre> <p>This actually worked for a little while, but in one of my runs it suddenly stopped working... giving the following error when executing the CreateProccessAsUser command (cant go any deeper)</p> <pre><code>{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} </code></pre> <p>I have no idea why this is happening or even how to debug this any further, anyhow has any idea?? because this doesnt make any sense to me.</p> <p>CreateProccessasuser definition:</p> <pre><code>[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] static extern bool CreateProcessAsUser(IntPtr hToken, String lpApplicationName, [In] StringBuilder lpCommandLine, IntPtr /*to a SecurityAttributes struct or null*/ lpProcessAttributes, IntPtr /*to a SecurityAttributes struct or null*/ lpThreadAttributes, bool bInheritHandles, CreationFlags creationFlags, IntPtr lpEnvironment, String lpCurrentDirectory, ref StartupInfo lpStartupInfo, out ProcessInformation lpProcessInformation); </code></pre> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload