AdvApi32CreateProcessWithLogon Method

Creates a new process and its primary thread. Then the new process runs the specified executable file in the security context of the specified credentials (user, domain, and password). It can optionally load the user profile for a specified user.

This function is similar to the CreateProcessAsUser(IntPtr, String, StringBuilder, SecurityAttributes, SecurityAttributes, Boolean, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) and CreateProcessWithToken(IntPtr, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) functions, except that the caller does not need to call the LogonUser function to authenticate the user and get a token.

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", EntryPoint = "CreateProcessWithLogonW", 
	CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true, 
	SetLastError = true)]
public static bool CreateProcessWithLogon(
	string username,
	string domain,
	string password,
	ProcessLogonFlags logonFlags,
	string applicationName,
	[OptionalAttribute] StringBuilder commandLine,
	CreateProcessFlags createFlags,
	IntPtr environment,
	string currentDir,
	in ProcessStartupInfo refStartupInfo,
	out ProcessInformation refProcessInformation
)

Parameters

username  String
The name of the user. This is the name of the user account to log on to.

If you use the UPN format, user@DNS_domain_name, the domain parameter must be NULL.

The user account must have the Log On Locally permission on the local computer. This permission is granted to all users on workstations and servers, but only to administrators on domain controllers.

domain  String
The name of the domain or server whose account database contains the lpUsername account.

If this parameter is NULL, the user name must be specified in UPN format.

password  String
The password for the username account.
logonFlags  ProcessLogonFlags
The logon option.
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.

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