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

SHRegCreateUSKeyA

関数
HKCUとHKLMを統合するユーザー単位レジストリキーを作成する。
DLLSHLWAPI.dll文字セットANSI (-A)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR SHRegCreateUSKeyA(
    LPCSTR pszPath,
    DWORD samDesired,
    INT_PTR hRelativeUSKey,   // optional
    INT_PTR* phNewUSKey,
    DWORD dwFlags
);

パラメーター

名前方向説明
pszPathLPCSTRin作成または開くサブキーを格納した、null で終わる文字列へのポインターです。この名前の値がサブキー内に既に存在する場合、その値が開かれます。
samDesiredDWORDin要求するセキュリティアクセスです。セキュリティアクセスの詳細については、REGSAM を参照してください。
hRelativeUSKeyINT_PTRinoptional相対パスの基準として使用するキーです。pszPath が相対パスの場合、指定されたキーは hRelativeUSKey を基準とした相対パスになります。pszPath が絶対パスの場合は、hRelativeUSKeyNULL を設定します。その場合、キーは dwFlags の値に応じて HKEY_LOCAL_MACHINE または HKEY_CURRENT_USER の下に作成されます。
phNewUSKeyINT_PTR*out新しいキーのハンドルを受け取る HUSKEY へのポインターです。
dwFlagsDWORDin

キーを開く基準となるベースキーです。次の値のうち 1 つ以上を指定できます。

SHREGSET_HKCU

HKEY_CURRENT_USER の下にキーを作成または開きます。キーが空の場合のみ作成します。

SHREGSET_FORCE_HKCU

HKEY_CURRENT_USER の下にキーを作成または開きます。キーが空でなくても作成します。

SHREGSET_HKLM

HKEY_LOCAL_MACHINE の下にキーを作成または開きます。キーが空の場合のみ作成します。

SHREGSET_FORCE_HKLM

HKEY_LOCAL_MACHINE の下にキーを作成または開きます。キーが空でなくても作成します。

SHREGSET_DEFAULT

HKEY_CURRENT_USER(強制)と HKEY_LOCAL_MACHINE(空の場合のみ)の両方の下にキーを作成または開きます。このフラグは (SHREGSET_FORCE_HKCU | SHREGSET_HKLM) と同等です。

戻り値の型: WIN32_ERROR

公式ドキュメント

ユーザー固有のサブツリー(HKEY_CURRENT_USER または HKEY_LOCAL_MACHINE)内にレジストリのサブキーを作成または開きます。(ANSI)

戻り値

型: LSTATUS

成功した場合は ERROR_SUCCESS を、それ以外の場合は Winerror.h で定義されている 0 以外のエラーコードを返します。エラーの一般的な説明を取得するには、FormatMessage 関数に FORMAT_MESSAGE_FROM_SYSTEM フラグを指定して使用できます。

解説(Remarks)

新しいキーに値を書き込む場合は、SHRegWriteUSValue を使用し、phNewUSKey を通じて返される HUSKEY ハンドルを渡して各値を書き込みます。終了したら、SHRegCloseUSKey でユーザー固有のレジストリキーを閉じます。

メモ

shlwapi.h ヘッダーは、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択するエイリアスとして SHRegCreateUSKey を定義します。エンコーディング中立なエイリアスの使用を、エンコーディング中立でないコードと混在させると、不一致が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規約 を参照してください。

