SHRegSetPathW
関数ファイルパスを環境変数化してレジストリ値に書き込む。
シグネチャ
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR SHRegSetPathW(
HKEY hKey,
LPCWSTR pcszSubKey, // optional
LPCWSTR pcszValue, // optional
LPCWSTR pcszPath,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 |
|---|---|---|
| hKey | HKEY | in |
| pcszSubKey | LPCWSTR | inoptional |
| pcszValue | LPCWSTR | inoptional |
| pcszPath | LPCWSTR | in |
| dwFlags | DWORD | in |
戻り値の型: WIN32_ERROR
各言語での呼び出し定義
// SHLWAPI.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR SHRegSetPathW(
HKEY hKey,
LPCWSTR pcszSubKey, // optional
LPCWSTR pcszValue, // optional
LPCWSTR pcszPath,
DWORD dwFlags
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint SHRegSetPathW(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPWStr)] string pcszSubKey, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string pcszValue, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string pcszPath, // LPCWSTR
uint dwFlags // DWORD
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SHRegSetPathW(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPWStr)> pcszSubKey As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> pcszValue As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> pcszPath As String, ' LPCWSTR
dwFlags As UInteger ' DWORD
) As UInteger
End Function' hKey : HKEY
' pcszSubKey : LPCWSTR optional
' pcszValue : LPCWSTR optional
' pcszPath : LPCWSTR
' dwFlags : DWORD
Declare PtrSafe Function SHRegSetPathW Lib "shlwapi" ( _
ByVal hKey As LongPtr, _
ByVal pcszSubKey As LongPtr, _
ByVal pcszValue As LongPtr, _
ByVal pcszPath As LongPtr, _
ByVal dwFlags 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
SHRegSetPathW = ctypes.windll.shlwapi.SHRegSetPathW
SHRegSetPathW.restype = wintypes.DWORD
SHRegSetPathW.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCWSTR, # pcszSubKey : LPCWSTR optional
wintypes.LPCWSTR, # pcszValue : LPCWSTR optional
wintypes.LPCWSTR, # pcszPath : LPCWSTR
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
SHRegSetPathW = Fiddle::Function.new(
lib['SHRegSetPathW'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # pcszSubKey : LPCWSTR optional
Fiddle::TYPE_VOIDP, # pcszValue : LPCWSTR optional
Fiddle::TYPE_VOIDP, # pcszPath : LPCWSTR
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "shlwapi")]
extern "system" {
fn SHRegSetPathW(
hKey: *mut core::ffi::c_void, // HKEY
pcszSubKey: *const u16, // LPCWSTR optional
pcszValue: *const u16, // LPCWSTR optional
pcszPath: *const u16, // LPCWSTR
dwFlags: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode)]
public static extern uint SHRegSetPathW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string pcszSubKey, [MarshalAs(UnmanagedType.LPWStr)] string pcszValue, [MarshalAs(UnmanagedType.LPWStr)] string pcszPath, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_SHRegSetPathW' -Namespace Win32 -PassThru
# $api::SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)#uselib "SHLWAPI.dll"
#func global SHRegSetPathW "SHRegSetPathW" wptr, wptr, wptr, wptr, wptr
; SHRegSetPathW hKey, pcszSubKey, pcszValue, pcszPath, dwFlags ; 戻り値は stat
; hKey : HKEY -> "wptr"
; pcszSubKey : LPCWSTR optional -> "wptr"
; pcszValue : LPCWSTR optional -> "wptr"
; pcszPath : LPCWSTR -> "wptr"
; dwFlags : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHLWAPI.dll"
#cfunc global SHRegSetPathW "SHRegSetPathW" sptr, wstr, wstr, wstr, int
; res = SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
; hKey : HKEY -> "sptr"
; pcszSubKey : LPCWSTR optional -> "wstr"
; pcszValue : LPCWSTR optional -> "wstr"
; pcszPath : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"; WIN32_ERROR SHRegSetPathW(HKEY hKey, LPCWSTR pcszSubKey, LPCWSTR pcszValue, LPCWSTR pcszPath, DWORD dwFlags)
#uselib "SHLWAPI.dll"
#cfunc global SHRegSetPathW "SHRegSetPathW" intptr, wstr, wstr, wstr, int
; res = SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
; hKey : HKEY -> "intptr"
; pcszSubKey : LPCWSTR optional -> "wstr"
; pcszValue : LPCWSTR optional -> "wstr"
; pcszPath : LPCWSTR -> "wstr"
; dwFlags : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procSHRegSetPathW = shlwapi.NewProc("SHRegSetPathW")
)
// hKey (HKEY), pcszSubKey (LPCWSTR optional), pcszValue (LPCWSTR optional), pcszPath (LPCWSTR), dwFlags (DWORD)
r1, _, err := procSHRegSetPathW.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pcszSubKey))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pcszValue))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pcszPath))),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction SHRegSetPathW(
hKey: THandle; // HKEY
pcszSubKey: PWideChar; // LPCWSTR optional
pcszValue: PWideChar; // LPCWSTR optional
pcszPath: PWideChar; // LPCWSTR
dwFlags: DWORD // DWORD
): DWORD; stdcall;
external 'SHLWAPI.dll' name 'SHRegSetPathW';result := DllCall("SHLWAPI\SHRegSetPathW"
, "Ptr", hKey ; HKEY
, "WStr", pcszSubKey ; LPCWSTR optional
, "WStr", pcszValue ; LPCWSTR optional
, "WStr", pcszPath ; LPCWSTR
, "UInt", dwFlags ; DWORD
, "UInt") ; return: WIN32_ERROR●SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags) = DLL("SHLWAPI.dll", "dword SHRegSetPathW(void*, char*, char*, char*, dword)")
# 呼び出し: SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
# hKey : HKEY -> "void*"
# pcszSubKey : LPCWSTR optional -> "char*"
# pcszValue : LPCWSTR optional -> "char*"
# pcszPath : LPCWSTR -> "char*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。