2012-01-09 12 views
1

私はcreateProcessasUserを使用してWindowsサービス(ローカルシステム)からグイトレイアプリケーションを起動しようとしています - そのような:私はように機能を使用していますCreateProcessasuser - AccessViolationError

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()); 
     } 
    } 

protected override void OnStart(string[] args) 
    { 
     _agentProcess = StartProcessInSession(WTSGetActiveConsoleSessionId(), "Some_correct_path"); 
    } 

これは実際に少し動いていましたが、私の実行では突然動作を停止しました... CreateProccessAsUserコマンドを実行するときに次のエラーが発生しました。(深くは行かない)

{"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."} 

なぜこれが起こっているのかわかりませんし、これをさらにデバッグする方法もわかりません。これは私にとって意味をなさないからです。

CreateProccessasuser定義:

[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); 

おかげ

+0

'lpEnvironment'は有効なポインタに設定されていますか?そして 'userToken'は有効な' HANDLE'ですか? –

+0

'CreateProcessAsUser'はどのように定義しましたか? –

+0

定義を追加しました。 – Menyh

答えて

1

あなたProcessInformation型は値型(構造体)または参照型(クラス)ですか?

CreateProcessAsUserの定義とp/invoke宣言を表示します。

ごくごくGetLastWin32Error正しい属性を使用している場合は、p/invokeによってチェックが行われます。

+0

[StructLayout(LayoutKind.Sequential)] 内部構造体ProcessInformation {\t \t #regionデータメンバー(4) 公共INT dwProcessId。 public int dwThreadId; public IntPtr hProcess; public IntPtr hThread; \t \t #endregionデータメンバー } – Menyh

関連する問題