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 | 既存のサブキーの名前を含む、null で終わる文字列へのポインター。サブキーが存在しない場合、SHRegSetPath は失敗します。 |
| pcszValue | LPCWSTR | inoptional | パス文字列を保持する値の名前を含む、null で終わる文字列へのポインター。 |
| pcszPath | LPCWSTR | in | 完全修飾されたファイルパスを含む、null で終わる文字列へのポインター。 |
| dwFlags | DWORD | in | 予約済みです。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
ファイルパスを受け取り、フォルダー名を環境文字列に置き換えて、結果の文字列をレジストリに格納します。(Unicode)
戻り値
型: LSTATUS
成功した場合は ERROR_SUCCESS を返し、それ以外の場合は Windows エラーコードを返します。
解説(Remarks)
Windows 2000 では、SHRegSetPath は PathUnExpandEnvStrings を使用してフォルダー名を対応する環境文字列に変換します。いずれかの環境変数が置換された場合、レジストリ値は REG_EXPAND_SZ データ型で設定されます。そうでない場合は、REG_SZ データ型で設定されます。
以下のフォルダーパスは、それぞれ同等の環境文字列に置き換えられます。
| フォルダー | 環境文字列 |
|---|---|
| 現在のユーザーのプロファイルフォルダー | %USERPROFILE% |
| All Users プロファイルフォルダー | %ALLUSERSPROFILE% |
| Program Files フォルダー | %ProgramFiles% |
| システムルートフォルダー | %SystemRoot% |
| システムドライブのドライブ文字 | %SystemDrive% |
注意 %USERPROFILE% は呼び出しを行うユーザーに対して相対的です。サービスからユーザーが偽装されている場合、この関数は機能しません。
メモ
shlwapi.h ヘッダーは SHRegSetPath をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング中立のエイリアスの使用を、エンコーディング中立でないコードと混在させると、コンパイルエラーや実行時エラーを引き起こす不一致が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// 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 版の利用を推奨。const std = @import("std");
extern "shlwapi" fn SHRegSetPathW(
hKey: ?*anyopaque, // HKEY
pcszSubKey: [*c]const u16, // LPCWSTR optional
pcszValue: [*c]const u16, // LPCWSTR optional
pcszPath: [*c]const u16, // LPCWSTR
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc SHRegSetPathW(
hKey: pointer, # HKEY
pcszSubKey: WideCString, # LPCWSTR optional
pcszValue: WideCString, # LPCWSTR optional
pcszPath: WideCString, # LPCWSTR
dwFlags: uint32 # DWORD
): uint32 {.importc: "SHRegSetPathW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "shlwapi");
extern(Windows)
uint SHRegSetPathW(
void* hKey, // HKEY
const(wchar)* pcszSubKey, // LPCWSTR optional
const(wchar)* pcszValue, // LPCWSTR optional
const(wchar)* pcszPath, // LPCWSTR
uint dwFlags // DWORD
);ccall((:SHRegSetPathW, "SHLWAPI.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cwstring, Cwstring, Cwstring, UInt32),
hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
# hKey : HKEY -> Ptr{Cvoid}
# pcszSubKey : LPCWSTR optional -> Cwstring
# pcszValue : LPCWSTR optional -> Cwstring
# pcszPath : LPCWSTR -> Cwstring
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t SHRegSetPathW(
void* hKey,
const uint16_t* pcszSubKey,
const uint16_t* pcszValue,
const uint16_t* pcszPath,
uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
-- hKey : HKEY
-- pcszSubKey : LPCWSTR optional
-- pcszValue : LPCWSTR optional
-- pcszPath : LPCWSTR
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const SHRegSetPathW = lib.func('__stdcall', 'SHRegSetPathW', 'uint32_t', ['void *', 'str16', 'str16', 'str16', 'uint32_t']);
// SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
// hKey : HKEY -> 'void *'
// pcszSubKey : LPCWSTR optional -> 'str16'
// pcszValue : LPCWSTR optional -> 'str16'
// pcszPath : LPCWSTR -> 'str16'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
SHRegSetPathW: { parameters: ["pointer", "buffer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
// hKey : HKEY -> "pointer"
// pcszSubKey : LPCWSTR optional -> "buffer"
// pcszValue : LPCWSTR optional -> "buffer"
// pcszPath : LPCWSTR -> "buffer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t SHRegSetPathW(
void* hKey,
const uint16_t* pcszSubKey,
const uint16_t* pcszValue,
const uint16_t* pcszPath,
uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->SHRegSetPathW(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags);
// hKey : HKEY
// pcszSubKey : LPCWSTR optional
// pcszValue : LPCWSTR optional
// pcszPath : LPCWSTR
// dwFlags : DWORD
// 構造体/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 Shlwapi extends StdCallLibrary {
Shlwapi INSTANCE = Native.load("shlwapi", Shlwapi.class, W32APIOptions.UNICODE_OPTIONS);
int SHRegSetPathW(
Pointer hKey, // HKEY
WString pcszSubKey, // LPCWSTR optional
WString pcszValue, // LPCWSTR optional
WString pcszPath, // LPCWSTR
int dwFlags // DWORD
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("shlwapi")]
lib LibSHLWAPI
fun SHRegSetPathW = SHRegSetPathW(
hKey : Void*, # HKEY
pcszSubKey : UInt16*, # LPCWSTR optional
pcszValue : UInt16*, # LPCWSTR optional
pcszPath : UInt16*, # LPCWSTR
dwFlags : UInt32 # DWORD
) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。import 'dart:ffi';
import 'package:ffi/ffi.dart';
typedef SHRegSetPathWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef SHRegSetPathWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int);
final SHRegSetPathW = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<SHRegSetPathWNative, SHRegSetPathWDart>('SHRegSetPathW');
// hKey : HKEY -> Pointer<Void>
// pcszSubKey : LPCWSTR optional -> Pointer<Utf16>
// pcszValue : LPCWSTR optional -> Pointer<Utf16>
// pcszPath : LPCWSTR -> Pointer<Utf16>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function 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';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SHRegSetPathW"
c_SHRegSetPathW :: Ptr () -> CWString -> CWString -> CWString -> Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- pcszSubKey : LPCWSTR optional -> CWString
-- pcszValue : LPCWSTR optional -> CWString
-- pcszPath : LPCWSTR -> CWString
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let shregsetpathw =
foreign "SHRegSetPathW"
((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* pcszSubKey : LPCWSTR optional -> (ptr uint16_t) *)
(* pcszValue : LPCWSTR optional -> (ptr uint16_t) *)
(* pcszPath : LPCWSTR -> (ptr uint16_t) *)
(* dwFlags : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library shlwapi (t "SHLWAPI.dll"))
(cffi:use-foreign-library shlwapi)
(cffi:defcfun ("SHRegSetPathW" shreg-set-path-w :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(pcsz-sub-key (:string :encoding :utf-16le)) ; LPCWSTR optional
(pcsz-value (:string :encoding :utf-16le)) ; LPCWSTR optional
(pcsz-path (:string :encoding :utf-16le)) ; LPCWSTR
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SHRegSetPathW = Win32::API::More->new('SHLWAPI',
'DWORD SHRegSetPathW(HANDLE hKey, LPCWSTR pcszSubKey, LPCWSTR pcszValue, LPCWSTR pcszPath, DWORD dwFlags)');
# my $ret = $SHRegSetPathW->Call($hKey, $pcszSubKey, $pcszValue, $pcszPath, $dwFlags);
# hKey : HKEY -> HANDLE
# pcszSubKey : LPCWSTR optional -> LPCWSTR
# pcszValue : LPCWSTR optional -> LPCWSTR
# pcszPath : LPCWSTR -> LPCWSTR
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
文字セット違い
- f SHRegSetPathA (ANSI版) — ファイルパスを環境変数化してレジストリ値に書き込む。
使用する型