Win32 API 日本語リファレンス
ホームSystem.Registry › RegDeleteKeyW

RegDeleteKeyW

関数
指定したサブキーをレジストリから削除する。
DLLADVAPI32.dll文字セットUnicode (-W)呼出規約winapi対応OSWindows 2000 以降

シグネチャ

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

WIN32_ERROR RegDeleteKeyW(
    HKEY hKey,
    LPCWSTR lpSubKey
);

パラメーター

名前方向説明
hKeyHKEYin

開いているレジストリキーのハンドルです。このキーのアクセス権は削除操作には影響しません。アクセス権の詳細については、 Registry Key Security and Access Rights を参照してください。

このハンドルは RegCreateKeyEx または RegOpenKeyEx 関数によって返されます。あるいは、次の Predefined Keys のいずれかを指定できます。

HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
lpSubKeyLPCWSTRin

削除するキーの名前です。hKey が指定するキーのサブキーである必要があり、サブキーを持たないものでなければなりません。このパラメーターを NULL にすることはできません。

この関数はサブキーを DELETE アクセス権で開きます。

キー名は大文字と小文字を区別しません。

詳細については、 Registry Element Size Limits を参照してください。

戻り値の型: WIN32_ERROR

公式ドキュメント

サブキーとその値を削除します。(Unicode)

戻り値

関数が成功すると、戻り値は ERROR_SUCCESS です。

関数が失敗すると、戻り値は Winerror.h で定義されている 0 以外のエラーコードになります。エラーの一般的な説明を取得するには、FORMAT_MESSAGE_FROM_SYSTEM フラグを指定して FormatMessage 関数を使用できます。

解説(Remarks)

削除されたキーは、それに対する最後のハンドルが閉じられるまで除去されません。

削除するサブキーは、サブキーを持っていてはなりません。キーとそのすべてのサブキーを削除するには、サブキーを列挙して個別に削除する必要があります。キーを再帰的に削除するには、 RegDeleteTree または SHDeleteKey 関数を使用してください。

この関数を使用する例については、Deleting a Key with Subkeys を参照してください。

メモ

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

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

各言語での呼び出し定義

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

WIN32_ERROR RegDeleteKeyW(
    HKEY hKey,
    LPCWSTR lpSubKey
);
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegDeleteKeyW(
    IntPtr hKey,   // HKEY
    [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey   // LPCWSTR
);
<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegDeleteKeyW(
    hKey As IntPtr,   ' HKEY
    <MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String   ' LPCWSTR
) As UInteger
End Function
' hKey : HKEY
' lpSubKey : LPCWSTR
Declare PtrSafe Function RegDeleteKeyW Lib "advapi32" ( _
    ByVal hKey As LongPtr, _
    ByVal lpSubKey As LongPtr) 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

RegDeleteKeyW = ctypes.windll.advapi32.RegDeleteKeyW
RegDeleteKeyW.restype = wintypes.DWORD
RegDeleteKeyW.argtypes = [
    wintypes.HANDLE,  # hKey : HKEY
    wintypes.LPCWSTR,  # lpSubKey : LPCWSTR
]
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('ADVAPI32.dll')
RegDeleteKeyW = Fiddle::Function.new(
  lib['RegDeleteKeyW'],
  [
    Fiddle::TYPE_VOIDP,  # hKey : HKEY
    Fiddle::TYPE_VOIDP,  # lpSubKey : LPCWSTR
  ],
  -Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "advapi32")]
extern "system" {
    fn RegDeleteKeyW(
        hKey: *mut core::ffi::c_void,  // HKEY
        lpSubKey: *const u16  // LPCWSTR
    ) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegDeleteKeyW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegDeleteKeyW' -Namespace Win32 -PassThru
# $api::RegDeleteKeyW(hKey, lpSubKey)
#uselib "ADVAPI32.dll"
#func global RegDeleteKeyW "RegDeleteKeyW" wptr, wptr
; RegDeleteKeyW hKey, lpSubKey   ; 戻り値は stat
; hKey : HKEY -> "wptr"
; lpSubKey : LPCWSTR -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
#uselib "ADVAPI32.dll"
#cfunc global RegDeleteKeyW "RegDeleteKeyW" sptr, wstr
; res = RegDeleteKeyW(hKey, lpSubKey)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCWSTR -> "wstr"
; WIN32_ERROR RegDeleteKeyW(HKEY hKey, LPCWSTR lpSubKey)
#uselib "ADVAPI32.dll"
#cfunc global RegDeleteKeyW "RegDeleteKeyW" intptr, wstr
; res = RegDeleteKeyW(hKey, lpSubKey)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCWSTR -> "wstr"
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
	procRegDeleteKeyW = advapi32.NewProc("RegDeleteKeyW")
)

