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

SHRegGetPathA

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

シグネチャ

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

WIN32_ERROR SHRegGetPathA(
    HKEY hKey,
    LPCSTR pcszSubKey,   // optional
    LPCSTR pcszValue,   // optional
    LPSTR pszPath,
    DWORD dwFlags
);

パラメーター

名前方向説明
hKeyHKEYin現在開いているキー、またはレジストリのルートキーへのハンドルです。
pcszSubKeyLPCSTRinoptionalサブキーの名前を格納した null 終端文字列へのポインターです。
pcszValueLPCSTRinoptional展開前のパス文字列を保持する値の名前を格納した null 終端文字列へのポインターです。
pszPathLPSTRout展開後のパスを保持するバッファーです。返される文字列を格納するのに十分な大きさを確保するため、このバッファーのサイズは MAX_PATH に設定してください。
dwFlagsDWORDin予約済みです。

戻り値の型: WIN32_ERROR

公式ドキュメント

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

戻り値

型: 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 版を自動的に選択します。エンコーディング非依存のエイリアスと、エンコーディング非依存ではないコードを混在させて使用すると、不一致が生じてコンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規則 を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は Microsoft の Win32 API ドキュメント(MicrosoftDocs/sdk-api)を日本語に翻訳・改変したものです。© Microsoft Corporation. CC BY 4.0 で提供。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)

各言語での呼び出し定義

// SHLWAPI.dll  (ANSI / -A)
#include <windows.h>

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

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

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHRegGetPathA = shlwapi.NewProc("SHRegGetPathA")
)

// hKey (HKEY), pcszSubKey (LPCSTR optional), pcszValue (LPCSTR optional), pszPath (LPSTR out), dwFlags (DWORD)
r1, _, err := procSHRegGetPathA.Call(
	uintptr(hKey),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pcszSubKey))),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(pcszValue))),
	uintptr(pszPath),
	uintptr(dwFlags),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function SHRegGetPathA(
  hKey: THandle;   // HKEY
  pcszSubKey: PAnsiChar;   // LPCSTR optional
  pcszValue: PAnsiChar;   // LPCSTR optional
  pszPath: PAnsiChar;   // LPSTR out
  dwFlags: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHLWAPI.dll' name 'SHRegGetPathA';
result := DllCall("SHLWAPI\SHRegGetPathA"
    , "Ptr", hKey   ; HKEY
    , "AStr", pcszSubKey   ; LPCSTR optional
    , "AStr", pcszValue   ; LPCSTR optional
    , "Ptr", pszPath   ; LPSTR out
    , "UInt", dwFlags   ; DWORD
    , "UInt")   ; return: WIN32_ERROR
●SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags) = DLL("SHLWAPI.dll", "dword SHRegGetPathA(void*, char*, char*, char*, dword)")
# 呼び出し: SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
# hKey : HKEY -> "void*"
# pcszSubKey : LPCSTR optional -> "char*"
# pcszValue : LPCSTR optional -> "char*"
# pszPath : LPSTR out -> "char*"
# dwFlags : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "shlwapi" fn SHRegGetPathA(
    hKey: ?*anyopaque, // HKEY
    pcszSubKey: [*c]const u8, // LPCSTR optional
    pcszValue: [*c]const u8, // LPCSTR optional
    pszPath: [*c]u8, // LPSTR out
    dwFlags: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
