Win32 API 日本語リファレンス
ホームUI.Shell › SHRegGetPathW

SHRegGetPathW

関数
レジストリ値からファイルパスを取得し環境変数を展開する。
DLLSHLWAPI.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

// SHLWAPI.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR SHRegGetPathW(
    HKEY hKey,
    LPCWSTR pcszSubKey,   // optional
    LPCWSTR pcszValue,   // optional
    LPWSTR pszPath,
    DWORD dwFlags
);

パラメーター

名前方向説明
hKeyHKEYin現在開かれているキーへのハンドル、またはレジストリのルートキー。
pcszSubKeyLPCWSTRinoptionalサブキーの名前を含む null 終端文字列へのポインター。
pcszValueLPCWSTRinoptional展開されていないパス文字列を保持する値の名前を含む null 終端文字列へのポインター。
pszPathLPWSTRout展開されたパスを保持するバッファー。返される文字列を確実に保持できるよう、このバッファーのサイズは MAX_PATH に設定してください。
dwFlagsDWORDin予約済み。

戻り値の型: WIN32_ERROR

公式ドキュメント

レジストリからファイルパスを取得し、必要に応じて環境変数を展開します。(Unicode)

戻り値

型: LSTATUS

成功した場合は ERROR_SUCCESS を、それ以外の場合は Windows エラーコードを返します。

解説(Remarks)

指定したレジストリ値のデータ型は、REG_EXPAND_SZ または REG_SZ のいずれかである必要があります。REG_EXPAND_SZ 型の場合、レジストリ文字列内のすべての環境変数は ExpandEnvironmentStrings によって展開されます。REG_SZ データ型の場合、環境変数は展開されず、pszPath が指す文字列はレジストリ内の文字列と同一になります。

次の環境文字列は、それぞれに対応するパスに置き換えられます。

環境文字列 フォルダー
%USERPROFILE% 現在のユーザーのプロファイルフォルダー
%ALLUSERSPROFILE% All Users プロファイルフォルダー
%ProgramFiles% Program Files フォルダー
%SystemRoot% システムルートフォルダー
%SystemDrive% システムドライブのドライブ文字
注意 %USERPROFILE% は呼び出しを行うユーザーに対する相対的な値です。サービスからユーザーが偽装されている場合、この関数は機能しません。
メモ

shlwapi.h ヘッダーは SHRegGetPath をエイリアスとして定義しており、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)

各言語での呼び出し定義

// SHLWAPI.dll  (Unicode / -W)
#include <windows.h>

