AdvApi32CreateProcessAsUser Method

Creates a new process and its primary thread. The new process runs in the security context of the user represented by the specified token.

Typically, the process that calls the CreateProcessAsUser(IntPtr, String, StringBuilder, SecurityAttributes, SecurityAttributes, Boolean, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) function must have the IncreaseQuotaPrivilege privilege and may require the AssignPrimaryTokenPrivilege privilege if the token is not assignable.

If this function fails with ERROR_PRIVILEGE_NOT_HELD (1314), use the CreateProcessWithLogon(String, String, String, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) function instead. CreateProcessWithLogon(String, String, String, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) requires no special privileges, but the specified user account must be allowed to log on interactively.

Generally, it is best to use CreateProcessWithLogon(String, String, String, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) to create a process with alternate credentials.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("AdvApi32.dll", CharSet = CharSet.Auto, BestFitMapping = false, 
	ThrowOnUnmappableChar = true, SetLastError = true)]
public static bool CreateProcessAsUser(
	IntPtr hToken,
	string applicationName,
	[OptionalAttribute] StringBuilder commandLine,
	ref SecurityAttributes refProcessAttribs,
	ref SecurityAttributes refThreadAttribs,
	bool inheritHandles,
	CreateProcessFlags createFlags,
	IntPtr environment,
	string currentDir,
	in ProcessStartupInfo refStartupInfo,
	out ProcessInformation refProcessInformation
)

Parameters

hToken  IntPtr
A handle to the primary token that represents a user. The handle must have the Query, Duplicate, and AssignPrimary access rights.

The user represented by the token must have read and execute access to the application specified by the applicationName or the commandLine parameter.

To get a primary token that represents the specified user, call the LogonUser function. Alternatively, you can call the DuplicateTokenEx function to convert an impersonation token into a primary token. This allows a server application that is impersonating a client to create a process that has the security context of the client.

If hToken is a restricted version of the caller's primary token, the AssignPrimary privilege is not required. If the necessary privileges are not already enabled, CreateProcessAsUser(IntPtr, String, StringBuilder, SecurityAttributes, SecurityAttributes, Boolean, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) enables them for the duration of the call.

applicationName  String
The name of the module to be executed. This module can be a Windows-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.

The string can specify the full path and file name of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification. The function will not use the search path. This parameter must include the file name extension; no default extension is assumed.

The applicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the commandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries to interpret the possibilities in the following order:

c:\program.exe, c:\program files\sub.exe, c:\program files\sub dir\program.exe, c:\program files\sub dir\program name.exe

If the executable module is a 16-bit application, applicationName should be NULL, and the string pointed to by commandLine should specify the executable module as well as its arguments.

To run a batch file, you must start the command interpreter; set applicationName to cmd.exe and set commandLine to the following arguments: "/c", plus the name of the batch file.

commandLine  StringBuilder  (Optional)
The command line to be executed.

The maximum length of this string is Int16 (32,768 characters), including the Unicode terminating null character.

If applicationName is NULL, the module name portion of commandLine is limited to MAX_PATH characters.

The commandLine parameter can be NULL. In that case, the function uses the string pointed to by applicationName as the command line.

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, the function may cause an access violation.

refProcessAttribs  SecurityAttributes
A pointer to a SecurityAttributes structure that determines whether the returned handle to the new process object can be inherited by child processes.

If refProcessAttribs is NULL, the handle cannot be inherited.

The SecurityDescriptor member specifies a security descriptor for the new process. If refProcessAttribs is NULL or SecurityDescriptor is Zero, the process gets a default security descriptor.

refThreadAttribs  SecurityAttributes
A pointer to a SecurityAttributes structure that determines whether the returned handle to the new thread object can be inherited by child processes.

If refThreadAttribs is NULL, the handle cannot be inherited.

The SecurityDescriptor member specifies a security descriptor for the main thread. If refThreadAttribs is NULL or SecurityDescriptor is Zero, the thread gets a default security descriptor.

inheritHandles  Boolean
If this parameter is , each inheritable handle in the calling process is inherited by the new process.

If the parameter is , the handles are not inherited.

Note that inherited handles have the same value and access rights as the original handles.

createFlags  CreateProcessFlags
The flags that control the priority class and the creation of the process.

This parameter also controls the new process's priority class, which is used to determine the scheduling priorities of the process's threads.

environment  IntPtr
A pointer to the environment block for the new process.

If this parameter is Zero, the new process uses the environment of the calling process.

An environment block consists of a null-terminated block of null-terminated strings. Each string is in the following form:

name=value\0

Because the equal sign is used as a separator, it must not be used in the name of an environment variable.

The ANSI version of this function, CreateProcessA, fails if the total size of the environment block for the process exceeds 32,767 characters.

currentDir  String
The full path to the current directory for the process. The string can also specify a UNC path.

If this parameter is NULL, the new process will have the same current drive and directory as the calling process. (This feature is provided primarily for shells that need to start an application and specify its initial drive and working directory.)

refStartupInfo  ProcessStartupInfo
A pointer to a ProcessStartupInfo or ProcessStartupInfoEx structure.

To set extended attributes, use a ProcessStartupInfoEx structure and specify ExtendedStartupInfoPresent in the createFlags parameter.

refProcessInformation  ProcessInformation
A pointer to a ProcessInformation structure that receives identification information about the new process.

Return Value

Boolean
If the function succeeds, the return value is .

If the function fails, the return value is .

To get extended error information, call GetLastWin32Error.

Note that the function returns before the process has finished initialization. If a required DLL cannot be located or fails to initialize, the process is terminated.

To get the termination status of a process, call GetExitCodeProcess(IntPtr, UInt32).

Remarks

See Also