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

GetUserNameA

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

シグネチャ

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

BOOL GetUserNameA(
    LPSTR lpBuffer,   // optional
    DWORD* pcbBuffer
);

パラメーター

名前方向
lpBufferLPSTRoutoptional
pcbBufferDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

// ADVAPI32.dll  (ANSI / -A)
#include <windows.h>

BOOL GetUserNameA(
    LPSTR lpBuffer,   // optional
    DWORD* pcbBuffer
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool GetUserNameA(
    [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer,   // LPSTR optional, out
    ref uint pcbBuffer   // DWORD* in/out
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetUserNameA(
    <MarshalAs(UnmanagedType.LPStr)> lpBuffer As System.Text.StringBuilder,   ' LPSTR optional, out
    ByRef pcbBuffer As UInteger   ' DWORD* in/out
) As Boolean
End Function
' lpBuffer : LPSTR optional, out
' pcbBuffer : DWORD* in/out
Declare PtrSafe Function GetUserNameA Lib "advapi32" ( _
    ByVal lpBuffer As String, _
    ByRef pcbBuffer As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

GetUserNameA = ctypes.windll.advapi32.GetUserNameA
GetUserNameA.restype = wintypes.BOOL
GetUserNameA.argtypes = [
    wintypes.LPSTR,  # lpBuffer : LPSTR 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')
GetUserNameA = Fiddle::Function.new(
  lib['GetUserNameA'],
  [
    Fiddle::TYPE_VOIDP,  # lpBuffer : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # pcbBuffer : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "advapi32")]
extern "system" {
    fn GetUserNameA(
        lpBuffer: *mut u8,  // LPSTR 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.Ansi, SetLastError = true)]
public static extern bool GetUserNameA([MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpBuffer, ref uint pcbBuffer);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_GetUserNameA' -Namespace Win32 -PassThru
# $api::GetUserNameA(lpBuffer, pcbBuffer)
#uselib "ADVAPI32.dll"
#func global GetUserNameA "GetUserNameA" sptr, sptr
; GetUserNameA varptr(lpBuffer), varptr(pcbBuffer)   ; 戻り値は stat
; lpBuffer : LPSTR optional, out -> "sptr"
; pcbBuffer : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "ADVAPI32.dll"
#cfunc global GetUserNameA "GetUserNameA" var, var
; res = GetUserNameA(lpBuffer, pcbBuffer)
; lpBuffer : LPSTR optional, out -> "var"
; pcbBuffer : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetUserNameA(LPSTR lpBuffer, DWORD* pcbBuffer)
#uselib "ADVAPI32.dll"
#cfunc global GetUserNameA "GetUserNameA" var, var
; res = GetUserNameA(lpBuffer, pcbBuffer)
; lpBuffer : LPSTR optional, out -> "var"
; pcbBuffer : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procGetUserNameA = advapi32.NewProc("GetUserNameA")
)

// lpBuffer (LPSTR optional, out), pcbBuffer (DWORD* in/out)
r1, _, err := procGetUserNameA.Call(
	uintptr(lpBuffer),
	uintptr(pcbBuffer),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function GetUserNameA(
  lpBuffer: PAnsiChar;   // LPSTR optional, out
  pcbBuffer: Pointer   // DWORD* in/out
): BOOL; stdcall;
  external 'ADVAPI32.dll' name 'GetUserNameA';
result := DllCall("ADVAPI32\GetUserNameA"
    , "Ptr", lpBuffer   ; LPSTR optional, out
    , "Ptr", pcbBuffer   ; DWORD* in/out
    , "Int")   ; return: BOOL
●GetUserNameA(lpBuffer, pcbBuffer) = DLL("ADVAPI32.dll", "bool GetUserNameA(char*, void*)")
# 呼び出し: GetUserNameA(lpBuffer, pcbBuffer)
# lpBuffer : LPSTR optional, out -> "char*"
# pcbBuffer : DWORD* in/out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。