WIN32_ERROR SHRegGetPathW(
    HKEY hKey,
    LPCWSTR pcszSubKey,   // optional
    LPCWSTR pcszValue,   // optional
    LPWSTR pszPath,
    DWORD dwFlags
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint SHRegGetPathW(
    IntPtr hKey,   // HKEY
    [MarshalAs(UnmanagedType.LPWStr)] string pcszSubKey,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] string pcszValue,   // LPCWSTR optional
    [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath,   // LPWSTR out
    uint dwFlags   // DWORD
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function SHRegGetPathW(
    hKey As IntPtr,   ' HKEY
    <MarshalAs(UnmanagedType.LPWStr)> pcszSubKey As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pcszValue As String,   ' LPCWSTR optional
    <MarshalAs(UnmanagedType.LPWStr)> pszPath As System.Text.StringBuilder,   ' LPWSTR out
    dwFlags As UInteger   ' DWORD
) As UInteger
End Function
' hKey : HKEY
' pcszSubKey : LPCWSTR optional
' pcszValue : LPCWSTR optional
' pszPath : LPWSTR out
' dwFlags : DWORD
Declare PtrSafe Function SHRegGetPathW Lib "shlwapi" ( _
    ByVal hKey As LongPtr, _
    ByVal pcszSubKey As LongPtr, _
    ByVal pcszValue As LongPtr, _
    ByVal pszPath 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

SHRegGetPathW = ctypes.windll.shlwapi.SHRegGetPathW
SHRegGetPathW.restype = wintypes.DWORD
SHRegGetPathW.argtypes = [
    wintypes.HANDLE,  # hKey : HKEY
    wintypes.LPCWSTR,  # pcszSubKey : LPCWSTR optional
    wintypes.LPCWSTR,  # pcszValue : LPCWSTR optional
    wintypes.LPWSTR,  # pszPath : LPWSTR out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SHLWAPI.dll')
SHRegGetPathW = Fiddle::Function.new(
  lib['SHRegGetPathW'],
  [
    Fiddle::TYPE_VOIDP,  # hKey : HKEY
    Fiddle::TYPE_VOIDP,  # pcszSubKey : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pcszValue : LPCWSTR optional
    Fiddle::TYPE_VOIDP,  # pszPath : LPWSTR out
    -Fiddle::TYPE_INT,  # dwFlags : DWORD
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "shlwapi")]
extern "system" {
    fn SHRegGetPathW(
        hKey: *mut core::ffi::c_void,  // HKEY
        pcszSubKey: *const u16,  // LPCWSTR optional
        pcszValue: *const u16,  // LPCWSTR optional
        pszPath: *mut u16,  // LPWSTR out
        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 SHRegGetPathW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string pcszSubKey, [MarshalAs(UnmanagedType.LPWStr)] string pcszValue, [MarshalAs(UnmanagedType.LPWStr)] System.Text.StringBuilder pszPath, uint dwFlags);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SHLWAPI_SHRegGetPathW' -Namespace Win32 -PassThru
# $api::SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
#uselib "SHLWAPI.dll"
#func global SHRegGetPathW "SHRegGetPathW" wptr, wptr, wptr, wptr, wptr
; SHRegGetPathW hKey, pcszSubKey, pcszValue, varptr(pszPath), dwFlags   ; 戻り値は stat
; hKey : HKEY -> "wptr"
; pcszSubKey : LPCWSTR optional -> "wptr"
; pcszValue : LPCWSTR optional -> "wptr"
; pszPath : LPWSTR out -> "wptr"
; dwFlags : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SHLWAPI.dll"
#cfunc global SHRegGetPathW "SHRegGetPathW" sptr, wstr, wstr, var, int
; res = SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
; hKey : HKEY -> "sptr"
; pcszSubKey : LPCWSTR optional -> "wstr"
; pcszValue : LPCWSTR optional -> "wstr"
; pszPath : LPWSTR out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; WIN32_ERROR SHRegGetPathW(HKEY hKey, LPCWSTR pcszSubKey, LPCWSTR pcszValue, LPWSTR pszPath, DWORD dwFlags)
#uselib "SHLWAPI.dll"
#cfunc global SHRegGetPathW "SHRegGetPathW" intptr, wstr, wstr, var, int
; res = SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
; hKey : HKEY -> "intptr"
; pcszSubKey : LPCWSTR optional -> "wstr"
; pcszValue : LPCWSTR optional -> "wstr"
; pszPath : LPWSTR out -> "var"
; dwFlags : DWORD -> "int"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHRegGetPathW = shlwapi.NewProc("SHRegGetPathW")
)

// hKey (HKEY), pcszSubKey (LPCWSTR optional), pcszValue (LPCWSTR optional), pszPath (LPWSTR out), dwFlags (DWORD)
r1, _, err := procSHRegGetPathW.Call(
	uintptr(hKey),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pcszSubKey))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(pcszValue))),
	uintptr(pszPath),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function SHRegGetPathW(
  hKey: THandle;   // HKEY
  pcszSubKey: PWideChar;   // LPCWSTR optional
  pcszValue: PWideChar;   // LPCWSTR optional
  pszPath: PWideChar;   // LPWSTR out
  dwFlags: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHLWAPI.dll' name 'SHRegGetPathW';
result := DllCall("SHLWAPI\SHRegGetPathW"
    , "Ptr", hKey   ; HKEY
    , "WStr", pcszSubKey   ; LPCWSTR optional
    , "WStr", pcszValue   ; LPCWSTR optional
    , "Ptr", pszPath   ; LPWSTR out
    , "UInt", dwFlags   ; DWORD
    , "UInt")   ; return: WIN32_ERROR
●SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags) = DLL("SHLWAPI.dll", "dword SHRegGetPathW(void*, char*, char*, char*, dword)")
# 呼び出し: SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
# hKey : HKEY -> "void*"
# pcszSubKey : LPCWSTR optional -> "char*"
# pcszValue : LPCWSTR optional -> "char*"
# pszPath : LPWSTR out -> "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 SHRegGetPathW(
    hKey: ?*anyopaque, // HKEY
    pcszSubKey: [*c]const u16, // LPCWSTR optional
    pcszValue: [*c]const u16, // LPCWSTR optional
    pszPath: [*c]u16, // LPWSTR out
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SHRegGetPathW(
    hKey: pointer,  # HKEY
    pcszSubKey: WideCString,  # LPCWSTR optional
    pcszValue: WideCString,  # LPCWSTR optional
    pszPath: ptr uint16,  # LPWSTR out
    dwFlags: uint32  # DWORD
): uint32 {.importc: "SHRegGetPathW", stdcall, dynlib: "SHLWAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "shlwapi");
extern(Windows)
uint SHRegGetPathW(
    void* hKey,   // HKEY
    const(wchar)* pcszSubKey,   // LPCWSTR optional
    const(wchar)* pcszValue,   // LPCWSTR optional
    wchar* pszPath,   // LPWSTR out
    uint dwFlags   // DWORD
);
ccall((:SHRegGetPathW, "SHLWAPI.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Cwstring, Cwstring, Ptr{UInt16}, UInt32),
      hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
# hKey : HKEY -> Ptr{Cvoid}
# pcszSubKey : LPCWSTR optional -> Cwstring
# pcszValue : LPCWSTR optional -> Cwstring
# pszPath : LPWSTR out -> Ptr{UInt16}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t SHRegGetPathW(
    void* hKey,
    const uint16_t* pcszSubKey,
    const uint16_t* pcszValue,
    uint16_t* pszPath,
    uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
-- hKey : HKEY
-- pcszSubKey : LPCWSTR optional
-- pcszValue : LPCWSTR optional
-- pszPath : LPWSTR out
-- 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 SHRegGetPathW = lib.func('__stdcall', 'SHRegGetPathW', 'uint32_t', ['void *', 'str16', 'str16', 'uint16_t *', 'uint32_t']);
// SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
// hKey : HKEY -> 'void *'
// pcszSubKey : LPCWSTR optional -> 'str16'
// pcszValue : LPCWSTR optional -> 'str16'
// pszPath : LPWSTR out -> 'uint16_t *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  SHRegGetPathW: { parameters: ["pointer", "buffer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
// hKey : HKEY -> "pointer"
// pcszSubKey : LPCWSTR optional -> "buffer"
// pcszValue : LPCWSTR optional -> "buffer"
// pszPath : LPWSTR out -> "buffer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t SHRegGetPathW(
    void* hKey,
    const uint16_t* pcszSubKey,
    const uint16_t* pcszValue,
    uint16_t* pszPath,
    uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->SHRegGetPathW(hKey, pcszSubKey, pcszValue, pszPath, dwFlags);
// hKey : HKEY
// pcszSubKey : LPCWSTR optional
// pcszValue : LPCWSTR optional
// pszPath : LPWSTR out
// 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 SHRegGetPathW(
        Pointer hKey,   // HKEY
        WString pcszSubKey,   // LPCWSTR optional
        WString pcszValue,   // LPCWSTR optional
        char[] pszPath,   // LPWSTR out
        int dwFlags   // DWORD
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("shlwapi")]
lib LibSHLWAPI
  fun SHRegGetPathW = SHRegGetPathW(
    hKey : Void*,   # HKEY
    pcszSubKey : UInt16*,   # LPCWSTR optional
    pcszValue : UInt16*,   # LPCWSTR optional
    pszPath : UInt16*,   # LPWSTR out
    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 SHRegGetPathWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, Uint32);
typedef SHRegGetPathWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Pointer<Utf16>, int);
final SHRegGetPathW = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<SHRegGetPathWNative, SHRegGetPathWDart>('SHRegGetPathW');
// hKey : HKEY -> Pointer<Void>
// pcszSubKey : LPCWSTR optional -> Pointer<Utf16>
// pcszValue : LPCWSTR optional -> Pointer<Utf16>
// pszPath : LPWSTR out -> Pointer<Utf16>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHRegGetPathW(
  hKey: THandle;   // HKEY
  pcszSubKey: PWideChar;   // LPCWSTR optional
  pcszValue: PWideChar;   // LPCWSTR optional
  pszPath: PWideChar;   // LPWSTR out
  dwFlags: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHLWAPI.dll' name 'SHRegGetPathW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SHRegGetPathW"
  c_SHRegGetPathW :: Ptr () -> CWString -> CWString -> CWString -> Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- pcszSubKey : LPCWSTR optional -> CWString
-- pcszValue : LPCWSTR optional -> CWString
-- pszPath : LPWSTR out -> CWString
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shreggetpathw =
  foreign "SHRegGetPathW"
    ((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) *)
(* pszPath : LPWSTR out -> (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 ("SHRegGetPathW" shreg-get-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
  (psz-path :pointer)   ; LPWSTR out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHRegGetPathW = Win32::API::More->new('SHLWAPI',
    'DWORD SHRegGetPathW(HANDLE hKey, LPCWSTR pcszSubKey, LPCWSTR pcszValue, LPWSTR pszPath, DWORD dwFlags)');
# my $ret = $SHRegGetPathW->Call($hKey, $pcszSubKey, $pcszValue, $pszPath, $dwFlags);
# hKey : HKEY -> HANDLE
# pcszSubKey : LPCWSTR optional -> LPCWSTR
# pcszValue : LPCWSTR optional -> LPCWSTR
# pszPath : LPWSTR out -> LPWSTR
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
使用する型