ホーム › System.Environment › SetEnvironmentVariableW
SetEnvironmentVariableW
関数指定した環境変数の値を設定または削除する(Unicode版)。
シグネチャ
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL SetEnvironmentVariableW(
LPCWSTR lpName,
LPCWSTR lpValue // optional
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| lpName | LPCWSTR | in |
| lpValue | LPCWSTR | inoptional |
戻り値の型: BOOL
各言語での呼び出し定義
// KERNEL32.dll (Unicode / -W)
#include <windows.h>
BOOL SetEnvironmentVariableW(
LPCWSTR lpName,
LPCWSTR lpValue // optional
);[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern bool SetEnvironmentVariableW(
[MarshalAs(UnmanagedType.LPWStr)] string lpName, // LPCWSTR
[MarshalAs(UnmanagedType.LPWStr)] string lpValue // LPCWSTR optional
);<DllImport("KERNEL32.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetEnvironmentVariableW(
<MarshalAs(UnmanagedType.LPWStr)> lpName As String, ' LPCWSTR
<MarshalAs(UnmanagedType.LPWStr)> lpValue As String ' LPCWSTR optional
) As Boolean
End Function' lpName : LPCWSTR
' lpValue : LPCWSTR optional
Declare PtrSafe Function SetEnvironmentVariableW Lib "kernel32" ( _
ByVal lpName As LongPtr, _
ByVal lpValue 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
SetEnvironmentVariableW = ctypes.windll.kernel32.SetEnvironmentVariableW
SetEnvironmentVariableW.restype = wintypes.BOOL
SetEnvironmentVariableW.argtypes = [
wintypes.LPCWSTR, # lpName : LPCWSTR
wintypes.LPCWSTR, # lpValue : LPCWSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('KERNEL32.dll')
SetEnvironmentVariableW = Fiddle::Function.new(
lib['SetEnvironmentVariableW'],
[
Fiddle::TYPE_VOIDP, # lpName : LPCWSTR
Fiddle::TYPE_VOIDP, # lpValue : LPCWSTR optional
],
Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "kernel32")]
extern "system" {
fn SetEnvironmentVariableW(
lpName: *const u16, // LPCWSTR
lpValue: *const u16 // LPCWSTR optional
) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool SetEnvironmentVariableW([MarshalAs(UnmanagedType.LPWStr)] string lpName, [MarshalAs(UnmanagedType.LPWStr)] string lpValue);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetEnvironmentVariableW' -Namespace Win32 -PassThru
# $api::SetEnvironmentVariableW(lpName, lpValue)#uselib "KERNEL32.dll"
#func global SetEnvironmentVariableW "SetEnvironmentVariableW" wptr, wptr
; SetEnvironmentVariableW lpName, lpValue ; 戻り値は stat
; lpName : LPCWSTR -> "wptr"
; lpValue : LPCWSTR optional -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "KERNEL32.dll"
#cfunc global SetEnvironmentVariableW "SetEnvironmentVariableW" wstr, wstr
; res = SetEnvironmentVariableW(lpName, lpValue)
; lpName : LPCWSTR -> "wstr"
; lpValue : LPCWSTR optional -> "wstr"; BOOL SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue)
#uselib "KERNEL32.dll"
#cfunc global SetEnvironmentVariableW "SetEnvironmentVariableW" wstr, wstr
; res = SetEnvironmentVariableW(lpName, lpValue)
; lpName : LPCWSTR -> "wstr"
; lpValue : LPCWSTR optional -> "wstr"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
procSetEnvironmentVariableW = kernel32.NewProc("SetEnvironmentVariableW")
)
// lpName (LPCWSTR), lpValue (LPCWSTR optional)
r1, _, err := procSetEnvironmentVariableW.Call(
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpName))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpValue))),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // BOOLfunction SetEnvironmentVariableW(
lpName: PWideChar; // LPCWSTR
lpValue: PWideChar // LPCWSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetEnvironmentVariableW';result := DllCall("KERNEL32\SetEnvironmentVariableW"
, "WStr", lpName ; LPCWSTR
, "WStr", lpValue ; LPCWSTR optional
, "Int") ; return: BOOL●SetEnvironmentVariableW(lpName, lpValue) = DLL("KERNEL32.dll", "bool SetEnvironmentVariableW(char*, char*)")
# 呼び出し: SetEnvironmentVariableW(lpName, lpValue)
# lpName : LPCWSTR -> "char*"
# lpValue : LPCWSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。