0

を使用して GUI アプリを呼び出す

[DllImport(
    "advapi32.dll",
    EntryPoint = "CreateProcessAsUser",
    SetLastError = true,
    CharSet = CharSet.Ansi,
    CallingConvention = CallingConvention.StdCall)]
private static extern bool CreateProcessAsUser(
    IntPtr hToken,
    string lpApplicationName,
    string lpCommandLine,
    ref SECURITY_ATTRIBUTES lpProcessAttributes,
    ref SECURITY_ATTRIBUTES lpThreadAttributes,
    bool bInheritHandle,
    int dwCreationFlags,
    IntPtr lpEnvironment,
    string lpCurrentDirectory,
    ref STARTUPINFO lpStartupInfo,
    out PROCESS_INFORMATION lpProcessInformation);


bool result = CreateProcessAsUser(
    hUserTokenDup,
    null,
    applicationName + " " + arguments,
    ref sa,                 // pointer to process SECURITY_ATTRIBUTES
    ref sa,                 // pointer to thread SECURITY_ATTRIBUTES
    false,                  // handles are not inheritable
    NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,        // creation flags
    IntPtr.Zero,            // pointer to new environment block 
    null,                   // name of current directory 
    ref si,                 // pointer to STARTUPINFO structure
    out procInfo);          // receives information about new process

LocalSystem Windows サービスから動作します。ユーザー画面にウィンドウがポップアップしますが、プロセス ユーザーは LocalSystem のままです。それを変更する方法はありますか?

PSリクエストに応じて、私はから取得hUserTokenDupします

[DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
private static extern bool DuplicateTokenEx(
    IntPtr ExistingTokenHandle,
    uint dwDesiredAccess,
    ref SECURITY_ATTRIBUTES lpThreadAttributes,
    int TokenType,
    int ImpersonationLevel,
    ref IntPtr DuplicateTokenHandle);

 DuplicateTokenEx(
     hPToken,
     MAXIMUM_ALLOWED,
     ref sa,
     (int)SECURITY_IMPERSONATION_LEVEL.SecurityIdentification,
     (int)TOKEN_TYPE.TokenPrimary,
     ref hUserTokenDup);
4

3 に答える 3

3

私のサービスでは、 を呼び出す前にWTSGetActiveConsoleSessionId()WTSQueryUserToken()、およびを使用していますが、問題なく動作します。生成されたプロセスは、サービス アカウントではなく、ユーザー アカウントで実行されます。DuplicationTokenEx()CreateProcessAsUser()

于 2011-02-08T23:04:38.103 に答える
2

DuplicateTokenEx を使用して現在のトークンをコピーする代わりに、LogonUser を呼び出してターゲット ユーザーを表すトークンを取得する必要があるようです。DuplicateTokenEx を呼び出すだけで、ローカル システム ユーザーのトークンが作成されます。コード スニペットを正しく理解していれば。

また、インタラクティブなユーザーをターゲットにしているため、代わりにCreateProcessWithLogonW関数を検討してください。

于 2011-02-08T22:43:45.710 に答える