AdvApi32CreateProcessWithToken Method
Creates a new process and its primary thread.
The new process runs in the security context of the specified token.
It can optionally load the user profile for the specified user.
The process that calls
CreateProcessWithToken(IntPtr, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation)
must have the
ImpersonatePrivilege privilege.
If this function fails with
ERROR_PRIVILEGE_NOT_HELD (1314),
use the
CreateProcessAsUser(IntPtr, String, StringBuilder, SecurityAttributes, SecurityAttributes, Boolean, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) or
CreateProcessWithLogon(String, String, String, ProcessLogonFlags, String, StringBuilder, CreateProcessFlags, IntPtr, String, ProcessStartupInfo, ProcessInformation) function instead.
Typically, the process that calls.
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.
Namespace: DevCase.Win32.NativeMethodsAssembly: 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 = "CreateProcessWithTokenW",
CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true,
SetLastError = true)]
public static bool CreateProcessWithToken(
IntPtr hToken,
ProcessLogonFlags logonFlags,
string applicationName,
[OptionalAttribute] StringBuilder commandLine,
CreateProcessFlags createFlags,
IntPtr environment,
string currentDir,
in ProcessStartupInfo refStartupInfo,
out ProcessInformation refProcessInformation
)
<DllImportAttribute("AdvApi32.dll", EntryPoint := "CreateProcessWithTokenW",
CharSet := CharSet.Auto, BestFitMapping := false, ThrowOnUnmappableChar := true,
SetLastError := true>]
Public Shared Function CreateProcessWithToken (
hToken As IntPtr,
logonFlags As ProcessLogonFlags,
applicationName As String,
<OptionalAttribute> <OutAttribute> commandLine As StringBuilder,
createFlags As CreateProcessFlags,
environment As IntPtr,
currentDir As String,
ByRef refStartupInfo As ProcessStartupInfo,
<OutAttribute> ByRef refProcessInformation As ProcessInformation
) As Boolean
Dim hToken As IntPtr
Dim logonFlags As ProcessLogonFlags
Dim applicationName As String
Dim commandLine As StringBuilder
Dim createFlags As CreateProcessFlags
Dim environment As IntPtr
Dim currentDir As String
Dim refStartupInfo As ProcessStartupInfo
Dim refProcessInformation As ProcessInformation
Dim returnValue As Boolean
returnValue = AdvApi32.CreateProcessWithToken(hToken,
logonFlags, applicationName, commandLine,
createFlags, environment, currentDir,
refStartupInfo, refProcessInformation)
public:
[DllImportAttribute(L"AdvApi32.dll", EntryPoint = L"CreateProcessWithTokenW",
CharSet = CharSet::Auto, BestFitMapping = false, ThrowOnUnmappableChar = true,
SetLastError = true)]
static bool CreateProcessWithToken(
IntPtr hToken,
ProcessLogonFlags logonFlags,
String^ applicationName,
[OptionalAttribute] [InAttribute] [OutAttribute] StringBuilder^ commandLine,
CreateProcessFlags createFlags,
IntPtr environment,
String^ currentDir,
[InAttribute] ProcessStartupInfo% refStartupInfo,
[OutAttribute] ProcessInformation% refProcessInformation
)
[<DllImportAttribute("AdvApi32.dll", EntryPoint = "CreateProcessWithTokenW",
CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true,
SetLastError = true)>]
static member CreateProcessWithToken :
hToken : IntPtr *
logonFlags : ProcessLogonFlags *
applicationName : string *
[<OptionalAttribute>] commandLine : StringBuilder byref *
createFlags : CreateProcessFlags *
environment : IntPtr *
currentDir : string *
refStartupInfo : ProcessStartupInfo byref *
refProcessInformation : ProcessInformation byref -> bool
No code example is currently available or this language may not be supported.
- 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.
- 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.
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).