Win32 API 日本語リファレンス
ホームSystem.Threading › CreateProcessWithTokenW

CreateProcessWithTokenW

関数
指定のアクセストークンで新しいプロセスを作成する。
DLLADVAPI32.dll呼出規約winapiSetLastErrorあり対応OSWindows Vista 以降

シグネチャ

// ADVAPI32.dll
#include <windows.h>

BOOL CreateProcessWithTokenW(
    HANDLE hToken,
    CREATE_PROCESS_LOGON_FLAGS dwLogonFlags,
    LPCWSTR lpApplicationName,   // optional
    LPWSTR lpCommandLine,   // optional
    PROCESS_CREATION_FLAGS dwCreationFlags,
    void* lpEnvironment,   // optional
    LPCWSTR lpCurrentDirectory,   // optional
    STARTUPINFOW* lpStartupInfo,
    PROCESS_INFORMATION* lpProcessInformation
);

パラメーター

名前方向
hTokenHANDLEin
dwLogonFlagsCREATE_PROCESS_LOGON_FLAGSin
lpApplicationNameLPCWSTRinoptional
lpCommandLineLPWSTRinoutoptional
dwCreationFlagsPROCESS_CREATION_FLAGSin
lpEnvironmentvoid*inoptional
lpCurrentDirectoryLPCWSTRinoptional
lpStartupInfoSTARTUPINFOW*in
lpProcessInformationPROCESS_INFORMATION*out

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll
#include <windows.h>

