ホーム › System.Environment › GetEnvironmentVariableW
GetEnvironmentVariableW
関数指定した環境変数の値を取得する(Unicode版)。
シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetEnvironmentVariableW(
LPCWSTR lpName, // optional
LPWSTR lpBuffer, // optional
DWORD nSize
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpName | LPCWSTR | inoptional |
| lpBuffer | LPWSTR | outoptional |
| nSize | DWORD | in |
戻り値の型: DWORD
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
DWORD GetEnvironmentVariableW(
LPCWSTR lpName, // optional
LPWSTR lpBuffer, // optional
DWORD nSize
);[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern uint GetEnvironmentVariableW(
[MarshalAs(UnmanagedType.LPWStr)] string lpName, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, // LPWSTR optional, out
uint nSize // DWORD
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function GetEnvironmentVariableW(
<MarshalAs(UnmanagedType.LPWStr)> lpName As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpBuffer As System.Text.StringBuilder, ' LPWSTR optional, out
nSize As UInteger ' DWORD
) As UInteger
End Function' lpName : LPCWSTR optional
' lpBuffer : LPWSTR optional, out
' nSize : DWORD
Declare PtrSafe Function GetEnvironmentVariableW Lib "kernel32" ( _
ByVal lpName As LongPtr, _
ByVal lpBuffer As LongPtr, _
ByVal nSize 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
GetEnvironmentVariableW = ctypes.windll.kernel32.GetEnvironmentVariableW
GetEnvironmentVariableW.restype = wintypes.DWORD
GetEnvironmentVariableW.argtypes = [
wintypes.LPCWSTR, # lpName : LPCWSTR optional
wintypes.LPWSTR, # lpBuffer : LPWSTR optional, out
wintypes.DWORD, # nSize : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
GetEnvironmentVariableW = Fiddle::Function.new(
lib['GetEnvironmentVariableW'],
[
Fiddle::TYPE_VOIDP, # lpName : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpBuffer : LPWSTR optional, out
-Fiddle::TYPE_INT, # nSize : DWORD
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn GetEnvironmentVariableW(
lpName: *const u16, // LPCWSTR optional
lpBuffer: *mut u16, // LPWSTR optional, out
nSize: 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 GetEnvironmentVariableW([MarshalAs(UnmanagedType.LPWStr)] string lpName, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder lpBuffer, uint nSize);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_GetEnvironmentVariableW' -Namespace Win32 -PassThru
# $api::GetEnvironmentVariableW(lpName, lpBuffer, nSize)#uselib "KERNEL32.dll"
#func global GetEnvironmentVariableW "GetEnvironmentVariableW" wptr, wptr, wptr
; GetEnvironmentVariableW lpName, varptr(lpBuffer), nSize ; 戻り値は stat
; lpName : LPCWSTR optional -> "wptr"
; lpBuffer : LPWSTR optional, out -> "wptr"
; nSize : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。出力引数:
#uselib "KERNEL32.dll" #cfunc global GetEnvironmentVariableW "GetEnvironmentVariableW" wstr, var, int ; res = GetEnvironmentVariableW(lpName, lpBuffer, nSize) ; lpName : LPCWSTR optional -> "wstr" ; lpBuffer : LPWSTR optional, out -> "var" ; nSize : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。#uselib "KERNEL32.dll" #cfunc global GetEnvironmentVariableW "GetEnvironmentVariableW" wstr, sptr, int ; res = GetEnvironmentVariableW(lpName, varptr(lpBuffer), nSize) ; lpName : LPCWSTR optional -> "wstr" ; lpBuffer : LPWSTR optional, out -> "sptr" ; nSize : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=sptr / 呼び出しは varptr(変数))。
出力引数:
; DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize) #uselib "KERNEL32.dll" #cfunc global GetEnvironmentVariableW "GetEnvironmentVariableW" wstr, var, int ; res = GetEnvironmentVariableW(lpName, lpBuffer, nSize) ; lpName : LPCWSTR optional -> "wstr" ; lpBuffer : LPWSTR optional, out -> "var" ; nSize : DWORD -> "int" ; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。; DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize) #uselib "KERNEL32.dll" #cfunc global GetEnvironmentVariableW "GetEnvironmentVariableW" wstr, intptr, int ; res = GetEnvironmentVariableW(lpName, varptr(lpBuffer), nSize) ; lpName : LPCWSTR optional -> "wstr" ; lpBuffer : LPWSTR optional, out -> "intptr" ; nSize : DWORD -> "int" ; ※出力/バッファ引数はポインタ方式(token=intptr / 呼び出しは varptr(変数))。
import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procGetEnvironmentVariableW = kernel32.NewProc("GetEnvironmentVariableW")
)
// lpName (LPCWSTR optional), lpBuffer (LPWSTR optional, out), nSize (DWORD)
r1, _, err := procGetEnvironmentVariableW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
uintptr(lpBuffer),
uintptr(nSize),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // DWORDfunction GetEnvironmentVariableW(
lpName: PWideChar; // LPCWSTR optional
lpBuffer: PWideChar; // LPWSTR optional, out
nSize: DWORD // DWORD
): DWORD; stdcall;
external 'KERNEL32.dll' name 'GetEnvironmentVariableW';result := DllCall("KERNEL32\GetEnvironmentVariableW"
, "WStr", lpName ; LPCWSTR optional
, "Ptr", lpBuffer ; LPWSTR optional, out
, "UInt", nSize ; DWORD
, "UInt") ; return: DWORD●GetEnvironmentVariableW(lpName, lpBuffer, nSize) = DLL("KERNEL32.dll", "dword GetEnvironmentVariableW(char*, char*, dword)")
# 呼び出し: GetEnvironmentVariableW(lpName, lpBuffer, nSize)
# lpName : LPCWSTR optional -> "char*"
# lpBuffer : LPWSTR optional, out -> "char*"
# nSize : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。