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

RegSetKeyValueA

関数
指定サブキー配下に値とデータを設定する。
DLLADVAPI32.dll文字セットANSI (-A)呼出規約winapi対応OSWindows Vista 以降

シグネチャ

// 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
);

パラメーター

名前方向
hKeyHKEYin
lpSubKeyLPCSTRinoptional
lpValueNameLPCSTRinoptional
dwTypeDWORDin
lpDatavoid*inoptional
cbDataDWORDin

戻り値の型: WIN32_ERROR

各言語での呼び出し定義

// 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_ERROR
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';
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)。