ホーム › Security.Authentication.Identity › GetUserNameExW
GetUserNameExW
関数指定形式で現在のユーザー名を取得する(Unicode版)。
シグネチャ
// SECUR32.dll (Unicode / -W)
#include <windows.h>
BOOLEAN GetUserNameExW(
EXTENDED_NAME_FORMAT NameFormat,
LPWSTR lpNameBuffer, // optional
DWORD* nSize
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| NameFormat | EXTENDED_NAME_FORMAT | in |
| lpNameBuffer | LPWSTR | outoptional |
| nSize | DWORD* | inout |
戻り値の型: BOOLEAN
各言語での呼び出し定義
// SECUR32.dll (Unicode / -W)
#include <windows.h>
BOOLEAN GetUserNameExW(
EXTENDED_NAME_FORMAT NameFormat,
LPWSTR lpNameBuffer, // optional
DWORD* nSize
);[DllImport("SECUR32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern byte GetUserNameExW(
int NameFormat, // EXTENDED_NAME_FORMAT
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpNameBuffer, // LPWSTR optional, out
ref uint nSize // DWORD* in/out
);<DllImport("SECUR32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetUserNameExW(
NameFormat As Integer, ' EXTENDED_NAME_FORMAT
<MarshalAs(UnmanagedType.LPWStr)> lpNameBuffer As System.Text.StringBuilder, ' LPWSTR optional, out
ByRef nSize As UInteger ' DWORD* in/out
) As Byte
End Function' NameFormat : EXTENDED_NAME_FORMAT
' lpNameBuffer : LPWSTR optional, out
' nSize : DWORD* in/out
Declare PtrSafe Function GetUserNameExW Lib "secur32" ( _
ByVal NameFormat As Long, _
ByVal lpNameBuffer As LongPtr, _
ByRef nSize As Long) As Byte
' Unicode(W): 文字列は ByVal As LongPtr とし StrPtr(unicodeStr) を渡す
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
GetUserNameExW = ctypes.windll.secur32.GetUserNameExW
GetUserNameExW.restype = ctypes.c_byte
GetUserNameExW.argtypes = [
ctypes.c_int, # NameFormat : EXTENDED_NAME_FORMAT
wintypes.LPWSTR, # lpNameBuffer : LPWSTR optional, out
ctypes.POINTER(wintypes.DWORD), # nSize : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SECUR32.dll')
GetUserNameExW = Fiddle::Function.new(
lib['GetUserNameExW'],
[
Fiddle::TYPE_INT, # NameFormat : EXTENDED_NAME_FORMAT
Fiddle::TYPE_VOIDP, # lpNameBuffer : LPWSTR optional, out
Fiddle::TYPE_VOIDP, # nSize : DWORD* in/out
],
Fiddle::TYPE_CHAR)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "secur32")]
extern "system" {
fn GetUserNameExW(
NameFormat: i32, // EXTENDED_NAME_FORMAT
lpNameBuffer: *mut u16, // LPWSTR optional, out
nSize: *mut u32 // DWORD* in/out
) -> u8;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SECUR32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern byte GetUserNameExW(int NameFormat, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpNameBuffer, ref uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SECUR32_GetUserNameExW' -Namespace Win32 -PassThru
# $api::GetUserNameExW(NameFormat, lpNameBuffer, nSize)#uselib "SECUR32.dll"
#func global GetUserNameExW "GetUserNameExW" wptr, wptr, wptr
; GetUserNameExW NameFormat, varptr(lpNameBuffer), varptr(nSize) ; 戻り値は stat
; NameFormat : EXTENDED_NAME_FORMAT -> "wptr"
; lpNameBuffer : LPWSTR optional, out -> "wptr"
; nSize : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "SECUR32.dll" #cfunc global GetUserNameExW "GetUserNameExW" int, var, var ; res = GetUserNameExW(NameFormat, lpNameBuffer, nSize) ; NameFormat : EXTENDED_NAME_FORMAT -> "int" ; lpNameBuffer : LPWSTR optional, out -> "var" ; nSize : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "SECUR32.dll" #cfunc global GetUserNameExW "GetUserNameExW" int, sptr, sptr ; res = GetUserNameExW(NameFormat, varptr(lpNameBuffer), varptr(nSize)) ; NameFormat : EXTENDED_NAME_FORMAT -> "int" ; lpNameBuffer : LPWSTR optional, out -> "sptr" ; nSize : DWORD* in/out -> "sptr" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; BOOLEAN GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, DWORD* nSize) #uselib "SECUR32.dll" #cfunc global GetUserNameExW "GetUserNameExW" int, var, var ; res = GetUserNameExW(NameFormat, lpNameBuffer, nSize) ; NameFormat : EXTENDED_NAME_FORMAT -> "int" ; lpNameBuffer : LPWSTR optional, out -> "var" ; nSize : DWORD* in/out -> "var" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; BOOLEAN GetUserNameExW(EXTENDED_NAME_FORMAT NameFormat, LPWSTR lpNameBuffer, DWORD* nSize) #uselib "SECUR32.dll" #cfunc global GetUserNameExW "GetUserNameExW" int, intptr, intptr ; res = GetUserNameExW(NameFormat, varptr(lpNameBuffer), varptr(nSize)) ; NameFormat : EXTENDED_NAME_FORMAT -> "int" ; lpNameBuffer : LPWSTR optional, out -> "intptr" ; nSize : DWORD* in/out -> "intptr" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
secur32 = windows.NewLazySystemDLL("SECUR32.dll")
procGetUserNameExW = secur32.NewProc("GetUserNameExW")
)
// NameFormat (EXTENDED_NAME_FORMAT), lpNameBuffer (LPWSTR optional, out), nSize (DWORD* in/out)
r1, _, err := procGetUserNameExW.Call(
uintptr(NameFormat),
uintptr(lpNameBuffer),
uintptr(nSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLEANfunction GetUserNameExW(
NameFormat: Integer; // EXTENDED_NAME_FORMAT
lpNameBuffer: PWideChar; // LPWSTR optional, out
nSize: Pointer // DWORD* in/out
): ByteBool; stdcall;
external 'SECUR32.dll' name 'GetUserNameExW';result := DllCall("SECUR32\GetUserNameExW"
, "Int", NameFormat ; EXTENDED_NAME_FORMAT
, "Ptr", lpNameBuffer ; LPWSTR optional, out
, "Ptr", nSize ; DWORD* in/out
, "Char") ; return: BOOLEAN●GetUserNameExW(NameFormat, lpNameBuffer, nSize) = DLL("SECUR32.dll", "byte GetUserNameExW(int, char*, void*)")
# 呼び出し: GetUserNameExW(NameFormat, lpNameBuffer, nSize)
# NameFormat : EXTENDED_NAME_FORMAT -> "int"
# lpNameBuffer : LPWSTR optional, out -> "char*"
# nSize : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。