Win32 API 日本語リファレンス
ホームStorage.FileSystem › GetTempPathW

GetTempPathW

関数
一時ファイル用ディレクトリのパスを取得する。
DLLKERNEL32.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

DWORD GetTempPathW(
    DWORD nBufferLength,
    LPWSTR lpBuffer   // optional
);

パラメーター

名前方向
nBufferLengthDWORDin
lpBufferLPWSTRoutoptional

戻り値の型: DWORD

各言語での呼び出し定義

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

DWORD GetTempPathW(
    DWORD nBufferLength,
    LPWSTR lpBuffer   // optional
);
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetTempPathW(
    uint nBufferLength,   // DWORD
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer   // LPWSTR optional, out
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetTempPathW(
    nBufferLength As UInteger,   ' DWORD
    <MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder   ' LPWSTR optional, out
) As UInteger
End Function
' nBufferLength : DWORD
' lpBuffer : LPWSTR optional, out
Declare PtrSafe Function GetTempPathW Lib "kernel32" ( _
    ByVal nBufferLength As Long, _
    ByVal lpBuffer As LongPtr) 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

GetTempPathW = ctypes.windll.kernel32.GetTempPathW
GetTempPathW.restype = wintypes.DWORD
GetTempPathW.argtypes = [
    wintypes.DWORD,  # nBufferLength : DWORD
    wintypes.LPWSTR,  # lpBuffer : LPWSTR optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetTempPathW = kernel32.NewProc("GetTempPathW")
)

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