SHRegSetPathA
関数ファイルパスを環境変数化してレジストリ値に書き込む。
シグネチャ
// SHLWAPI.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR SHRegSetPathA(
HKEY hKey,
LPCSTR pcszSubKey, // optional
LPCSTR pcszValue, // optional
LPCSTR pcszPath,
DWORD dwFlags
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | 現在開いているキー、またはレジストリのルートキーへのハンドル。 |
| pcszSubKey | LPCSTR | inoptional | 既存のサブキーの名前を含む、null で終わる文字列へのポインター。サブキーが存在しない場合、SHRegSetPath は失敗します。 |
| pcszValue | LPCSTR | inoptional | パス文字列を保持する値の名前を含む、null で終わる文字列へのポインター。 |
| pcszPath | LPCSTR | in | 完全修飾ファイルパスを含む、null で終わる文字列へのポインター。 |
| dwFlags | DWORD | in | 予約済み。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
ファイルパスを受け取り、フォルダー名を環境文字列に置き換えて、その結果の文字列をレジストリに格納します。(ANSI)
戻り値
型: 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 (ANSI / -A)
#include <windows.h>
WIN32_ERROR SHRegSetPathA(
HKEY hKey,
LPCSTR pcszSubKey, // optional
LPCSTR pcszValue, // optional
LPCSTR pcszPath,
DWORD dwFlags
);[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint SHRegSetPathA(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPStr)] string pcszSubKey, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] string pcszValue, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] string pcszPath, // LPCSTR
uint dwFlags // DWORD
);<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function SHRegSetPathA(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPStr)> pcszSubKey As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> pcszValue As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> pcszPath As String, ' LPCSTR
dwFlags As UInteger ' DWORD
) As UInteger
End Function' hKey : HKEY
' pcszSubKey : LPCSTR optional
' pcszValue : LPCSTR optional
' pcszPath : LPCSTR
' dwFlags : DWORD
Declare PtrSafe Function SHRegSetPathA Lib "shlwapi" ( _
ByVal hKey As LongPtr, _
ByVal pcszSubKey As String, _
ByVal pcszValue As String, _
ByVal pcszPath As String, _
ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
SHRegSetPathA = ctypes.windll.shlwapi.SHRegSetPathA
SHRegSetPathA.restype = wintypes.DWORD
SHRegSetPathA.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCSTR, # pcszSubKey : LPCSTR optional
wintypes.LPCSTR, # pcszValue : LPCSTR optional
wintypes.LPCSTR, # pcszPath : LPCSTR
wintypes.DWORD, # dwFlags : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('SHLWAPI.dll')
SHRegSetPathA = Fiddle::Function.new(
lib['SHRegSetPathA'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # pcszSubKey : LPCSTR optional
Fiddle::TYPE_VOIDP, # pcszValue : LPCSTR optional
Fiddle::TYPE_VOIDP, # pcszPath : LPCSTR
-Fiddle::TYPE_INT, # dwFlags : DWORD
],
-Fiddle::TYPE_INT)#[link(name = "shlwapi")]
extern "system" {
fn SHRegSetPathA(
hKey: *mut core::ffi::c_void, // HKEY
pcszSubKey: *const u8, // LPCSTR optional
pcszValue: *const u8, // LPCSTR optional
pcszPath: *const u8, // LPCSTR
dwFlags: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi)]
public static extern uint SHRegSetPathA(IntPtr hKey, [MarshalAs(UnmanagedType.LPStr)] string pcszSubKey, [MarshalAs(UnmanagedType.LPStr)] string pcszValue, [MarshalAs(UnmanagedType.LPStr)] string pcszPath, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_SHRegSetPathA' -Namespace Win32 -PassThru
# $api::SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)#uselib "SHLWAPI.dll"
#func global SHRegSetPathA "SHRegSetPathA" sptr, sptr, sptr, sptr, sptr
; SHRegSetPathA hKey, pcszSubKey, pcszValue, pcszPath, dwFlags ; 戻り値は stat
; hKey : HKEY -> "sptr"
; pcszSubKey : LPCSTR optional -> "sptr"
; pcszValue : LPCSTR optional -> "sptr"
; pcszPath : LPCSTR -> "sptr"
; dwFlags : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "SHLWAPI.dll"
#cfunc global SHRegSetPathA "SHRegSetPathA" sptr, str, str, str, int
; res = SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
; hKey : HKEY -> "sptr"
; pcszSubKey : LPCSTR optional -> "str"
; pcszValue : LPCSTR optional -> "str"
; pcszPath : LPCSTR -> "str"
; dwFlags : DWORD -> "int"; WIN32_ERROR SHRegSetPathA(HKEY hKey, LPCSTR pcszSubKey, LPCSTR pcszValue, LPCSTR pcszPath, DWORD dwFlags)
#uselib "SHLWAPI.dll"
#cfunc global SHRegSetPathA "SHRegSetPathA" intptr, str, str, str, int
; res = SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
; hKey : HKEY -> "intptr"
; pcszSubKey : LPCSTR optional -> "str"
; pcszValue : LPCSTR optional -> "str"
; pcszPath : LPCSTR -> "str"
; dwFlags : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
procSHRegSetPathA = shlwapi.NewProc("SHRegSetPathA")
)
// hKey (HKEY), pcszSubKey (LPCSTR optional), pcszValue (LPCSTR optional), pcszPath (LPCSTR), dwFlags (DWORD)
r1, _, err := procSHRegSetPathA.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pcszSubKey))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pcszValue))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(pcszPath))),
uintptr(dwFlags),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction SHRegSetPathA(
hKey: THandle; // HKEY
pcszSubKey: PAnsiChar; // LPCSTR optional
pcszValue: PAnsiChar; // LPCSTR optional
pcszPath: PAnsiChar; // LPCSTR
dwFlags: DWORD // DWORD
): DWORD; stdcall;
external 'SHLWAPI.dll' name 'SHRegSetPathA';result := DllCall("SHLWAPI\SHRegSetPathA"
, "Ptr", hKey ; HKEY
, "AStr", pcszSubKey ; LPCSTR optional
, "AStr", pcszValue ; LPCSTR optional
, "AStr", pcszPath ; LPCSTR
, "UInt", dwFlags ; DWORD
, "UInt") ; return: WIN32_ERROR●SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags) = DLL("SHLWAPI.dll", "dword SHRegSetPathA(void*, char*, char*, char*, dword)")
# 呼び出し: SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
# hKey : HKEY -> "void*"
# pcszSubKey : LPCSTR optional -> "char*"
# pcszValue : LPCSTR optional -> "char*"
# pcszPath : LPCSTR -> "char*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "shlwapi" fn SHRegSetPathA(
hKey: ?*anyopaque, // HKEY
pcszSubKey: [*c]const u8, // LPCSTR optional
pcszValue: [*c]const u8, // LPCSTR optional
pcszPath: [*c]const u8, // LPCSTR
dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;proc SHRegSetPathA(
hKey: pointer, # HKEY
pcszSubKey: cstring, # LPCSTR optional
pcszValue: cstring, # LPCSTR optional
pcszPath: cstring, # LPCSTR
dwFlags: uint32 # DWORD
): uint32 {.importc: "SHRegSetPathA", stdcall, dynlib: "SHLWAPI.dll".}pragma(lib, "shlwapi");
extern(Windows)
uint SHRegSetPathA(
void* hKey, // HKEY
const(char)* pcszSubKey, // LPCSTR optional
const(char)* pcszValue, // LPCSTR optional
const(char)* pcszPath, // LPCSTR
uint dwFlags // DWORD
);ccall((:SHRegSetPathA, "SHLWAPI.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cstring, Cstring, Cstring, UInt32),
hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
# hKey : HKEY -> Ptr{Cvoid}
# pcszSubKey : LPCSTR optional -> Cstring
# pcszValue : LPCSTR optional -> Cstring
# pcszPath : LPCSTR -> Cstring
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t SHRegSetPathA(
void* hKey,
const char* pcszSubKey,
const char* pcszValue,
const char* pcszPath,
uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
-- hKey : HKEY
-- pcszSubKey : LPCSTR optional
-- pcszValue : LPCSTR optional
-- pcszPath : LPCSTR
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const SHRegSetPathA = lib.func('__stdcall', 'SHRegSetPathA', 'uint32_t', ['void *', 'str', 'str', 'str', 'uint32_t']);
// SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
// hKey : HKEY -> 'void *'
// pcszSubKey : LPCSTR optional -> 'str'
// pcszValue : LPCSTR optional -> 'str'
// pcszPath : LPCSTR -> 'str'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("SHLWAPI.dll", {
SHRegSetPathA: { parameters: ["pointer", "buffer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags)
// hKey : HKEY -> "pointer"
// pcszSubKey : LPCSTR optional -> "buffer"
// pcszValue : LPCSTR optional -> "buffer"
// pcszPath : LPCSTR -> "buffer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t SHRegSetPathA(
void* hKey,
const char* pcszSubKey,
const char* pcszValue,
const char* pcszPath,
uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->SHRegSetPathA(hKey, pcszSubKey, pcszValue, pcszPath, dwFlags);
// hKey : HKEY
// pcszSubKey : LPCSTR optional
// pcszValue : LPCSTR optional
// pcszPath : LPCSTR
// 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.ASCII_OPTIONS);
int SHRegSetPathA(
Pointer hKey, // HKEY
String pcszSubKey, // LPCSTR optional
String pcszValue, // LPCSTR optional
String pcszPath, // LPCSTR
int dwFlags // DWORD
);
}@[Link("shlwapi")]
lib LibSHLWAPI
fun SHRegSetPathA = SHRegSetPathA(
hKey : Void*, # HKEY
pcszSubKey : UInt8*, # LPCSTR optional
pcszValue : UInt8*, # LPCSTR optional
pcszPath : UInt8*, # LPCSTR
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 SHRegSetPathANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Uint32);
typedef SHRegSetPathADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, int);
final SHRegSetPathA = DynamicLibrary.open('SHLWAPI.dll')
.lookupFunction<SHRegSetPathANative, SHRegSetPathADart>('SHRegSetPathA');
// hKey : HKEY -> Pointer<Void>
// pcszSubKey : LPCSTR optional -> Pointer<Utf8>
// pcszValue : LPCSTR optional -> Pointer<Utf8>
// pcszPath : LPCSTR -> Pointer<Utf8>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function SHRegSetPathA(
hKey: THandle; // HKEY
pcszSubKey: PAnsiChar; // LPCSTR optional
pcszValue: PAnsiChar; // LPCSTR optional
pcszPath: PAnsiChar; // LPCSTR
dwFlags: DWORD // DWORD
): DWORD; stdcall;
external 'SHLWAPI.dll' name 'SHRegSetPathA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "SHRegSetPathA"
c_SHRegSetPathA :: Ptr () -> CString -> CString -> CString -> Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- pcszSubKey : LPCSTR optional -> CString
-- pcszValue : LPCSTR optional -> CString
-- pcszPath : LPCSTR -> CString
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let shregsetpatha =
foreign "SHRegSetPathA"
((ptr void) @-> string @-> string @-> string @-> uint32_t @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* pcszSubKey : LPCSTR optional -> string *)
(* pcszValue : LPCSTR optional -> string *)
(* pcszPath : LPCSTR -> string *)
(* 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 ("SHRegSetPathA" shreg-set-path-a :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(pcsz-sub-key :string) ; LPCSTR optional
(pcsz-value :string) ; LPCSTR optional
(pcsz-path :string) ; LPCSTR
(dw-flags :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $SHRegSetPathA = Win32::API::More->new('SHLWAPI',
'DWORD SHRegSetPathA(HANDLE hKey, LPCSTR pcszSubKey, LPCSTR pcszValue, LPCSTR pcszPath, DWORD dwFlags)');
# my $ret = $SHRegSetPathA->Call($hKey, $pcszSubKey, $pcszValue, $pcszPath, $dwFlags);
# hKey : HKEY -> HANDLE
# pcszSubKey : LPCSTR optional -> LPCSTR
# pcszValue : LPCSTR optional -> LPCSTR
# pcszPath : LPCSTR -> LPCSTR
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
文字セット違い
- f SHRegSetPathW (Unicode版) — ファイルパスを環境変数化してレジストリ値に書き込む。
使用する型