proc SHRegGetPathA(
    hKey: pointer,  # HKEY
    pcszSubKey: cstring,  # LPCSTR optional
    pcszValue: cstring,  # LPCSTR optional
    pszPath: ptr char,  # LPSTR out
    dwFlags: uint32  # DWORD
): uint32 {.importc: "SHRegGetPathA", stdcall, dynlib: "SHLWAPI.dll".}
pragma(lib, "shlwapi");
extern(Windows)
uint SHRegGetPathA(
    void* hKey,   // HKEY
    const(char)* pcszSubKey,   // LPCSTR optional
    const(char)* pcszValue,   // LPCSTR optional
    char* pszPath,   // LPSTR out
    uint dwFlags   // DWORD
);
ccall((:SHRegGetPathA, "SHLWAPI.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Cstring, Cstring, Ptr{UInt8}, UInt32),
      hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
# hKey : HKEY -> Ptr{Cvoid}
# pcszSubKey : LPCSTR optional -> Cstring
# pcszValue : LPCSTR optional -> Cstring
# pszPath : LPSTR out -> Ptr{UInt8}
# dwFlags : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
uint32_t SHRegGetPathA(
    void* hKey,
    const char* pcszSubKey,
    const char* pcszValue,
    char* pszPath,
    uint32_t dwFlags);
]]
local shlwapi = ffi.load("shlwapi")
-- shlwapi.SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
-- hKey : HKEY
-- pcszSubKey : LPCSTR optional
-- pcszValue : LPCSTR optional
-- pszPath : LPSTR out
-- dwFlags : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SHLWAPI.dll');
const SHRegGetPathA = lib.func('__stdcall', 'SHRegGetPathA', 'uint32_t', ['void *', 'str', 'str', 'char *', 'uint32_t']);
// SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
// hKey : HKEY -> 'void *'
// pcszSubKey : LPCSTR optional -> 'str'
// pcszValue : LPCSTR optional -> 'str'
// pszPath : LPSTR out -> 'char *'
// dwFlags : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SHLWAPI.dll", {
  SHRegGetPathA: { parameters: ["pointer", "buffer", "buffer", "buffer", "u32"], result: "u32" },
});
// lib.symbols.SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags)
// hKey : HKEY -> "pointer"
// pcszSubKey : LPCSTR optional -> "buffer"
// pcszValue : LPCSTR optional -> "buffer"
// pszPath : LPSTR out -> "buffer"
// dwFlags : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t SHRegGetPathA(
    void* hKey,
    const char* pcszSubKey,
    const char* pcszValue,
    char* pszPath,
    uint32_t dwFlags);
C, "SHLWAPI.dll");
// $ffi->SHRegGetPathA(hKey, pcszSubKey, pcszValue, pszPath, dwFlags);
// hKey : HKEY
// pcszSubKey : LPCSTR optional
// pcszValue : LPCSTR optional
// pszPath : LPSTR 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.ASCII_OPTIONS);
    int SHRegGetPathA(
        Pointer hKey,   // HKEY
        String pcszSubKey,   // LPCSTR optional
        String pcszValue,   // LPCSTR optional
        byte[] pszPath,   // LPSTR out
        int dwFlags   // DWORD
    );
}
@[Link("shlwapi")]
lib LibSHLWAPI
  fun SHRegGetPathA = SHRegGetPathA(
    hKey : Void*,   # HKEY
    pcszSubKey : UInt8*,   # LPCSTR optional
    pcszValue : UInt8*,   # LPCSTR optional
    pszPath : UInt8*,   # LPSTR 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 SHRegGetPathANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Uint32);
typedef SHRegGetPathADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, int);
final SHRegGetPathA = DynamicLibrary.open('SHLWAPI.dll')
    .lookupFunction<SHRegGetPathANative, SHRegGetPathADart>('SHRegGetPathA');
// hKey : HKEY -> Pointer<Void>
// pcszSubKey : LPCSTR optional -> Pointer<Utf8>
// pcszValue : LPCSTR optional -> Pointer<Utf8>
// pszPath : LPSTR out -> Pointer<Utf8>
// dwFlags : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SHRegGetPathA(
  hKey: THandle;   // HKEY
  pcszSubKey: PAnsiChar;   // LPCSTR optional
  pcszValue: PAnsiChar;   // LPCSTR optional
  pszPath: PAnsiChar;   // LPSTR out
  dwFlags: DWORD   // DWORD
): DWORD; stdcall;
  external 'SHLWAPI.dll' name 'SHRegGetPathA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

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

let shreggetpatha =
  foreign "SHRegGetPathA"
    ((ptr void) @-> string @-> string @-> string @-> uint32_t @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* pcszSubKey : LPCSTR optional -> string *)
(* pcszValue : LPCSTR optional -> string *)
(* pszPath : LPSTR out -> 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 ("SHRegGetPathA" shreg-get-path-a :convention :stdcall) :uint32
  (h-key :pointer)   ; HKEY
  (pcsz-sub-key :string)   ; LPCSTR optional
  (pcsz-value :string)   ; LPCSTR optional
  (psz-path :pointer)   ; LPSTR out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHRegGetPathA = Win32::API::More->new('SHLWAPI',
    'DWORD SHRegGetPathA(HANDLE hKey, LPCSTR pcszSubKey, LPCSTR pcszValue, LPSTR pszPath, DWORD dwFlags)');
# my $ret = $SHRegGetPathA->Call($hKey, $pcszSubKey, $pcszValue, $pszPath, $dwFlags);
# hKey : HKEY -> HANDLE
# pcszSubKey : LPCSTR optional -> LPCSTR
# pcszValue : LPCSTR optional -> LPCSTR
# pszPath : LPSTR out -> LPSTR
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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