Win32 API 日本語リファレンス
ホームUI.Shell › GetUserProfileDirectoryA

GetUserProfileDirectoryA

関数
トークンが示すユーザーのプロファイルディレクトリを取得する(ANSI版)。
DLLUSERENV.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

BOOL GetUserProfileDirectoryA(
    HANDLE hToken,
    LPSTR lpProfileDir,   // optional
    DWORD* lpcchSize
);

パラメーター

名前方向
hTokenHANDLEin
lpProfileDirLPSTRoutoptional
lpcchSizeDWORD*inout

戻り値の型: BOOL

各言語での呼び出し定義

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

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

GetUserProfileDirectoryA = ctypes.windll.userenv.GetUserProfileDirectoryA
GetUserProfileDirectoryA.restype = wintypes.BOOL
GetUserProfileDirectoryA.argtypes = [
    wintypes.HANDLE,  # hToken : HANDLE
    wintypes.LPSTR,  # lpProfileDir : LPSTR optional, out
    ctypes.POINTER(wintypes.DWORD),  # lpcchSize : DWORD* in/out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('USERENV.dll')
GetUserProfileDirectoryA = Fiddle::Function.new(
  lib['GetUserProfileDirectoryA'],
  [
    Fiddle::TYPE_VOIDP,  # hToken : HANDLE
    Fiddle::TYPE_VOIDP,  # lpProfileDir : LPSTR optional, out
    Fiddle::TYPE_VOIDP,  # lpcchSize : DWORD* in/out
  ],
  Fiddle::TYPE_INT)
#[link(name = "userenv")]
extern "system" {
    fn GetUserProfileDirectoryA(
        hToken: *mut core::ffi::c_void,  // HANDLE
        lpProfileDir: *mut u8,  // LPSTR optional, out
        lpcchSize: *mut u32  // DWORD* in/out
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("USERENV.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool GetUserProfileDirectoryA(IntPtr hToken, [MarshalAs(UnmanagedType.LPStr)] System.Text.StringBuilder lpProfileDir, ref uint lpcchSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'USERENV_GetUserProfileDirectoryA' -Namespace Win32 -PassThru
# $api::GetUserProfileDirectoryA(hToken, lpProfileDir, lpcchSize)
#uselib "USERENV.dll"
#func global GetUserProfileDirectoryA "GetUserProfileDirectoryA" sptr, sptr, sptr
; GetUserProfileDirectoryA hToken, varptr(lpProfileDir), varptr(lpcchSize)   ; 戻り値は stat
; hToken : HANDLE -> "sptr"
; lpProfileDir : LPSTR optional, out -> "sptr"
; lpcchSize : DWORD* in/out -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "USERENV.dll"
#cfunc global GetUserProfileDirectoryA "GetUserProfileDirectoryA" sptr, var, var
; res = GetUserProfileDirectoryA(hToken, lpProfileDir, lpcchSize)
; hToken : HANDLE -> "sptr"
; lpProfileDir : LPSTR optional, out -> "var"
; lpcchSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; BOOL GetUserProfileDirectoryA(HANDLE hToken, LPSTR lpProfileDir, DWORD* lpcchSize)
#uselib "USERENV.dll"
#cfunc global GetUserProfileDirectoryA "GetUserProfileDirectoryA" intptr, var, var
; res = GetUserProfileDirectoryA(hToken, lpProfileDir, lpcchSize)
; hToken : HANDLE -> "intptr"
; lpProfileDir : LPSTR optional, out -> "var"
; lpcchSize : DWORD* in/out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	userenv = windows.NewLazySystemDLL("USERENV.dll")
	procGetUserProfileDirectoryA = userenv.NewProc("GetUserProfileDirectoryA")
)

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