Win32 API 日本語リファレンス
ホームSecurity.Authentication.Identity › GetUserNameExW

GetUserNameExW

関数
指定形式で現在のユーザー名を取得する(Unicode版)。
DLLSECUR32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOLEAN GetUserNameExW(
    EXTENDED_NAME_FORMAT NameFormat,
    LPWSTR lpNameBuffer,   // optional
    DWORD* nSize
);

パラメーター

名前方向説明
NameFormatEXTENDED_NAME_FORMATin取得するユーザー名の形式を示すEXTENDED_NAME_FORMAT列挙値(SamCompatible等)。
lpNameBufferLPWSTRoutoptionalユーザー名を受け取るUnicode文字列バッファへのポインタ。
nSizeDWORD*inout入力時はバッファの文字数、出力時は格納された文字数(終端含まず)を示すDWORDへのポインタ。

戻り値の型: BOOLEAN

各言語での呼び出し定義

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

BOOLEAN GetUserNameExW(
    EXTENDED_NAME_FORMAT NameFormat,
    LPWSTR lpNameBuffer,   // optional
    DWORD* nSize
);
[return: MarshalAs(UnmanagedType.U1)]
[DllImport("SECUR32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool 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 <MarshalAs(UnmanagedType.U1)> Boolean
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 = wintypes.BOOLEAN
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 = @"
[return: MarshalAs(UnmanagedType.U1)]
[DllImport("SECUR32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool 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 方式にも切替可。
出力引数:
; 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 方式にも切替可。
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   // BOOLEAN
function 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 版の利用を推奨。
const std = @import("std");

extern "secur32" fn GetUserNameExW(
    NameFormat: i32, // EXTENDED_NAME_FORMAT
    lpNameBuffer: [*c]u16, // LPWSTR optional, out
    nSize: [*c]u32 // DWORD* in/out
) callconv(std.os.windows.WINAPI) u8;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc GetUserNameExW(
    NameFormat: int32,  # EXTENDED_NAME_FORMAT
    lpNameBuffer: ptr uint16,  # LPWSTR optional, out
    nSize: ptr uint32  # DWORD* in/out
): uint8 {.importc: "GetUserNameExW", stdcall, dynlib: "SECUR32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "secur32");
extern(Windows)
ubyte GetUserNameExW(
    int NameFormat,   // EXTENDED_NAME_FORMAT
    wchar* lpNameBuffer,   // LPWSTR optional, out
    uint* nSize   // DWORD* in/out
);
ccall((:GetUserNameExW, "SECUR32.dll"), stdcall, UInt8,
      (Int32, Ptr{UInt16}, Ptr{UInt32}),
      NameFormat, lpNameBuffer, nSize)
# NameFormat : EXTENDED_NAME_FORMAT -> Int32
# lpNameBuffer : LPWSTR optional, out -> Ptr{UInt16}
# nSize : DWORD* in/out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint8_t GetUserNameExW(
    int32_t NameFormat,
    uint16_t* lpNameBuffer,
    uint32_t* nSize);
]]
local secur32 = ffi.load("secur32")
-- secur32.GetUserNameExW(NameFormat, lpNameBuffer, nSize)
-- NameFormat : EXTENDED_NAME_FORMAT
-- lpNameBuffer : LPWSTR optional, out
-- nSize : DWORD* in/out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('SECUR32.dll');
const GetUserNameExW = lib.func('__stdcall', 'GetUserNameExW', 'uint8_t', ['int32_t', 'uint16_t *', 'uint32_t *']);
// GetUserNameExW(NameFormat, lpNameBuffer, nSize)
// NameFormat : EXTENDED_NAME_FORMAT -> 'int32_t'
// lpNameBuffer : LPWSTR optional, out -> 'uint16_t *'
// nSize : DWORD* in/out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SECUR32.dll", {
  GetUserNameExW: { parameters: ["i32", "buffer", "pointer"], result: "u8" },
});
// lib.symbols.GetUserNameExW(NameFormat, lpNameBuffer, nSize)
// NameFormat : EXTENDED_NAME_FORMAT -> "i32"
// lpNameBuffer : LPWSTR optional, out -> "buffer"
// nSize : DWORD* in/out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint8_t GetUserNameExW(
    int32_t NameFormat,
    uint16_t* lpNameBuffer,
    uint32_t* nSize);
