ホーム › System.Threading › CreateProcessWithLogonW
CreateProcessWithLogonW
関数指定のユーザー資格情報でログオンして新しいプロセスを作成する。
シグネチャ
// ADVAPI32.dll
#include <windows.h>
BOOL CreateProcessWithLogonW(
LPCWSTR lpUsername,
LPCWSTR lpDomain, // optional
LPCWSTR lpPassword,
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
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpUsername | LPCWSTR | in |
| lpDomain | LPCWSTR | inoptional |
| lpPassword | LPCWSTR | in |
| dwLogonFlags | CREATE_PROCESS_LOGON_FLAGS | in |
| lpApplicationName | LPCWSTR | inoptional |
| lpCommandLine | LPWSTR | inoutoptional |
| dwCreationFlags | PROCESS_CREATION_FLAGS | in |
| lpEnvironment | void* | inoptional |
| lpCurrentDirectory | LPCWSTR | inoptional |
| lpStartupInfo | STARTUPINFOW* | in |
| lpProcessInformation | PROCESS_INFORMATION* | out |
戻り値の型: BOOL
各言語での呼び出し定義
// ADVAPI32.dll
#include <windows.h>
BOOL CreateProcessWithLogonW(
LPCWSTR lpUsername,
LPCWSTR lpDomain, // optional
LPCWSTR lpPassword,
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 CreateProcessWithLogonW(
[MarshalAs(UnmanagedType.LPWStr)] string lpUsername, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpDomain, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpPassword, // LPCWSTR
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 CreateProcessWithLogonW(
<MarshalAs(UnmanagedType.LPWStr)> lpUsername As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpDomain As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpPassword As String, ' LPCWSTR
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' lpUsername : LPCWSTR
' lpDomain : LPCWSTR optional
' lpPassword : LPCWSTR
' 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 CreateProcessWithLogonW Lib "advapi32" ( _
ByVal lpUsername As LongPtr, _
ByVal lpDomain As LongPtr, _
ByVal lpPassword 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
CreateProcessWithLogonW = ctypes.windll.advapi32.CreateProcessWithLogonW
CreateProcessWithLogonW.restype = wintypes.BOOL
CreateProcessWithLogonW.argtypes = [
wintypes.LPCWSTR, # lpUsername : LPCWSTR
wintypes.LPCWSTR, # lpDomain : LPCWSTR optional
wintypes.LPCWSTR, # lpPassword : LPCWSTR
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')
CreateProcessWithLogonW = Fiddle::Function.new(
lib['CreateProcessWithLogonW'],
[
Fiddle::TYPE_VOIDP, # lpUsername : LPCWSTR
Fiddle::TYPE_VOIDP, # lpDomain : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpPassword : LPCWSTR
-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 CreateProcessWithLogonW(
lpUsername: *const u16, // LPCWSTR
lpDomain: *const u16, // LPCWSTR optional
lpPassword: *const u16, // LPCWSTR
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 CreateProcessWithLogonW([MarshalAs(UnmanagedType.LPWStr)] string lpUsername, [MarshalAs(UnmanagedType.LPWStr)] string lpDomain, [MarshalAs(UnmanagedType.LPWStr)] string lpPassword, 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_CreateProcessWithLogonW' -Namespace Win32 -PassThru
# $api::CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)#uselib "ADVAPI32.dll"
#func global CreateProcessWithLogonW "CreateProcessWithLogonW" sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr, sptr
; CreateProcessWithLogonW lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, varptr(lpCommandLine), dwCreationFlags, lpEnvironment, lpCurrentDirectory, varptr(lpStartupInfo), varptr(lpProcessInformation) ; 戻り値は stat
; lpUsername : LPCWSTR -> "sptr"
; lpDomain : LPCWSTR optional -> "sptr"
; lpPassword : LPCWSTR -> "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 CreateProcessWithLogonW "CreateProcessWithLogonW" wstr, wstr, wstr, int, wstr, var, int, sptr, wstr, var, var ; res = CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) ; lpUsername : LPCWSTR -> "wstr" ; lpDomain : LPCWSTR optional -> "wstr" ; lpPassword : LPCWSTR -> "wstr" ; 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 方式にも切替可。#uselib "ADVAPI32.dll" #cfunc global CreateProcessWithLogonW "CreateProcessWithLogonW" wstr, wstr, wstr, int, wstr, sptr, int, sptr, wstr, sptr, sptr ; res = CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, varptr(lpCommandLine), dwCreationFlags, lpEnvironment, lpCurrentDirectory, varptr(lpStartupInfo), varptr(lpProcessInformation)) ; lpUsername : LPCWSTR -> "wstr" ; lpDomain : LPCWSTR optional -> "wstr" ; lpPassword : LPCWSTR -> "wstr" ; dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "int" ; lpApplicationName : LPCWSTR optional -> "wstr" ; lpCommandLine : LPWSTR optional, in/out -> "sptr" ; dwCreationFlags : PROCESS_CREATION_FLAGS -> "int" ; lpEnvironment : void* optional -> "sptr" ; lpCurrentDirectory : LPCWSTR optional -> "wstr" ; lpStartupInfo : STARTUPINFOW* -> "sptr" ; lpProcessInformation : PROCESS_INFORMATION* out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOL CreateProcessWithLogonW(LPCWSTR lpUsername, LPCWSTR lpDomain, LPCWSTR lpPassword, 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 CreateProcessWithLogonW "CreateProcessWithLogonW" wstr, wstr, wstr, int, wstr, var, int, intptr, wstr, var, var ; res = CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) ; lpUsername : LPCWSTR -> "wstr" ; lpDomain : LPCWSTR optional -> "wstr" ; lpPassword : LPCWSTR -> "wstr" ; 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 方式にも切替可。; BOOL CreateProcessWithLogonW(LPCWSTR lpUsername, LPCWSTR lpDomain, LPCWSTR lpPassword, 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 CreateProcessWithLogonW "CreateProcessWithLogonW" wstr, wstr, wstr, int, wstr, intptr, int, intptr, wstr, intptr, intptr ; res = CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, varptr(lpCommandLine), dwCreationFlags, lpEnvironment, lpCurrentDirectory, varptr(lpStartupInfo), varptr(lpProcessInformation)) ; lpUsername : LPCWSTR -> "wstr" ; lpDomain : LPCWSTR optional -> "wstr" ; lpPassword : LPCWSTR -> "wstr" ; dwLogonFlags : CREATE_PROCESS_LOGON_FLAGS -> "int" ; lpApplicationName : LPCWSTR optional -> "wstr" ; lpCommandLine : LPWSTR optional, in/out -> "intptr" ; dwCreationFlags : PROCESS_CREATION_FLAGS -> "int" ; lpEnvironment : void* optional -> "intptr" ; lpCurrentDirectory : LPCWSTR optional -> "wstr" ; lpStartupInfo : STARTUPINFOW* -> "intptr" ; lpProcessInformation : PROCESS_INFORMATION* out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procCreateProcessWithLogonW = advapi32.NewProc("CreateProcessWithLogonW")
)
// lpUsername (LPCWSTR), lpDomain (LPCWSTR optional), lpPassword (LPCWSTR), 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 := procCreateProcessWithLogonW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpUsername))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpDomain))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpPassword))),
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 // BOOLfunction CreateProcessWithLogonW(
lpUsername: PWideChar; // LPCWSTR
lpDomain: PWideChar; // LPCWSTR optional
lpPassword: PWideChar; // LPCWSTR
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 'CreateProcessWithLogonW';result := DllCall("ADVAPI32\CreateProcessWithLogonW"
, "WStr", lpUsername ; LPCWSTR
, "WStr", lpDomain ; LPCWSTR optional
, "WStr", lpPassword ; LPCWSTR
, "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●CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation) = DLL("ADVAPI32.dll", "bool CreateProcessWithLogonW(char*, char*, char*, dword, char*, char*, dword, void*, char*, void*, void*)")
# 呼び出し: CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword, dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags, lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInformation)
# lpUsername : LPCWSTR -> "char*"
# lpDomain : LPCWSTR optional -> "char*"
# lpPassword : LPCWSTR -> "char*"
# 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)。