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

GetUserNameW

関数
現在のスレッドに関連付けられたユーザー名を取得する(Unicode版)。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetUserNameW(
    LPWSTR lpBuffer,   // optional
    DWORD* pcbBuffer
);

パラメーター

名前方向
lpBufferLPWSTRoutoptional
pcbBufferDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll  (Unicode / -W)
#include <windows.h>

BOOL GetUserNameW(
    LPWSTR lpBuffer,   // optional
    DWORD* pcbBuffer
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool GetUserNameW(
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer,   // LPWSTR optional, out
    ref uint pcbBuffer   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetUserNameW(
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder,   ' LPWSTR optional, out
    ByRef pcbBuffer As UInteger   ' DWORD* in/out
) As Boolean
End Function
' lpBuffer : LPWSTR optional, out
' pcbBuffer : DWORD* in/out
Declare PtrSafe Function GetUserNameW Lib "advapi32" ( _
    ByVal lpBuffer As LongPtr, _
    ByRef pcbBuffer As Long) As Long
' 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

GetUserNameW = ctypes.windll.advapi32.GetUserNameW
GetUserNameW.restype = wintypes.BOOL
GetUserNameW.argtypes = [
    wintypes.LPWSTR,  # lpBuffer : LPWSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # pcbBuffer : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
GetUserNameW = Fiddle::Function.new(
  lib['GetUserNameW'],
  [
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPWSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcbBuffer : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn GetUserNameW(
        lpBuffer: *mut u16,  // LPWSTR optional, out
        pcbBuffer: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool GetUserNameW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, ref uint pcbBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_GetUserNameW' -Namespace Win32 -PassThru
# $api::GetUserNameW(lpBuffer, pcbBuffer)
#uselib "ADVAPI32.dll"
#func global GetUserNameW "GetUserNameW" wptr, wptr
; GetUserNameW varptr(lpBuffer), varptr(pcbBuffer)   ; 戻り値は stat
; lpBuffer : LPWSTR optional, out -> "wptr"
; pcbBuffer : DWORD* in/out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global GetUserNameW "GetUserNameW" var, var
; res = GetUserNameW(lpBuffer, pcbBuffer)
; lpBuffer : LPWSTR optional, out -> "var"
; pcbBuffer : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetUserNameW(LPWSTR lpBuffer, DWORD* pcbBuffer)
#uselib "ADVAPI32.dll"
#cfunc global GetUserNameW "GetUserNameW" var, var
; res = GetUserNameW(lpBuffer, pcbBuffer)
; lpBuffer : LPWSTR optional, out -> "var"
; pcbBuffer : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procGetUserNameW = advapi32.NewProc("GetUserNameW")
)

// lpBuffer (LPWSTR optional, out), pcbBuffer (DWORD* in/out)
r1, _, err := procGetUserNameW.Call(
	uintptr(lpBuffer),
	uintptr(pcbBuffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetUserNameW(
  lpBuffer: PWideChar;   // LPWSTR optional, out
  pcbBuffer: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'GetUserNameW';
result := DllCall("ADVAPI32\GetUserNameW"
    , "Ptr", lpBuffer   ; LPWSTR optional, out
    , "Ptr", pcbBuffer   ; DWORD* in/out
    , "Int")   ; return: BOOL
●GetUserNameW(lpBuffer, pcbBuffer) = DLL("ADVAPI32.dll", "bool GetUserNameW(char*, void*)")
# 呼び出し: GetUserNameW(lpBuffer, pcbBuffer)
# lpBuffer : LPWSTR optional, out -> "char*"
# pcbBuffer : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。