RegSetKeyValueW
関数シグネチャ
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegSetKeyValueW(
HKEY hKey,
LPCWSTR lpSubKey, // optional
LPCWSTR lpValueName, // optional
DWORD dwType,
const void* lpData, // optional
DWORD cbData
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | 開いているレジストリキーへのハンドル。このキーは KEY_SET_VALUE アクセス権で開かれている必要があります。詳細については、 Registry Key Security and Access Rights を参照してください。 このハンドルは、 RegCreateKeyEx、RegCreateKeyTransacted、RegOpenKeyEx、または RegOpenKeyTransacted 関数によって返されます。次の 定義済みキー のいずれかを指定することもできます。 HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS |
| lpSubKey | LPCWSTR | inoptional | hKey で識別されるキーからの相対的なサブキーの名前。 サブキーが存在しない場合は、既定のセキュリティ記述子を持つ不揮発性キーとして作成されます。 このパラメーターが NULL の場合、値は hKey で指定されたキー内に作成されます。 |
| lpValueName | LPCWSTR | inoptional | データを更新するレジストリ値の名前。 |
| dwType | DWORD | in | lpData パラメーターが指すデータの型。指定可能な型の一覧については、 Registry Value Types を参照してください。 |
| lpData | void* | inoptional | 指定した値名とともに格納するデータ。 REG_SZ などの文字列ベースの型では、文字列は null で終端されている必要があります。REG_MULTI_SZ データ型では、文字列は 2 つの null 文字で終端されている必要があります。 |
| cbData | DWORD | in | lpData パラメーターが指す情報のサイズ(バイト単位)。データの型が REG_SZ、REG_EXPAND_SZ、または REG_MULTI_SZ の場合、cbData には終端の null 文字のサイズを含める必要があります。 |
戻り値の型: WIN32_ERROR
公式ドキュメント
指定したレジストリキーおよびサブキー内の、指定した値のデータを設定します。(Unicode)
戻り値
関数が成功した場合、戻り値は ERROR_SUCCESS です。
関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラーコードです。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。
解説(Remarks)
この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以降として定義します。詳細については、 Using the Windows Headers を参照してください。
winreg.h ヘッダーは RegSetKeyValue をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版を自動的に選択します。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードの使用を混在させると、コンパイルエラーや実行時エラーを引き起こす不整合が生じる可能性があります。詳細については、Conventions for Function Prototypes を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (Unicode / -W)
#include <windows.h>
WIN32_ERROR RegSetKeyValueW(
HKEY hKey,
LPCWSTR lpSubKey, // optional
LPCWSTR lpValueName, // optional
DWORD dwType,
const void* lpData, // optional
DWORD cbData
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
static extern uint RegSetKeyValueW(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, // LPCWSTR optional
[MarshalAs(UnmanagedType.LPWStr)] string lpValueName, // LPCWSTR optional
uint dwType, // DWORD
IntPtr lpData, // void* optional
uint cbData // DWORD
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
Public Shared Function RegSetKeyValueW(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPWStr)> lpSubKey As String, ' LPCWSTR optional
<MarshalAs(UnmanagedType.LPWStr)> lpValueName As String, ' LPCWSTR optional
dwType As UInteger, ' DWORD
lpData As IntPtr, ' void* optional
cbData As UInteger ' DWORD
) As UInteger
End Function' hKey : HKEY
' lpSubKey : LPCWSTR optional
' lpValueName : LPCWSTR optional
' dwType : DWORD
' lpData : void* optional
' cbData : DWORD
Declare PtrSafe Function RegSetKeyValueW Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal lpSubKey As LongPtr, _
ByVal lpValueName As LongPtr, _
ByVal dwType As Long, _
ByVal lpData As LongPtr, _
ByVal cbData 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
RegSetKeyValueW = ctypes.windll.advapi32.RegSetKeyValueW
RegSetKeyValueW.restype = wintypes.DWORD
RegSetKeyValueW.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCWSTR, # lpSubKey : LPCWSTR optional
wintypes.LPCWSTR, # lpValueName : LPCWSTR optional
wintypes.DWORD, # dwType : DWORD
ctypes.POINTER(None), # lpData : void* optional
wintypes.DWORD, # cbData : DWORD
]require 'fiddle'
require 'fiddle/import'
lib = Fiddle.dlopen('ADVAPI32.dll')
RegSetKeyValueW = Fiddle::Function.new(
lib['RegSetKeyValueW'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # lpSubKey : LPCWSTR optional
Fiddle::TYPE_VOIDP, # lpValueName : LPCWSTR optional
-Fiddle::TYPE_INT, # dwType : DWORD
Fiddle::TYPE_VOIDP, # lpData : void* optional
-Fiddle::TYPE_INT, # cbData : DWORD
],
-Fiddle::TYPE_INT)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"#[link(name = "advapi32")]
extern "system" {
fn RegSetKeyValueW(
hKey: *mut core::ffi::c_void, // HKEY
lpSubKey: *const u16, // LPCWSTR optional
lpValueName: *const u16, // LPCWSTR optional
dwType: u32, // DWORD
lpData: *const (), // void* optional
cbData: u32 // DWORD
) -> u32;
}
// crates: windows-sys provides ready-made bindings for this API.$sig = @"
[DllImport("ADVAPI32.dll", CharSet = CharSet.Unicode)]
public static extern uint RegSetKeyValueW(IntPtr hKey, [MarshalAs(UnmanagedType.LPWStr)] string lpSubKey, [MarshalAs(UnmanagedType.LPWStr)] string lpValueName, uint dwType, IntPtr lpData, uint cbData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegSetKeyValueW' -Namespace Win32 -PassThru
# $api::RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)#uselib "ADVAPI32.dll"
#func global RegSetKeyValueW "RegSetKeyValueW" wptr, wptr, wptr, wptr, wptr, wptr
; RegSetKeyValueW hKey, lpSubKey, lpValueName, dwType, lpData, cbData ; 戻り値は stat
; hKey : HKEY -> "wptr"
; lpSubKey : LPCWSTR optional -> "wptr"
; lpValueName : LPCWSTR optional -> "wptr"
; dwType : DWORD -> "wptr"
; lpData : void* optional -> "wptr"
; cbData : DWORD -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RegSetKeyValueW "RegSetKeyValueW" sptr, wstr, wstr, int, sptr, int
; res = RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; lpValueName : LPCWSTR optional -> "wstr"
; dwType : DWORD -> "int"
; lpData : void* optional -> "sptr"
; cbData : DWORD -> "int"; WIN32_ERROR RegSetKeyValueW(HKEY hKey, LPCWSTR lpSubKey, LPCWSTR lpValueName, DWORD dwType, void* lpData, DWORD cbData)
#uselib "ADVAPI32.dll"
#cfunc global RegSetKeyValueW "RegSetKeyValueW" intptr, wstr, wstr, int, intptr, int
; res = RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCWSTR optional -> "wstr"
; lpValueName : LPCWSTR optional -> "wstr"
; dwType : DWORD -> "int"
; lpData : void* optional -> "intptr"
; cbData : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegSetKeyValueW = advapi32.NewProc("RegSetKeyValueW")
)
// hKey (HKEY), lpSubKey (LPCWSTR optional), lpValueName (LPCWSTR optional), dwType (DWORD), lpData (void* optional), cbData (DWORD)
r1, _, err := procRegSetKeyValueW.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpSubKey))),
uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(lpValueName))),
uintptr(dwType),
uintptr(lpData),
uintptr(cbData),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegSetKeyValueW(
hKey: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
lpValueName: PWideChar; // LPCWSTR optional
dwType: DWORD; // DWORD
lpData: Pointer; // void* optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSetKeyValueW';result := DllCall("ADVAPI32\RegSetKeyValueW"
, "Ptr", hKey ; HKEY
, "WStr", lpSubKey ; LPCWSTR optional
, "WStr", lpValueName ; LPCWSTR optional
, "UInt", dwType ; DWORD
, "Ptr", lpData ; void* optional
, "UInt", cbData ; DWORD
, "UInt") ; return: WIN32_ERROR●RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData) = DLL("ADVAPI32.dll", "dword RegSetKeyValueW(void*, char*, char*, dword, void*, dword)")
# 呼び出し: RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
# hKey : HKEY -> "void*"
# lpSubKey : LPCWSTR optional -> "char*"
# lpValueName : LPCWSTR optional -> "char*"
# dwType : DWORD -> "dword"
# lpData : void* optional -> "void*"
# cbData : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。const std = @import("std");
extern "advapi32" fn RegSetKeyValueW(
hKey: ?*anyopaque, // HKEY
lpSubKey: [*c]const u16, // LPCWSTR optional
lpValueName: [*c]const u16, // LPCWSTR optional
dwType: u32, // DWORD
lpData: ?*anyopaque, // void* optional
cbData: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。proc RegSetKeyValueW(
hKey: pointer, # HKEY
lpSubKey: WideCString, # LPCWSTR optional
lpValueName: WideCString, # LPCWSTR optional
dwType: uint32, # DWORD
lpData: pointer, # void* optional
cbData: uint32 # DWORD
): uint32 {.importc: "RegSetKeyValueW", stdcall, dynlib: "ADVAPI32.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。pragma(lib, "advapi32");
extern(Windows)
uint RegSetKeyValueW(
void* hKey, // HKEY
const(wchar)* lpSubKey, // LPCWSTR optional
const(wchar)* lpValueName, // LPCWSTR optional
uint dwType, // DWORD
void* lpData, // void* optional
uint cbData // DWORD
);ccall((:RegSetKeyValueW, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cwstring, Cwstring, UInt32, Ptr{Cvoid}, UInt32),
hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
# hKey : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCWSTR optional -> Cwstring
# lpValueName : LPCWSTR optional -> Cwstring
# dwType : DWORD -> UInt32
# lpData : void* optional -> Ptr{Cvoid}
# cbData : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegSetKeyValueW(
void* hKey,
const uint16_t* lpSubKey,
const uint16_t* lpValueName,
uint32_t dwType,
void* lpData,
uint32_t cbData);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
-- hKey : HKEY
-- lpSubKey : LPCWSTR optional
-- lpValueName : LPCWSTR optional
-- dwType : DWORD
-- lpData : void* optional
-- cbData : 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('ADVAPI32.dll');
const RegSetKeyValueW = lib.func('__stdcall', 'RegSetKeyValueW', 'uint32_t', ['void *', 'str16', 'str16', 'uint32_t', 'void *', 'uint32_t']);
// RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
// hKey : HKEY -> 'void *'
// lpSubKey : LPCWSTR optional -> 'str16'
// lpValueName : LPCWSTR optional -> 'str16'
// dwType : DWORD -> 'uint32_t'
// lpData : void* optional -> 'void *'
// cbData : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegSetKeyValueW: { parameters: ["pointer", "buffer", "buffer", "u32", "pointer", "u32"], result: "u32" },
});
// lib.symbols.RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
// hKey : HKEY -> "pointer"
// lpSubKey : LPCWSTR optional -> "buffer"
// lpValueName : LPCWSTR optional -> "buffer"
// dwType : DWORD -> "u32"
// lpData : void* optional -> "pointer"
// cbData : DWORD -> "u32"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegSetKeyValueW(
void* hKey,
const uint16_t* lpSubKey,
const uint16_t* lpValueName,
uint32_t dwType,
void* lpData,
uint32_t cbData);
C, "ADVAPI32.dll");
// $ffi->RegSetKeyValueW(hKey, lpSubKey, lpValueName, dwType, lpData, cbData);
// hKey : HKEY
// lpSubKey : LPCWSTR optional
// lpValueName : LPCWSTR optional
// dwType : DWORD
// lpData : void* optional
// cbData : 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 Advapi32 extends StdCallLibrary {
Advapi32 INSTANCE = Native.load("advapi32", Advapi32.class, W32APIOptions.UNICODE_OPTIONS);
int RegSetKeyValueW(
Pointer hKey, // HKEY
WString lpSubKey, // LPCWSTR optional
WString lpValueName, // LPCWSTR optional
int dwType, // DWORD
Pointer lpData, // void* optional
int cbData // DWORD
);
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。@[Link("advapi32")]
lib LibADVAPI32
fun RegSetKeyValueW = RegSetKeyValueW(
hKey : Void*, # HKEY
lpSubKey : UInt16*, # LPCWSTR optional
lpValueName : UInt16*, # LPCWSTR optional
dwType : UInt32, # DWORD
lpData : Void*, # void* optional
cbData : 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 RegSetKeyValueWNative = Uint32 Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Void>, Uint32);
typedef RegSetKeyValueWDart = int Function(Pointer<Void>, Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Void>, int);
final RegSetKeyValueW = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegSetKeyValueWNative, RegSetKeyValueWDart>('RegSetKeyValueW');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCWSTR optional -> Pointer<Utf16>
// lpValueName : LPCWSTR optional -> Pointer<Utf16>
// dwType : DWORD -> Uint32
// lpData : void* optional -> Pointer<Void>
// cbData : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegSetKeyValueW(
hKey: THandle; // HKEY
lpSubKey: PWideChar; // LPCWSTR optional
lpValueName: PWideChar; // LPCWSTR optional
dwType: DWORD; // DWORD
lpData: Pointer; // void* optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSetKeyValueW';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegSetKeyValueW"
c_RegSetKeyValueW :: Ptr () -> CWString -> CWString -> Word32 -> Ptr () -> Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCWSTR optional -> CWString
-- lpValueName : LPCWSTR optional -> CWString
-- dwType : DWORD -> Word32
-- lpData : void* optional -> Ptr ()
-- cbData : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regsetkeyvaluew =
foreign "RegSetKeyValueW"
((ptr void) @-> (ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCWSTR optional -> (ptr uint16_t) *)
(* lpValueName : LPCWSTR optional -> (ptr uint16_t) *)
(* dwType : DWORD -> uint32_t *)
(* lpData : void* optional -> (ptr void) *)
(* cbData : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)(cffi:define-foreign-library advapi32 (t "ADVAPI32.dll"))
(cffi:use-foreign-library advapi32)
(cffi:defcfun ("RegSetKeyValueW" reg-set-key-value-w :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(lp-sub-key (:string :encoding :utf-16le)) ; LPCWSTR optional
(lp-value-name (:string :encoding :utf-16le)) ; LPCWSTR optional
(dw-type :uint32) ; DWORD
(lp-data :pointer) ; void* optional
(cb-data :uint32)) ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。use Win32::API;
my $RegSetKeyValueW = Win32::API::More->new('ADVAPI32',
'DWORD RegSetKeyValueW(HANDLE hKey, LPCWSTR lpSubKey, LPCWSTR lpValueName, DWORD dwType, LPVOID lpData, DWORD cbData)');
# my $ret = $RegSetKeyValueW->Call($hKey, $lpSubKey, $lpValueName, $dwType, $lpData, $cbData);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCWSTR optional -> LPCWSTR
# lpValueName : LPCWSTR optional -> LPCWSTR
# dwType : DWORD -> DWORD
# lpData : void* optional -> LPVOID
# cbData : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。関連項目
- f RegSetKeyValueA (ANSI版) — 指定サブキー配下に値とデータを設定する。
- f RegDeleteKeyValueW — 指定サブキー配下の値を削除する。