出典・ライセンス: 上記「公式ドキュメント」の内容は 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 SHRegCreateUSKeyA(
    LPCSTR pszPath,
    DWORD samDesired,
    INT_PTR hRelativeUSKey,   // optional
    INT_PTR* phNewUSKey,
    DWORD dwFlags
);
[DllImport("SHLWAPI.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint SHRegCreateUSKeyA(
    [MarshalAs(UnmanagedType.LPStr)] string pszPath,   // LPCSTR
    uint samDesired,   // DWORD
    IntPtr hRelativeUSKey,   // INT_PTR optional
    out IntPtr phNewUSKey,   // INT_PTR* out
    uint dwFlags   // DWORD
);
<DllImport("SHLWAPI.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function SHRegCreateUSKeyA(
    <MarshalAs(UnmanagedType.LPStr)> pszPath As String,   ' LPCSTR
    samDesired As UInteger,   ' DWORD
    hRelativeUSKey As IntPtr,   ' INT_PTR optional
    <Out> ByRef phNewUSKey As IntPtr,   ' INT_PTR* out
    dwFlags As UInteger   ' DWORD
) As UInteger
End Function
' pszPath : LPCSTR
' samDesired : DWORD
' hRelativeUSKey : INT_PTR optional
' phNewUSKey : INT_PTR* out
' dwFlags : DWORD
Declare PtrSafe Function SHRegCreateUSKeyA Lib "shlwapi" ( _
    ByVal pszPath As String, _
    ByVal samDesired As Long, _
    ByVal hRelativeUSKey As LongPtr, _
    ByRef phNewUSKey As LongPtr, _
    ByVal dwFlags As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SHRegCreateUSKeyA = ctypes.windll.shlwapi.SHRegCreateUSKeyA
SHRegCreateUSKeyA.restype = wintypes.DWORD
SHRegCreateUSKeyA.argtypes = [
    wintypes.LPCSTR,  # pszPath : LPCSTR
    wintypes.DWORD,  # samDesired : DWORD
    ctypes.c_ssize_t,  # hRelativeUSKey : INT_PTR optional
    ctypes.POINTER(ctypes.c_ssize_t),  # phNewUSKey : INT_PTR* out
    wintypes.DWORD,  # dwFlags : DWORD
]
require 'fiddle'
require 'fiddle/import'

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

var (
	shlwapi = windows.NewLazySystemDLL("SHLWAPI.dll")
	procSHRegCreateUSKeyA = shlwapi.NewProc("SHRegCreateUSKeyA")
)

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

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

foreign import stdcall safe "SHRegCreateUSKeyA"
  c_SHRegCreateUSKeyA :: CString -> Word32 -> CIntPtr -> Ptr CIntPtr -> Word32 -> IO Word32
-- pszPath : LPCSTR -> CString
-- samDesired : DWORD -> Word32
-- hRelativeUSKey : INT_PTR optional -> CIntPtr
-- phNewUSKey : INT_PTR* out -> Ptr CIntPtr
-- dwFlags : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let shregcreateuskeya =
  foreign "SHRegCreateUSKeyA"
    (string @-> uint32_t @-> intptr_t @-> (ptr intptr_t) @-> uint32_t @-> returning uint32_t)
(* pszPath : LPCSTR -> string *)
(* samDesired : DWORD -> uint32_t *)
(* hRelativeUSKey : INT_PTR optional -> intptr_t *)
(* phNewUSKey : INT_PTR* out -> (ptr intptr_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 ("SHRegCreateUSKeyA" shreg-create-uskey-a :convention :stdcall) :uint32
  (psz-path :string)   ; LPCSTR
  (sam-desired :uint32)   ; DWORD
  (h-relative-uskey :int64)   ; INT_PTR optional
  (ph-new-uskey :pointer)   ; INT_PTR* out
  (dw-flags :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SHRegCreateUSKeyA = Win32::API::More->new('SHLWAPI',
    'DWORD SHRegCreateUSKeyA(LPCSTR pszPath, DWORD samDesired, LPARAM hRelativeUSKey, LPVOID phNewUSKey, DWORD dwFlags)');
# my $ret = $SHRegCreateUSKeyA->Call($pszPath, $samDesired, $hRelativeUSKey, $phNewUSKey, $dwFlags);
# pszPath : LPCSTR -> LPCSTR
# samDesired : DWORD -> DWORD
# hRelativeUSKey : INT_PTR optional -> LPARAM
# phNewUSKey : INT_PTR* out -> LPVOID
# dwFlags : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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