BOOL CreateProcessWithTokenW(
    HANDLE hToken,
    CREATE_PROCESS_LOGON_FLAGS dwLogonFlags,
    LPCWSTR lpApplicationName,   // optional
    LPWSTR lpCommandLine,   // optional
    PROCESS_CREATION_FLAGS dwCreationFlags,
    void* lpEnvironment,   // optional
    LPCWSTR lpCurrentDirectory,   // optional
    STARTUPINFOW* lpStartupInfo,
    PROCESS_INFORMATION* lpProcessInformation
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CreateProcessWithTokenW(
    IntPtr hToken,   // HANDLE
    uint dwLogonFlags,   // CREATE_PROCESS_LOGON_FLAGS
    [MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpCommandLine,   // LPWSTR optional, in/out
    uint dwCreationFlags,   // PROCESS_CREATION_FLAGS
    IntPtr lpEnvironment,   // void* optional
    [MarshalAs(UnmanagedType.LPWStr)] string lpCurrentDirectory,   // LPCWSTR optional
    IntPtr lpStartupInfo,   // STARTUPINFOW*
    IntPtr lpProcessInformation   // PROCESS_INFORMATION* out
);
<DllImport("ADVAPI32.dll", SetLastError:=True, ExactSpelling:=True)>
Public Shared Function CreateProcessWithTokenW(
    hToken As IntPtr,   ' HANDLE
    dwLogonFlags As UInteger,   ' CREATE_PROCESS_LOGON_FLAGS
    <MarshalAs(UnmanagedType.LPWStr)> lpApplicationName As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> lpCommandLine As System.Text.StringBuilder,   ' LPWSTR optional, in/out
    dwCreationFlags As UInteger,   ' PROCESS_CREATION_FLAGS
    lpEnvironment As IntPtr,   ' void* optional
    <MarshalAs(UnmanagedType.LPWStr)> lpCurrentDirectory As String,   ' LPCWSTR optional
    lpStartupInfo As IntPtr,   ' STARTUPINFOW*
    lpProcessInformation As IntPtr   ' PROCESS_INFORMATION* out
) As Boolean
End Function
' hToken : HANDLE
' dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS
' lpApplicationName : LPCWSTR optional
' lpCommandLine : LPWSTR optional, in/out
' dwCreationFlags : PROCESS_CREATION_FLAGS
' lpEnvironment : void* optional
' lpCurrentDirectory : LPCWSTR optional
' lpStartupInfo : STARTUPINFOW*
' lpProcessInformation : PROCESS_INFORMATION* out
Declare PtrSafe Function CreateProcessWithTokenW Lib "advapi32" ( _
    ByVal hToken As LongPtr, _
    ByVal dwLogonFlags As Long, _
    ByVal lpApplicationName As LongPtr, _
    ByVal lpCommandLine As LongPtr, _
    ByVal dwCreationFlags As Long, _
    ByVal lpEnvironment As LongPtr, _
    ByVal lpCurrentDirectory As LongPtr, _
    ByVal lpStartupInfo As LongPtr, _
    ByVal lpProcessInformation As LongPtr) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

CreateProcessWithTokenW = ctypes.windll.advapi32.CreateProcessWithTokenW
CreateProcessWithTokenW.restype = wintypes.BOOL
CreateProcessWithTokenW.argtypes = [
    wintypes.HANDLE,  # hToken : HANDLE
    wintypes.DWORD,  # dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS
    wintypes.LPCWSTR,  # lpApplicationName : LPCWSTR optional
    wintypes.LPWSTR,  # lpCommandLine : LPWSTR optional, in/out
    wintypes.DWORD,  # dwCreationFlags : PROCESS_CREATION_FLAGS
    ctypes.POINTER(None),  # lpEnvironment : void* optional
    wintypes.LPCWSTR,  # lpCurrentDirectory : LPCWSTR optional
    ctypes.c_void_p,  # lpStartupInfo : STARTUPINFOW*
    ctypes.c_void_p,  # lpProcessInformation : PROCESS_INFORMATION* out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
CreateProcessWithTokenW = Fiddle::Function.new(
  lib['CreateProcessWithTokenW'],
  [
    Fiddle::TYPE_VOIDP,  # hToken : HANDLE
    -Fiddle::TYPE_INT,  # dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS
    Fiddle::TYPE_VOIDP,  # lpApplicationName : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpCommandLine : LPWSTR optional, in/out
    -Fiddle::TYPE_INT,  # dwCreationFlags : PROCESS_CREATION_FLAGS
    Fiddle::TYPE_VOIDP,  # lpEnvironment : void* optional
    Fiddle::TYPE_VOIDP,  # lpCurrentDirectory : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # lpStartupInfo : STARTUPINFOW*
    Fiddle::TYPE_VOIDP,  # lpProcessInformation : PROCESS_INFORMATION* out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn CreateProcessWithTokenW(
        hToken: *mut core::ffi::c_void,  // HANDLE
        dwLogonFlags: u32,  // CREATE_PROCESS_LOGON_FLAGS
        lpApplicationName: *const u16,  // LPCWSTR optional
        lpCommandLine: *mut u16,  // LPWSTR optional, in/out
        dwCreationFlags: u32,  // PROCESS_CREATION_FLAGS
        lpEnvironment: *mut (),  // void* optional
        lpCurrentDirectory: *const u16,  // LPCWSTR optional
        lpStartupInfo: *mut STARTUPINFOW,  // STARTUPINFOW*
        lpProcessInformation: *mut PROCESS_INFORMATION  // PROCESS_INFORMATION* out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", SetLastError = true)]
public static extern bool CreateProcessWithTokenW(IntPtr hToken, uint dwLogonFlags, [MarshalAs(UnmanagedType.LPWStr)] string lpApplicationName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpCommandLine, uint dwCreationFlags, IntPtr lpEnvironment, [MarshalAs(UnmanagedType.LPWStr)] string lpCurrentDirectory, IntPtr lpStartupInfo, IntPtr lpProcessInformation);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_CreateProcessWithTokenW' -Namespace Win32 -PassThru
# $api::CreateProcessWithTokenW(hToken, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)
#uselib "ADVAPI32.dll"
#func global CreateProcessWithTokenW "CreateProcessWithTokenW" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CreateProcessWithTokenW hToken, dwLogonFlags, lpApplicationName, varptr(lpCommandLine), dwCreationFlags, lpEnvironment, lpCurrentDirectory, varptr(lpStartupInfo), varptr(lpProcessInformation)   ; 戻り値は stat
; hToken : HANDLE -> "sptr"
; dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "sptr"
; lpApplicationName : LPCWSTR optional -> "sptr"
; lpCommandLine : LPWSTR optional, in/out -> "sptr"
; dwCreationFlags : PROCESS_CREATION_FLAGS -> "sptr"
; lpEnvironment : void* optional -> "sptr"
; lpCurrentDirectory : LPCWSTR optional -> "sptr"
; lpStartupInfo : STARTUPINFOW* -> "sptr"
; lpProcessInformation : PROCESS_INFORMATION* out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global CreateProcessWithTokenW "CreateProcessWithTokenW" sptr, int, wstr, var, int, sptr, wstr, var, var
; res = CreateProcessWithTokenW(hToken, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)
; hToken : HANDLE -> "sptr"
; dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "int"
; lpApplicationName : LPCWSTR optional -> "wstr"
; lpCommandLine : LPWSTR optional, in/out -> "var"
; dwCreationFlags : PROCESS_CREATION_FLAGS -> "int"
; lpEnvironment : void* optional -> "sptr"
; lpCurrentDirectory : LPCWSTR optional -> "wstr"
; lpStartupInfo : STARTUPINFOW* -> "var"
; lpProcessInformation : PROCESS_INFORMATION* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL CreateProcessWithTokenW(HANDLE hToken, CREATE_PROCESS_LOGON_FLAGS dwLogonFlags, LPCWSTR lpApplicationName, LPWSTR lpCommandLine, PROCESS_CREATION_FLAGS dwCreationFlags, void* lpEnvironment, LPCWSTR lpCurrentDirectory, STARTUPINFOW* lpStartupInfo, PROCESS_INFORMATION* lpProcessInformation)
#uselib "ADVAPI32.dll"
#cfunc global CreateProcessWithTokenW "CreateProcessWithTokenW" intptr, int, wstr, var, int, intptr, wstr, var, var
; res = CreateProcessWithTokenW(hToken, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)
; hToken : HANDLE -> "intptr"
; dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "int"
; lpApplicationName : LPCWSTR optional -> "wstr"
; lpCommandLine : LPWSTR optional, in/out -> "var"
; dwCreationFlags : PROCESS_CREATION_FLAGS -> "int"
; lpEnvironment : void* optional -> "intptr"
; lpCurrentDirectory : LPCWSTR optional -> "wstr"
; lpStartupInfo : STARTUPINFOW* -> "var"
; lpProcessInformation : PROCESS_INFORMATION* out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procCreateProcessWithTokenW = advapi32.NewProc("CreateProcessWithTokenW")
)

// hToken (HANDLE), dwLogonFlags (CREATE_PROCESS_LOGON_FLAGS), lpApplicationName (LPCWSTR optional), lpCommandLine (LPWSTR optional, in/out), dwCreationFlags (PROCESS_CREATION_FLAGS), lpEnvironment (void* optional), lpCurrentDirectory (LPCWSTR optional), lpStartupInfo (STARTUPINFOW*), lpProcessInformation (PROCESS_INFORMATION* out)
r1, _, err := procCreateProcessWithTokenW.Call(
	uintptr(hToken),
	uintptr(dwLogonFlags),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpApplicationName))),
	uintptr(lpCommandLine),
	uintptr(dwCreationFlags),
	uintptr(lpEnvironment),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpCurrentDirectory))),
	uintptr(lpStartupInfo),
	uintptr(lpProcessInformation),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function CreateProcessWithTokenW(
  hToken: THandle;   // HANDLE
  dwLogonFlags: DWORD;   // CREATE_PROCESS_LOGON_FLAGS
  lpApplicationName: PWideChar;   // LPCWSTR optional
  lpCommandLine: PWideChar;   // LPWSTR optional, in/out
  dwCreationFlags: DWORD;   // PROCESS_CREATION_FLAGS
  lpEnvironment: Pointer;   // void* optional
  lpCurrentDirectory: PWideChar;   // LPCWSTR optional
  lpStartupInfo: Pointer;   // STARTUPINFOW*
  lpProcessInformation: Pointer   // PROCESS_INFORMATION* out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'CreateProcessWithTokenW';
result := DllCall("ADVAPI32\CreateProcessWithTokenW"
    , "Ptr", hToken   ; HANDLE
    , "UInt", dwLogonFlags   ; CREATE_PROCESS_LOGON_FLAGS
    , "WStr", lpApplicationName   ; LPCWSTR optional
    , "Ptr", lpCommandLine   ; LPWSTR optional, in/out
    , "UInt", dwCreationFlags   ; PROCESS_CREATION_FLAGS
    , "Ptr", lpEnvironment   ; void* optional
    , "WStr", lpCurrentDirectory   ; LPCWSTR optional
    , "Ptr", lpStartupInfo   ; STARTUPINFOW*
    , "Ptr", lpProcessInformation   ; PROCESS_INFORMATION* out
    , "Int")   ; return: BOOL
●CreateProcessWithTokenW(hToken, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) = DLL("ADVAPI32.dll", "bool CreateProcessWithTokenW(void*, dword, char*, char*, dword, void*, char*, void*, void*)")
# 呼び出し: CreateProcessWithTokenW(hToken, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)
# hToken : HANDLE -> "void*"
# dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "dword"
# lpApplicationName : LPCWSTR optional -> "char*"
# lpCommandLine : LPWSTR optional, in/out -> "char*"
# dwCreationFlags : PROCESS_CREATION_FLAGS -> "dword"
# lpEnvironment : void* optional -> "void*"
# lpCurrentDirectory : LPCWSTR optional -> "char*"
# lpStartupInfo : STARTUPINFOW* -> "void*"
# lpProcessInformation : PROCESS_INFORMATION* out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。