RegSetKeyValueA
関数シグネチャ
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegSetKeyValueA(
HKEY hKey,
LPCSTR lpSubKey, // optional
LPCSTR lpValueName, // optional
DWORD dwType,
const void* lpData, // optional
DWORD cbData
);パラメーター
| 名前 | 型 | 方向 | 説明 |
|---|---|---|---|
| hKey | HKEY | in | 開いているレジストリキーへのハンドル。このキーは KEY_SET_VALUE アクセス権を指定して開かれている必要があります。詳細については、 レジストリキーのセキュリティとアクセス権を参照してください。 このハンドルは、 RegCreateKeyEx、RegCreateKeyTransacted、RegOpenKeyEx、または RegOpenKeyTransacted 関数によって返されます。次のいずれかの 定義済みキーを指定することもできます。 HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG HKEY_CURRENT_USER HKEY_LOCAL_MACHINE HKEY_USERS |
| lpSubKey | LPCSTR | inoptional | hKey で識別されるキーを基準とした、サブキーの名前。 サブキーが存在しない場合は、既定のセキュリティ記述子を持つ非揮発性キーとして作成されます。 このパラメーターが NULL の場合、値は hKey で指定されたキー内に作成されます。 |
| lpValueName | LPCSTR | inoptional | データを更新するレジストリ値の名前。 |
| dwType | DWORD | in | lpData パラメーターが指すデータの型。指定可能な型の一覧については、 レジストリ値の型を参照してください。 |
| 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
公式ドキュメント
指定したレジストリキーおよびサブキー内の、指定した値にデータを設定します。(ANSI)
戻り値
関数が成功した場合、戻り値は ERROR_SUCCESS です。
関数が失敗した場合、戻り値は Winerror.h で定義されている 0 以外のエラーコードです。 FormatMessage 関数を FORMAT_MESSAGE_FROM_SYSTEM フラグとともに使用すると、エラーの一般的な説明を取得できます。
解説(Remarks)
この関数を使用するアプリケーションをコンパイルするには、_WIN32_WINNT を 0x0600 以降として定義します。詳細については、 Windows ヘッダーの使用を参照してください。
winreg.h ヘッダーは RegSetKeyValue をエイリアスとして定義しており、UNICODE プリプロセッサ定数の定義に基づいて、この関数の ANSI 版または Unicode 版が自動的に選択されます。エンコーディング中立のエイリアスの使用と、エンコーディング中立でないコードを混在させると、不整合が生じ、コンパイルエラーや実行時エラーの原因となることがあります。詳細については、関数プロトタイプの規則を参照してください。
Microsoft 公式リファレンス: 英語 (en-us) · 日本語 (ja-jp) · 原文ソース (GitHub)
各言語での呼び出し定義
// ADVAPI32.dll (ANSI / -A)
#include <windows.h>
WIN32_ERROR RegSetKeyValueA(
HKEY hKey,
LPCSTR lpSubKey, // optional
LPCSTR lpValueName, // optional
DWORD dwType,
const void* lpData, // optional
DWORD cbData
);[DllImport("ADVAPI32.dll", CharSet = CharSet.Ansi, ExactSpelling = true)]
static extern uint RegSetKeyValueA(
IntPtr hKey, // HKEY
[MarshalAs(UnmanagedType.LPStr)] string lpSubKey, // LPCSTR optional
[MarshalAs(UnmanagedType.LPStr)] string lpValueName, // LPCSTR optional
uint dwType, // DWORD
IntPtr lpData, // void* optional
uint cbData // DWORD
);<DllImport("ADVAPI32.dll", CharSet:=CharSet.Ansi, ExactSpelling:=True)>
Public Shared Function RegSetKeyValueA(
hKey As IntPtr, ' HKEY
<MarshalAs(UnmanagedType.LPStr)> lpSubKey As String, ' LPCSTR optional
<MarshalAs(UnmanagedType.LPStr)> lpValueName As String, ' LPCSTR optional
dwType As UInteger, ' DWORD
lpData As IntPtr, ' void* optional
cbData As UInteger ' DWORD
) As UInteger
End Function' hKey : HKEY
' lpSubKey : LPCSTR optional
' lpValueName : LPCSTR optional
' dwType : DWORD
' lpData : void* optional
' cbData : DWORD
Declare PtrSafe Function RegSetKeyValueA Lib "advapi32" ( _
ByVal hKey As LongPtr, _
ByVal lpSubKey As String, _
ByVal lpValueName As String, _
ByVal dwType As Long, _
ByVal lpData As LongPtr, _
ByVal cbData As Long) As Long
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。import ctypes
from ctypes import wintypes
RegSetKeyValueA = ctypes.windll.advapi32.RegSetKeyValueA
RegSetKeyValueA.restype = wintypes.DWORD
RegSetKeyValueA.argtypes = [
wintypes.HANDLE, # hKey : HKEY
wintypes.LPCSTR, # lpSubKey : LPCSTR optional
wintypes.LPCSTR, # lpValueName : LPCSTR 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')
RegSetKeyValueA = Fiddle::Function.new(
lib['RegSetKeyValueA'],
[
Fiddle::TYPE_VOIDP, # hKey : HKEY
Fiddle::TYPE_VOIDP, # lpSubKey : LPCSTR optional
Fiddle::TYPE_VOIDP, # lpValueName : LPCSTR optional
-Fiddle::TYPE_INT, # dwType : DWORD
Fiddle::TYPE_VOIDP, # lpData : void* optional
-Fiddle::TYPE_INT, # cbData : DWORD
],
-Fiddle::TYPE_INT)#[link(name = "advapi32")]
extern "system" {
fn RegSetKeyValueA(
hKey: *mut core::ffi::c_void, // HKEY
lpSubKey: *const u8, // LPCSTR optional
lpValueName: *const u8, // LPCSTR 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.Ansi)]
public static extern uint RegSetKeyValueA(IntPtr hKey, [MarshalAs(UnmanagedType.LPStr)] string lpSubKey, [MarshalAs(UnmanagedType.LPStr)] string lpValueName, uint dwType, IntPtr lpData, uint cbData);
"@
$api = Add-Type -MemberDefinition $sig -Name 'ADVAPI32_RegSetKeyValueA' -Namespace Win32 -PassThru
# $api::RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)#uselib "ADVAPI32.dll"
#func global RegSetKeyValueA "RegSetKeyValueA" sptr, sptr, sptr, sptr, sptr, sptr
; RegSetKeyValueA hKey, lpSubKey, lpValueName, dwType, lpData, cbData ; 戻り値は stat
; hKey : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "sptr"
; lpValueName : LPCSTR optional -> "sptr"
; dwType : DWORD -> "sptr"
; lpData : void* optional -> "sptr"
; cbData : DWORD -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。#uselib "ADVAPI32.dll"
#cfunc global RegSetKeyValueA "RegSetKeyValueA" sptr, str, str, int, sptr, int
; res = RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
; hKey : HKEY -> "sptr"
; lpSubKey : LPCSTR optional -> "str"
; lpValueName : LPCSTR optional -> "str"
; dwType : DWORD -> "int"
; lpData : void* optional -> "sptr"
; cbData : DWORD -> "int"; WIN32_ERROR RegSetKeyValueA(HKEY hKey, LPCSTR lpSubKey, LPCSTR lpValueName, DWORD dwType, void* lpData, DWORD cbData)
#uselib "ADVAPI32.dll"
#cfunc global RegSetKeyValueA "RegSetKeyValueA" intptr, str, str, int, intptr, int
; res = RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
; hKey : HKEY -> "intptr"
; lpSubKey : LPCSTR optional -> "str"
; lpValueName : LPCSTR optional -> "str"
; dwType : DWORD -> "int"
; lpData : void* optional -> "intptr"
; cbData : DWORD -> "int"import (
"golang.org/x/sys/windows"
"unsafe"
)
var (
advapi32 = windows.NewLazySystemDLL("ADVAPI32.dll")
procRegSetKeyValueA = advapi32.NewProc("RegSetKeyValueA")
)
// hKey (HKEY), lpSubKey (LPCSTR optional), lpValueName (LPCSTR optional), dwType (DWORD), lpData (void* optional), cbData (DWORD)
r1, _, err := procRegSetKeyValueA.Call(
uintptr(hKey),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpSubKey))),
uintptr(unsafe.Pointer(windows.BytePtrFromString(lpValueName))),
uintptr(dwType),
uintptr(lpData),
uintptr(cbData),
)
_ = err // syscall.Errno (valid when the call sets last-error)
_ = r1 // WIN32_ERRORfunction RegSetKeyValueA(
hKey: THandle; // HKEY
lpSubKey: PAnsiChar; // LPCSTR optional
lpValueName: PAnsiChar; // LPCSTR optional
dwType: DWORD; // DWORD
lpData: Pointer; // void* optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSetKeyValueA';result := DllCall("ADVAPI32\RegSetKeyValueA"
, "Ptr", hKey ; HKEY
, "AStr", lpSubKey ; LPCSTR optional
, "AStr", lpValueName ; LPCSTR optional
, "UInt", dwType ; DWORD
, "Ptr", lpData ; void* optional
, "UInt", cbData ; DWORD
, "UInt") ; return: WIN32_ERROR●RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData) = DLL("ADVAPI32.dll", "dword RegSetKeyValueA(void*, char*, char*, dword, void*, dword)")
# 呼び出し: RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
# hKey : HKEY -> "void*"
# lpSubKey : LPCSTR optional -> "char*"
# lpValueName : LPCSTR optional -> "char*"
# dwType : DWORD -> "dword"
# lpData : void* optional -> "void*"
# cbData : DWORD -> "dword"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。const std = @import("std");
extern "advapi32" fn RegSetKeyValueA(
hKey: ?*anyopaque, // HKEY
lpSubKey: [*c]const u8, // LPCSTR optional
lpValueName: [*c]const u8, // LPCSTR optional
dwType: u32, // DWORD
lpData: ?*anyopaque, // void* optional
cbData: u32 // DWORD
) callconv(std.os.windows.WINAPI) u32;proc RegSetKeyValueA(
hKey: pointer, # HKEY
lpSubKey: cstring, # LPCSTR optional
lpValueName: cstring, # LPCSTR optional
dwType: uint32, # DWORD
lpData: pointer, # void* optional
cbData: uint32 # DWORD
): uint32 {.importc: "RegSetKeyValueA", stdcall, dynlib: "ADVAPI32.dll".}pragma(lib, "advapi32");
extern(Windows)
uint RegSetKeyValueA(
void* hKey, // HKEY
const(char)* lpSubKey, // LPCSTR optional
const(char)* lpValueName, // LPCSTR optional
uint dwType, // DWORD
void* lpData, // void* optional
uint cbData // DWORD
);ccall((:RegSetKeyValueA, "ADVAPI32.dll"), stdcall, UInt32,
(Ptr{Cvoid}, Cstring, Cstring, UInt32, Ptr{Cvoid}, UInt32),
hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
# hKey : HKEY -> Ptr{Cvoid}
# lpSubKey : LPCSTR optional -> Cstring
# lpValueName : LPCSTR optional -> Cstring
# dwType : DWORD -> UInt32
# lpData : void* optional -> Ptr{Cvoid}
# cbData : DWORD -> UInt32
# stdcall は 32bit のみ意味を持つ(x64 では無視)。local ffi = require("ffi")
ffi.cdef[[
uint32_t RegSetKeyValueA(
void* hKey,
const char* lpSubKey,
const char* lpValueName,
uint32_t dwType,
void* lpData,
uint32_t cbData);
]]
local advapi32 = ffi.load("advapi32")
-- advapi32.RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
-- hKey : HKEY
-- lpSubKey : LPCSTR optional
-- lpValueName : LPCSTR optional
-- dwType : DWORD
-- lpData : void* optional
-- cbData : DWORD
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。const koffi = require('koffi');
const lib = koffi.load('ADVAPI32.dll');
const RegSetKeyValueA = lib.func('__stdcall', 'RegSetKeyValueA', 'uint32_t', ['void *', 'str', 'str', 'uint32_t', 'void *', 'uint32_t']);
// RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
// hKey : HKEY -> 'void *'
// lpSubKey : LPCSTR optional -> 'str'
// lpValueName : LPCSTR optional -> 'str'
// dwType : DWORD -> 'uint32_t'
// lpData : void* optional -> 'void *'
// cbData : DWORD -> 'uint32_t'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。const lib = Deno.dlopen("ADVAPI32.dll", {
RegSetKeyValueA: { parameters: ["pointer", "buffer", "buffer", "u32", "pointer", "u32"], result: "u32" },
});
// lib.symbols.RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData)
// hKey : HKEY -> "pointer"
// lpSubKey : LPCSTR optional -> "buffer"
// lpValueName : LPCSTR optional -> "buffer"
// dwType : DWORD -> "u32"
// lpData : void* optional -> "pointer"
// cbData : DWORD -> "u32"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。<?php
$ffi = FFI::cdef(<<<C
uint32_t RegSetKeyValueA(
void* hKey,
const char* lpSubKey,
const char* lpValueName,
uint32_t dwType,
void* lpData,
uint32_t cbData);
C, "ADVAPI32.dll");
// $ffi->RegSetKeyValueA(hKey, lpSubKey, lpValueName, dwType, lpData, cbData);
// hKey : HKEY
// lpSubKey : LPCSTR optional
// lpValueName : LPCSTR 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.ASCII_OPTIONS);
int RegSetKeyValueA(
Pointer hKey, // HKEY
String lpSubKey, // LPCSTR optional
String lpValueName, // LPCSTR optional
int dwType, // DWORD
Pointer lpData, // void* optional
int cbData // DWORD
);
}@[Link("advapi32")]
lib LibADVAPI32
fun RegSetKeyValueA = RegSetKeyValueA(
hKey : Void*, # HKEY
lpSubKey : UInt8*, # LPCSTR optional
lpValueName : UInt8*, # LPCSTR 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 RegSetKeyValueANative = Uint32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Uint32, Pointer<Void>, Uint32);
typedef RegSetKeyValueADart = int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, int, Pointer<Void>, int);
final RegSetKeyValueA = DynamicLibrary.open('ADVAPI32.dll')
.lookupFunction<RegSetKeyValueANative, RegSetKeyValueADart>('RegSetKeyValueA');
// hKey : HKEY -> Pointer<Void>
// lpSubKey : LPCSTR optional -> Pointer<Utf8>
// lpValueName : LPCSTR optional -> Pointer<Utf8>
// dwType : DWORD -> Uint32
// lpData : void* optional -> Pointer<Void>
// cbData : DWORD -> Uint32
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。{$mode objfpc}{$H+}
function RegSetKeyValueA(
hKey: THandle; // HKEY
lpSubKey: PAnsiChar; // LPCSTR optional
lpValueName: PAnsiChar; // LPCSTR optional
dwType: DWORD; // DWORD
lpData: Pointer; // void* optional
cbData: DWORD // DWORD
): DWORD; stdcall;
external 'ADVAPI32.dll' name 'RegSetKeyValueA';import Foreign
import Foreign.C.Types
import Foreign.C.String
foreign import stdcall safe "RegSetKeyValueA"
c_RegSetKeyValueA :: Ptr () -> CString -> CString -> Word32 -> Ptr () -> Word32 -> IO Word32
-- hKey : HKEY -> Ptr ()
-- lpSubKey : LPCSTR optional -> CString
-- lpValueName : LPCSTR optional -> CString
-- dwType : DWORD -> Word32
-- lpData : void* optional -> Ptr ()
-- cbData : DWORD -> Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。open Ctypes
open Foreign
let regsetkeyvaluea =
foreign "RegSetKeyValueA"
((ptr void) @-> string @-> string @-> uint32_t @-> (ptr void) @-> uint32_t @-> returning uint32_t)
(* hKey : HKEY -> (ptr void) *)
(* lpSubKey : LPCSTR optional -> string *)
(* lpValueName : LPCSTR optional -> string *)
(* 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 ("RegSetKeyValueA" reg-set-key-value-a :convention :stdcall) :uint32
(h-key :pointer) ; HKEY
(lp-sub-key :string) ; LPCSTR optional
(lp-value-name :string) ; LPCSTR 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 $RegSetKeyValueA = Win32::API::More->new('ADVAPI32',
'DWORD RegSetKeyValueA(HANDLE hKey, LPCSTR lpSubKey, LPCSTR lpValueName, DWORD dwType, LPVOID lpData, DWORD cbData)');
# my $ret = $RegSetKeyValueA->Call($hKey, $lpSubKey, $lpValueName, $dwType, $lpData, $cbData);
# hKey : HKEY -> HANDLE
# lpSubKey : LPCSTR optional -> LPCSTR
# lpValueName : LPCSTR optional -> LPCSTR
# dwType : DWORD -> DWORD
# lpData : void* optional -> LPVOID
# cbData : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。関連項目
- f RegSetKeyValueW (Unicode版) — 指定サブキー配下に値とデータを設定する。
- f RegDeleteKeyValueA — 指定サブキー配下の値を削除する。