C, "SECUR32.dll");
// $ffi->GetUserNameExW(NameFormat, lpNameBuffer, nSize);
// NameFormat : EXTENDED_NAME_FORMAT
// lpNameBuffer : LPWSTR optional, out
// nSize : DWORD* in/out
// 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
// WINAPI(stdcall): x64 では呼出規約が統一されるため問題なし。x86 では __stdcall 対応のラッパが必要な場合あり。
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public interface Secur32 extends StdCallLibrary {
    Secur32 INSTANCE = Native.load("secur32", Secur32.class, W32APIOptions.UNICODE_OPTIONS);
    byte GetUserNameExW(
        int NameFormat,   // EXTENDED_NAME_FORMAT
        char[] lpNameBuffer,   // LPWSTR optional, out
        IntByReference nSize   // DWORD* in/out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("secur32")]
lib LibSECUR32
  fun GetUserNameExW = GetUserNameExW(
    NameFormat : Int32,   # EXTENDED_NAME_FORMAT
    lpNameBuffer : UInt16*,   # LPWSTR optional, out
    nSize : UInt32*   # DWORD* in/out
  ) : UInt8
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef GetUserNameExWNative = Uint8 Function(Int32, Pointer<Utf16>, Pointer<Uint32>);
typedef GetUserNameExWDart = int Function(int, Pointer<Utf16>, Pointer<Uint32>);
final GetUserNameExW = DynamicLibrary.open('SECUR32.dll')
    .lookupFunction<GetUserNameExWNative, GetUserNameExWDart>('GetUserNameExW');
// NameFormat : EXTENDED_NAME_FORMAT -> Int32
// lpNameBuffer : LPWSTR optional, out -> Pointer<Utf16>
// nSize : DWORD* in/out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetUserNameExW(
  NameFormat: Integer;   // EXTENDED_NAME_FORMAT
  lpNameBuffer: PWideChar;   // LPWSTR optional, out
  nSize: Pointer   // DWORD* in/out
): ByteBool; stdcall;
  external 'SECUR32.dll' name 'GetUserNameExW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetUserNameExW"
  c_GetUserNameExW :: Int32 -> CWString -> Ptr Word32 -> IO Word8
-- NameFormat : EXTENDED_NAME_FORMAT -> Int32
-- lpNameBuffer : LPWSTR optional, out -> CWString
-- nSize : DWORD* in/out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getusernameexw =
  foreign "GetUserNameExW"
    (int32_t @-> (ptr uint16_t) @-> (ptr uint32_t) @-> returning uint8_t)
(* NameFormat : EXTENDED_NAME_FORMAT -> int32_t *)
(* lpNameBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* nSize : DWORD* in/out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library secur32 (t "SECUR32.dll"))
(cffi:use-foreign-library secur32)

(cffi:defcfun ("GetUserNameExW" get-user-name-ex-w :convention :stdcall) :uint8
  (name-format :int32)   ; EXTENDED_NAME_FORMAT
  (lp-name-buffer :pointer)   ; LPWSTR optional, out
  (n-size :pointer))   ; DWORD* in/out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetUserNameExW = Win32::API::More->new('SECUR32',
    'BYTE GetUserNameExW(int NameFormat, LPWSTR lpNameBuffer, LPVOID nSize)');
# my $ret = $GetUserNameExW->Call($NameFormat, $lpNameBuffer, $nSize);
# NameFormat : EXTENDED_NAME_FORMAT -> int
# lpNameBuffer : LPWSTR optional, out -> LPWSTR
# nSize : DWORD* in/out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型