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

SetEnvironmentVariableA

関数
指定した環境変数の値を設定または削除する(ANSI版)。
DLLKERNEL32.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

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

BOOL SetEnvironmentVariableA(
    LPCSTR lpName,
    LPCSTR lpValue   // optional
);

パラメーター

名前方向
lpNameLPCSTRin
lpValueLPCSTRinoptional

戻り値の型: BOOL

各言語での呼び出し定義

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

BOOL SetEnvironmentVariableA(
    LPCSTR lpName,
    LPCSTR lpValue   // optional
);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern bool SetEnvironmentVariableA(
    [MarshalAs(UnmanagedType.LPStr)] string lpName,   // LPCSTR
    [MarshalAs(UnmanagedType.LPStr)] string lpValue   // LPCSTR optional
);
<DllImport("KERNEL32.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetEnvironmentVariableA(
    <MarshalAs(UnmanagedType.LPStr)> lpName As String,   ' LPCSTR
    <MarshalAs(UnmanagedType.LPStr)> lpValue As String   ' LPCSTR optional
) As Boolean
End Function
' lpName : LPCSTR
' lpValue : LPCSTR optional
Declare PtrSafe Function SetEnvironmentVariableA Lib "kernel32" ( _
    ByVal lpName As String, _
    ByVal lpValue As String) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetEnvironmentVariableA = ctypes.windll.kernel32.SetEnvironmentVariableA
SetEnvironmentVariableA.restype = wintypes.BOOL
SetEnvironmentVariableA.argtypes = [
    wintypes.LPCSTR,  # lpName : LPCSTR
    wintypes.LPCSTR,  # lpValue : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('KERNEL32.dll')
SetEnvironmentVariableA = Fiddle::Function.new(
  lib['SetEnvironmentVariableA'],
  [
    Fiddle::TYPE_VOIDP,  # lpName : LPCSTR
    Fiddle::TYPE_VOIDP,  # lpValue : LPCSTR optional
  ],
  Fiddle::TYPE_INT)
#[link(name = "kernel32")]
extern "system" {
    fn SetEnvironmentVariableA(
        lpName: *const u8,  // LPCSTR
        lpValue: *const u8  // LPCSTR optional
    ) -> i32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("KERNEL32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern bool SetEnvironmentVariableA([MarshalAs(UnmanagedType.LPStr)] string lpName, [MarshalAs(UnmanagedType.LPStr)] string lpValue);
"@
$api = Add-Type -MemberDefinition $sig -Name 'KERNEL32_SetEnvironmentVariableA' -Namespace Win32 -PassThru
# $api::SetEnvironmentVariableA(lpName, lpValue)
#uselib "KERNEL32.dll"
#func global SetEnvironmentVariableA "SetEnvironmentVariableA" sptr, sptr
; SetEnvironmentVariableA lpName, lpValue   ; 戻り値は stat
; lpName : LPCSTR -> "sptr"
; lpValue : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "KERNEL32.dll"
#cfunc global SetEnvironmentVariableA "SetEnvironmentVariableA" str, str
; res = SetEnvironmentVariableA(lpName, lpValue)
; lpName : LPCSTR -> "str"
; lpValue : LPCSTR optional -> "str"
; BOOL SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue)
#uselib "KERNEL32.dll"
#cfunc global SetEnvironmentVariableA "SetEnvironmentVariableA" str, str
; res = SetEnvironmentVariableA(lpName, lpValue)
; lpName : LPCSTR -> "str"
; lpValue : LPCSTR optional -> "str"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	kernel32 = windows.NewLazySystemDLL("KERNEL32.dll")
	procSetEnvironmentVariableA = kernel32.NewProc("SetEnvironmentVariableA")
)

// lpName (LPCSTR), lpValue (LPCSTR optional)
r1, _, err := procSetEnvironmentVariableA.Call(
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpName))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(lpValue))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // BOOL
function SetEnvironmentVariableA(
  lpName: PAnsiChar;   // LPCSTR
  lpValue: PAnsiChar   // LPCSTR optional
): BOOL; stdcall;
  external 'KERNEL32.dll' name 'SetEnvironmentVariableA';
result := DllCall("KERNEL32\SetEnvironmentVariableA"
    , "AStr", lpName   ; LPCSTR
    , "AStr", lpValue   ; LPCSTR optional
    , "Int")   ; return: BOOL
●SetEnvironmentVariableA(lpName, lpValue) = DLL("KERNEL32.dll", "bool SetEnvironmentVariableA(char*, char*)")
# 呼び出し: SetEnvironmentVariableA(lpName, lpValue)
# lpName : LPCSTR -> "char*"
# lpValue : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。