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

GetSystemWindowsDirectoryA

関数
共有のWindowsディレクトリのパスを取得する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

DWORD GetSystemWindowsDirectoryA(
    LPSTR lpBuffer,   // optional
    DWORD uSize
);

パラメーター

名前方向説明
lpBufferLPSTRoutoptionalパスを受け取るバッファーへのポインター。Windows ディレクトリがルートディレクトリである場合を除き、このパスの末尾にバックスラッシュは付きません。たとえば、ドライブ C 上の Windows という名前のディレクトリが Windows ディレクトリである場合、この関数が取得する Windows ディレクトリのパスは C:\Windows です。システムがドライブ C のルートディレクトリにインストールされている場合、取得されるパスは C:\ です。
uSizeDWORDinlpBuffer パラメーターで指定されたバッファーの最大サイズ。単位は TCHAR です。

戻り値の型: DWORD

公式ドキュメント

マルチユーザーシステムにおける共有 Windows ディレクトリのパスを取得します。(ANSI)

戻り値

関数が成功した場合、戻り値はバッファーにコピーされた文字列の長さ(終端の null 文字を含まない、TCHAR 単位)です。

長さがバッファーのサイズより大きい場合、戻り値はパスを格納するために必要なバッファーのサイズです。

関数が失敗した場合、戻り値は 0 です。拡張エラー情報を取得するには、 GetLastError を呼び出します。

解説(Remarks)

ターミナルサービスが動作しているシステムでは、各ユーザーが固有の Windows ディレクトリを持ちます。システムの Windows ディレクトリはすべてのユーザーで共有されるため、すべてのユーザーに適用される初期化ファイルやヘルプファイルをアプリケーションが格納すべきディレクトリです。

ターミナルサービスでは、 GetSystemWindowsDirectory 関数がシステムの Windows ディレクトリのパスを取得するのに対し、 GetWindowsDirectory 関数は各ユーザー固有の Windows ディレクトリのパスを取得します。シングルユーザーシステムでは、 GetSystemWindowsDirectoryGetWindowsDirectory と同じです。

メモ

sysinfoapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして GetSystemWindowsDirectory を定義しています。エンコーディング中立なエイリアスの使用と、エンコーディング中立でないコードを混在させると、コンパイルエラーや実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、関数プロトタイプの規則を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

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

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

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

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procGetSystemWindowsDirectoryA = kernel32.NewProc("GetSystemWindowsDirectoryA")
)

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

extern "kernel32" fn GetSystemWindowsDirectoryA(
    lpBuffer: [*c]u8, // LPSTR optional, out
    uSize: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc GetSystemWindowsDirectoryA(
    lpBuffer: ptr char,  # LPSTR optional, out
    uSize: uint32  # DWORD
): uint32 {.importc: "GetSystemWindowsDirectoryA", stdcall, dynlib: "KERNEL32.dll".}
pragma(lib, "kernel32");
extern(Windows)
uint GetSystemWindowsDirectoryA(
    char* lpBuffer,   // LPSTR optional, out
    uint uSize   // DWORD
);
ccall((:GetSystemWindowsDirectoryA, "KERNEL32.dll"), stdcall, UInt32,
      (Ptr{UInt8}, UInt32),
      lpBuffer, uSize)
# lpBuffer : LPSTR optional, out -> Ptr{UInt8}
# uSize : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t GetSystemWindowsDirectoryA(
    char* lpBuffer,
    uint32_t uSize);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.GetSystemWindowsDirectoryA(lpBuffer, uSize)
-- lpBuffer : LPSTR optional, out
-- uSize : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const GetSystemWindowsDirectoryA = lib.func('__stdcall', 'GetSystemWindowsDirectoryA', 'uint32_t', ['char *', 'uint32_t']);
// GetSystemWindowsDirectoryA(lpBuffer, uSize)
// lpBuffer : LPSTR optional, out -> 'char *'
// uSize : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("KERNEL32.dll", {
  GetSystemWindowsDirectoryA: { parameters: ["buffer", "u32"], result: "u32" },
});
// lib.symbols.GetSystemWindowsDirectoryA(lpBuffer, uSize)
// lpBuffer : LPSTR optional, out -> "buffer"
// uSize : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t GetSystemWindowsDirectoryA(
    char* lpBuffer,
    uint32_t uSize);
C, "KERNEL32.dll");
// $ffi->GetSystemWindowsDirectoryA(lpBuffer, uSize);
// lpBuffer : LPSTR 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.ASCII_OPTIONS);
    int GetSystemWindowsDirectoryA(
        byte[] lpBuffer,   // LPSTR optional, out
        int uSize   // DWORD
    );
}
@[Link("kernel32")]
lib LibKERNEL32
  fun GetSystemWindowsDirectoryA = GetSystemWindowsDirectoryA(
    lpBuffer : UInt8*,   # LPSTR 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 GetSystemWindowsDirectoryANative = Uint32 Function(Pointer<Utf8>, Uint32);
typedef GetSystemWindowsDirectoryADart = int Function(Pointer<Utf8>, int);
final GetSystemWindowsDirectoryA = DynamicLibrary.open('KERNEL32.dll')
    .lookupFunction<GetSystemWindowsDirectoryANative, GetSystemWindowsDirectoryADart>('GetSystemWindowsDirectoryA');
// lpBuffer : LPSTR optional, out -> Pointer<Utf8>
// uSize : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function GetSystemWindowsDirectoryA(
  lpBuffer: PAnsiChar;   // LPSTR optional, out
  uSize: DWORD   // DWORD
): DWORD; stdcall;
  external 'KERNEL32.dll' name 'GetSystemWindowsDirectoryA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "GetSystemWindowsDirectoryA"
  c_GetSystemWindowsDirectoryA :: CString -> Word32 -> IO Word32
-- lpBuffer : LPSTR optional, out -> CString
-- uSize : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let getsystemwindowsdirectorya =
  foreign "GetSystemWindowsDirectoryA"
    (string @-> uint32_t @-> returning uint32_t)
(* lpBuffer : LPSTR optional, out -> string *)
(* 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 ("GetSystemWindowsDirectoryA" get-system-windows-directory-a :convention :stdcall) :uint32
  (lp-buffer :pointer)   ; LPSTR optional, out
  (u-size :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $GetSystemWindowsDirectoryA = Win32::API::More->new('KERNEL32',
    'DWORD GetSystemWindowsDirectoryA(LPSTR lpBuffer, DWORD uSize)');
# my $ret = $GetSystemWindowsDirectoryA->Call($lpBuffer, $uSize);
# lpBuffer : LPSTR optional, out -> LPSTR
# uSize : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

文字セット違い
公式の関連項目