// hKey (HKEY), lpSubKey (LPCWSTR)
r1, _, err := procRegDeleteKeyW.Call(
	uintptr(hKey),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSubKey))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // WIN32_ERROR
function RegDeleteKeyW(
  hKey: THandle;   // HKEY
  lpSubKey: PWideChar   // LPCWSTR
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegDeleteKeyW';
result := DllCall("ADVAPI32\RegDeleteKeyW"
    , "Ptr", hKey   ; HKEY
    , "WStr", lpSubKey   ; LPCWSTR
    , "UInt")   ; return: WIN32_ERROR
●RegDeleteKeyW(hKey, lpSubKey) = DLL("ADVAPI32.dll", "dword RegDeleteKeyW(void*, char*)")
# 呼び出し: RegDeleteKeyW(hKey, lpSubKey)
# hKey : HKEY -> "void*"
# lpSubKey : LPCWSTR -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "advapi32" fn RegDeleteKeyW(
    hKey: ?*anyopaque, // HKEY
    lpSubKey: [*c]const u16 // LPCWSTR
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc RegDeleteKeyW(
    hKey: pointer,  # HKEY
    lpSubKey: WideCString  # LPCWSTR
): uint32 {.importc: "RegDeleteKeyW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "advapi32");
extern(Windows)
uint RegDeleteKeyW(
    void* hKey,   // HKEY
    const(wchar)* lpSubKey   // LPCWSTR
);
ccall((:RegDeleteKeyW, "ADVAPI32.dll"), stdcall, UInt32,
      (Ptr{Cvoid}, Cwstring),
      hKey, lpSubKey)
# hKey : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCWSTR -> Cwstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
uint32_t RegDeleteKeyW(
    void* hKey,
    const uint16_t* lpSubKey);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegDeleteKeyW(hKey, lpSubKey)
-- hKey : HKEY
-- lpSubKey : LPCWSTR
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegDeleteKeyW = lib.func('__stdcall', 'RegDeleteKeyW', 'uint32_t', ['void *', 'str16']);
// RegDeleteKeyW(hKey, lpSubKey)
// hKey : HKEY -> 'void *'
// lpSubKey : LPCWSTR -> 'str16'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("ADVAPI32.dll", {
  RegDeleteKeyW: { parameters: ["pointer", "buffer"], result: "u32" },
});
// lib.symbols.RegDeleteKeyW(hKey, lpSubKey)
// hKey : HKEY -> "pointer"
// lpSubKey : LPCWSTR -> "buffer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
uint32_t RegDeleteKeyW(
    void* hKey,
    const uint16_t* lpSubKey);
C, "ADVAPI32.dll");
// $ffi->RegDeleteKeyW(hKey, lpSubKey);
// hKey : HKEY
// lpSubKey : LPCWSTR
// 構造体/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 Advapi32 extends StdCallLibrary {
    Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
    int RegDeleteKeyW(
        Pointer hKey,   // HKEY
        WString lpSubKey   // LPCWSTR
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("advapi32")]
lib LibADVAPI32
  fun RegDeleteKeyW = RegDeleteKeyW(
    hKey : Void*,   # HKEY
    lpSubKey : UInt16*   # LPCWSTR
  ) : UInt32
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef RegDeleteKeyWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>);
typedef RegDeleteKeyWDart = int Function(Pointer<Void>, Pointer<Utf16>);
final RegDeleteKeyW = DynamicLibrary.open('ADVAPI32.dll')
    .lookupFunction<RegDeleteKeyWNative, RegDeleteKeyWDart>('RegDeleteKeyW');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCWSTR -> Pointer<Utf16>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function RegDeleteKeyW(
  hKey: THandle;   // HKEY
  lpSubKey: PWideChar   // LPCWSTR
): DWORD; stdcall;
  external 'ADVAPI32.dll' name 'RegDeleteKeyW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "RegDeleteKeyW"
  c_RegDeleteKeyW :: Ptr () -> CWString -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCWSTR -> CWString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let regdeletekeyw =
  foreign "RegDeleteKeyW"
    ((ptr void) @-> (ptr uint16_t) @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCWSTR -> (ptr uint16_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)

(cffi:defcfun ("RegDeleteKeyW" reg-delete-key-w :convention :stdcall) :uint32
  (h-key :pointer)   ; HKEY
  (lp-sub-key (:string :encoding :utf-16le)))   ; LPCWSTR
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $RegDeleteKeyW = Win32::API::More->new('ADVAPI32',
    'DWORD RegDeleteKeyW(HANDLE hKey, LPCWSTR lpSubKey)');
# my $ret = $RegDeleteKeyW->Call($hKey, $lpSubKey);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCWSTR -> LPCWSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

文字セット違い
類似 API
公式の関連項目
使用する型