GetWindowsDirectoryW
関数シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetWindowsDirectoryW(
LPWSTR lpBuffer, // optional
DWORD uSize
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpBuffer | LPWSTR | outoptional | パスを受け取るバッファーへのポインターです。Windows ディレクトリがルートディレクトリである場合を除き、このパスは末尾にバックスラッシュを含みません。たとえば、ドライブ C 上の Windows という名前のディレクトリが Windows ディレクトリである場合、この関数が取得する Windows ディレクトリのパスは C:\Windows です。システムがドライブ C のルートディレクトリにインストールされている場合、取得されるパスは C:\ です。 |
| uSize | DWORD | in | lpBuffer パラメーターで指定するバッファーの最大サイズを TCHAR 単位で指定します。この値は MAX_PATH に設定してください。 |
戻り値の型: DWORD
公式ドキュメント
Windows ディレクトリのパスを取得します。(Unicode)
戻り値
関数が成功した場合、戻り値はバッファーにコピーされた文字列の長さ(終端の null 文字を含まない、TCHAR 単位)です。
長さがバッファーのサイズより大きい場合、戻り値はパスを格納するために必要なバッファーのサイズです。
関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、GetLastError を呼び出してください。
解説(Remarks)
Windows ディレクトリは、一部のレガシーアプリケーションが初期化ファイルやヘルプファイルを格納するディレクトリです。新しいアプリケーションは Windows ディレクトリにファイルを格納すべきではありません。代わりに、システム全体で使用するデータはアプリケーションのインストールディレクトリに、ユーザー固有のデータはユーザーのプロファイルに格納してください。
ユーザーが共有バージョンのシステムを実行している場合でも、Windows ディレクトリは各ユーザーごとにプライベートであることが保証されています。
アプリケーションがユーザーごとに格納したい他のファイルを作成する場合は、HOMEPATH 環境変数で指定されるディレクトリにそれらを配置してください。このディレクトリは、管理者が User Manager 管理ツールを通じてそのように指定した場合、各ユーザーごとに異なります。HOMEPATH は常に、各ユーザーごとにプライベートであることが保証されているユーザーのホームディレクトリか、ユーザーがすべてのアクセス権を持つ既定のディレクトリ(たとえば C:\USERS\DEFAULT)のいずれかを指定します。
ターミナルサービス: アプリケーションがターミナルサービス環境で実行されている場合、各ユーザーはプライベートな Windows ディレクトリを持ちます。また、システム用の共有 Windows ディレクトリも存在します。アプリケーションがターミナルサービス対応である(イメージヘッダーに IMAGE_DLLCHARACTERISTICS_TERMINAL_SERVER_AWARE フラグが設定されている)場合、この関数は GetSystemWindowsDirectory 関数と同様に、システムの Windows ディレクトリのパスを返します。それ以外の場合は、ユーザーのプライベートな Windows ディレクトリのパスを取得します。
Examples
例については、Getting System Information を参照してください。
sysinfoapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetWindowsDirectory を定義します。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、コンパイルエラーや実行時エラーを引き起こす不一致につながる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetWindowsDirectoryW(
LPWSTR lpBuffer, // optional
DWORD uSize
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetWindowsDirectoryW(
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, // LPWSTR optional, out
uint uSize // DWORD
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetWindowsDirectoryW(
<MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder, ' LPWSTR optional, out
uSize As UInteger ' DWORD
) As UInteger
End Function' lpBuffer : LPWSTR optional, out
' uSize : DWORD
Declare PtrSafe Function GetWindowsDirectoryW Lib "kernel32" ( _
ByVal lpBuffer As LongPtr, _
ByVal uSize 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
GetWindowsDirectoryW = ctypes.windll.kernel32.GetWindowsDirectoryW
GetWindowsDirectoryW.restype = wintypes.DWORD
GetWindowsDirectoryW.argtypes = [
wintypes.LPWSTR, # lpBuffer : LPWSTR optional, out
wintypes.DWORD, # uSize : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetWindowsDirectoryW = Fiddle::Function.new(
lib['GetWindowsDirectoryW'],
[
Fiddle::TYPE_VOIDP, # lpBuffer : LPWSTR optional, out
-Fiddle::TYPE_INT, # uSize : DWORD
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn GetWindowsDirectoryW(
lpBuffer: *mut u16, // LPWSTR optional, out
uSize: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetWindowsDirectoryW([MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, uint uSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetWindowsDirectoryW' -Namespace Win32 -PassThru
# $api::GetWindowsDirectoryW(lpBuffer, uSize)#uselib "KERNEL32.dll"
#func global GetWindowsDirectoryW "GetWindowsDirectoryW" wptr, wptr
; GetWindowsDirectoryW varptr(lpBuffer), uSize ; 戻り値は stat
; lpBuffer : LPWSTR optional, out -> "wptr"
; uSize : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll" #cfunc global GetWindowsDirectoryW "GetWindowsDirectoryW" var, int ; res = GetWindowsDirectoryW(lpBuffer, uSize) ; lpBuffer : LPWSTR optional, out -> "var" ; uSize : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GetWindowsDirectoryW "GetWindowsDirectoryW" sptr, int ; res = GetWindowsDirectoryW(varptr(lpBuffer), uSize) ; lpBuffer : LPWSTR optional, out -> "sptr" ; uSize : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
; DWORD GetWindowsDirectoryW(LPWSTR lpBuffer, DWORD uSize) #uselib "KERNEL32.dll" #cfunc global GetWindowsDirectoryW "GetWindowsDirectoryW" var, int ; res = GetWindowsDirectoryW(lpBuffer, uSize) ; lpBuffer : LPWSTR optional, out -> "var" ; uSize : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD GetWindowsDirectoryW(LPWSTR lpBuffer, DWORD uSize) #uselib "KERNEL32.dll" #cfunc global GetWindowsDirectoryW "GetWindowsDirectoryW" intptr, int ; res = GetWindowsDirectoryW(varptr(lpBuffer), uSize) ; lpBuffer : LPWSTR optional, out -> "intptr" ; uSize : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetWindowsDirectoryW = kernel32.NewProc("GetWindowsDirectoryW")
)
// lpBuffer (LPWSTR optional, out), uSize (DWORD)
r1, _, err := procGetWindowsDirectoryW.Call(
uintptr(lpBuffer),
uintptr(uSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetWindowsDirectoryW(
lpBuffer: PWideChar; // LPWSTR optional, out
uSize: DWORD // DWORD
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetWindowsDirectoryW';result := DllCall("KERNEL32\GetWindowsDirectoryW"
, "Ptr", lpBuffer ; LPWSTR optional, out
, "UInt", uSize ; DWORD
, "UInt") ; return: DWORD●GetWindowsDirectoryW(lpBuffer, uSize) = DLL("KERNEL32.dll", "dword GetWindowsDirectoryW(char*, dword)")
# 呼び出し: GetWindowsDirectoryW(lpBuffer, uSize)
# lpBuffer : LPWSTR optional, out -> "char*"
# uSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "kernel32" fn GetWindowsDirectoryW(
lpBuffer: [*c]u16, // LPWSTR optional, out
uSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc GetWindowsDirectoryW(
lpBuffer: ptr uint16, # LPWSTR optional, out
uSize: uint32 # DWORD
): uint32 {.importc: "GetWindowsDirectoryW", stdcall, dynlib: "KERNEL32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "kernel32");
extern(Windows)
uint GetWindowsDirectoryW(
wchar* lpBuffer, // LPWSTR optional, out
uint uSize // DWORD
);ccall((:GetWindowsDirectoryW, "KERNEL32.dll"), stdcall, UInt32,
(Ptr{UInt16}, UInt32),
lpBuffer, uSize)
# lpBuffer : LPWSTR optional, out -> Ptr{UInt16}
# uSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t GetWindowsDirectoryW(
uint16_t* lpBuffer,
uint32_t uSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetWindowsDirectoryW(lpBuffer, uSize)
-- lpBuffer : LPWSTR optional, out
-- uSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetWindowsDirectoryW = lib.func('__stdcall', 'GetWindowsDirectoryW', 'uint32_t', ['uint16_t *', 'uint32_t']);
// GetWindowsDirectoryW(lpBuffer, uSize)
// lpBuffer : LPWSTR optional, out -> 'uint16_t *'
// uSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
GetWindowsDirectoryW: { parameters: ["buffer", "u32"], result: "u32" },
});
// lib.symbols.GetWindowsDirectoryW(lpBuffer, uSize)
// lpBuffer : LPWSTR optional, out -> "buffer"
// uSize : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t GetWindowsDirectoryW(
uint16_t* lpBuffer,
uint32_t uSize);
C, "KERNEL32.dll");
// $ffi->GetWindowsDirectoryW(lpBuffer, uSize);
// lpBuffer : LPWSTR optional, out
// uSize : DWORD
// 構造体/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 Kernel32 extends StdCallLibrary {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
int GetWindowsDirectoryW(
char[] lpBuffer, // LPWSTR optional, out
int uSize // DWORD
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("kernel32")]
lib LibKERNEL32
fun GetWindowsDirectoryW = GetWindowsDirectoryW(
lpBuffer : UInt16*, # LPWSTR optional, out
uSize : UInt32 # DWORD
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef GetWindowsDirectoryWNative = Uint32 Function(Pointer<Utf16>, Uint32);
typedef GetWindowsDirectoryWDart = int Function(Pointer<Utf16>, int);
final GetWindowsDirectoryW = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<GetWindowsDirectoryWNative, GetWindowsDirectoryWDart>('GetWindowsDirectoryW');
// lpBuffer : LPWSTR optional, out -> Pointer<Utf16>
// uSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function GetWindowsDirectoryW(
lpBuffer: PWideChar; // LPWSTR optional, out
uSize: DWORD // DWORD
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetWindowsDirectoryW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "GetWindowsDirectoryW"
c_GetWindowsDirectoryW :: CWString -> Word32 -> IO Word32
-- lpBuffer : LPWSTR optional, out -> CWString
-- uSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let getwindowsdirectoryw =
foreign "GetWindowsDirectoryW"
((ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* lpBuffer : LPWSTR optional, out -> (ptr uint16_t) *)
(* uSize : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("GetWindowsDirectoryW" get-windows-directory-w :convention :stdcall) :uint32
(lp-buffer :pointer) ; LPWSTR optional, out
(u-size :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $GetWindowsDirectoryW = Win32::API::More->new('KERNEL32',
'DWORD GetWindowsDirectoryW(LPWSTR lpBuffer, DWORD uSize)');
# my $ret = $GetWindowsDirectoryW->Call($lpBuffer, $uSize);
# lpBuffer : LPWSTR optional, out -> LPWSTR
# uSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f GetWindowsDirectoryA (ANSI版) — Windowsディレクトリのパスを取得する(ANSI版)。
- f GetSystemDirectoryW — Windowsシステムディレクトリのパスを取得する(Unicode版)。
- f GetSystemWindowsDirectoryW — 共有のWindowsディレクトリのパスを取得する(Unicode版)。