ホーム › System.Environment › SetEnvironmentVariableA
SetEnvironmentVariableA
関数指定した環境変数の値を設定または削除する(ANSI版)。
シグネチャ
// KERNEL32.dll (ANSI / -A)
#include <windows.h>
BOOL SetEnvironmentVariableA(
LPCSTR lpName,
LPCSTR lpValue // optional
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| lpName | LPCSTR | in | 設定する環境変数名を指すANSI文字列。 |
| lpValue | LPCSTR | inoptional | 設定する値を指すANSI文字列。NULLを指定すると変数を削除する。 |
戻り値の型: 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 <MarshalAs(UnmanagedType.Bool)> 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 // BOOLfunction 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)。const std = @import("std");
extern "kernel32" fn SetEnvironmentVariableA(
lpName: [*c]const u8, // LPCSTR
lpValue: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) i32;proc SetEnvironmentVariableA(
lpName: cstring, # LPCSTR
lpValue: cstring # LPCSTR optional
): int32 {.importc: "SetEnvironmentVariableA", stdcall, dynlib: "KERNEL32.dll".}pragma(lib, "kernel32");
extern(Windows)
int SetEnvironmentVariableA(
const(char)* lpName, // LPCSTR
const(char)* lpValue // LPCSTR optional
);ccall((:SetEnvironmentVariableA, "KERNEL32.dll"), stdcall, Int32,
(Cstring, Cstring),
lpName, lpValue)
# lpName : LPCSTR -> Cstring
# lpValue : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
int32_t SetEnvironmentVariableA(
const char* lpName,
const char* lpValue);
]]
local kernel32 = ffi.load("kernel32")
-- kernel32.SetEnvironmentVariableA(lpName, lpValue)
-- lpName : LPCSTR
-- lpValue : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('KERNEL32.dll');
const SetEnvironmentVariableA = lib.func('__stdcall', 'SetEnvironmentVariableA', 'int32_t', ['str', 'str']);
// SetEnvironmentVariableA(lpName, lpValue)
// lpName : LPCSTR -> 'str'
// lpValue : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("KERNEL32.dll", {
SetEnvironmentVariableA: { parameters: ["buffer", "buffer"], result: "i32" },
});
// lib.symbols.SetEnvironmentVariableA(lpName, lpValue)
// lpName : LPCSTR -> "buffer"
// lpValue : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
int32_t SetEnvironmentVariableA(
const char* lpName,
const char* lpValue);
C, "KERNEL32.dll");
// $ffi->SetEnvironmentVariableA(lpName, lpValue);
// lpName : LPCSTR
// lpValue : LPCSTR optional
// 構造体/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);
boolean SetEnvironmentVariableA(
String lpName, // LPCSTR
String lpValue // LPCSTR optional
);
}@[Link("kernel32")]
lib LibKERNEL32
fun SetEnvironmentVariableA = SetEnvironmentVariableA(
lpName : UInt8*, # LPCSTR
lpValue : UInt8* # LPCSTR optional
) : Int32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SetEnvironmentVariableANative = Int32 Function(Pointer<Utf8>, Pointer<Utf8>);
typedef SetEnvironmentVariableADart = int Function(Pointer<Utf8>, Pointer<Utf8>);
final SetEnvironmentVariableA = DynamicLibrary.open('KERNEL32.dll')
.lookupFunction<SetEnvironmentVariableANative, SetEnvironmentVariableADart>('SetEnvironmentVariableA');
// lpName : LPCSTR -> Pointer<Utf8>
// lpValue : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SetEnvironmentVariableA(
lpName: PAnsiChar; // LPCSTR
lpValue: PAnsiChar // LPCSTR optional
): BOOL; stdcall;
external 'KERNEL32.dll' name 'SetEnvironmentVariableA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SetEnvironmentVariableA"
c_SetEnvironmentVariableA :: CString -> CString -> IO CInt
-- lpName : LPCSTR -> CString
-- lpValue : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let setenvironmentvariablea =
foreign "SetEnvironmentVariableA"
(string @-> string @-> returning int32_t)
(* lpName : LPCSTR -> string *)
(* lpValue : LPCSTR optional -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library kernel32 (t "KERNEL32.dll"))
(cffi:use-foreign-library kernel32)
(cffi:defcfun ("SetEnvironmentVariableA" set-environment-variable-a :convention :stdcall) :int32
(lp-name :string) ; LPCSTR
(lp-value :string)) ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SetEnvironmentVariableA = Win32::API::More->new('KERNEL32',
'BOOL SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue)');
# my $ret = $SetEnvironmentVariableA->Call($lpName, $lpValue);
# lpName : LPCSTR -> LPCSTR
# lpValue : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f SetEnvironmentVariableW (Unicode版) — 指定した環境変数の値を設定または削除する(Unicode版)。