Win32 API 日本語リファレンス
ホームDevices.DeviceAndDriverInstallation › SetupDiCreateDevRegKeyA

SetupDiCreateDevRegKeyA

関数
デバイス用のレジストリキーをスコープ指定で作成する。
DLLSETUPAPI.dll文字セットANSI (-A)呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

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

HKEY SetupDiCreateDevRegKeyA(
    HDEVINFO DeviceInfoSet,
    SP_DEVINFO_DATA* DeviceInfoData,
    DWORD Scope,
    DWORD HwProfile,
    DWORD KeyType,
    void* InfHandle,   // optional
    LPCSTR InfSectionName   // optional
);

パラメーター

名前方向説明
DeviceInfoSetHDEVINFOinレジストリキーの作成対象となるデバイスを表すデバイス情報要素を含む、デバイス情報セットへのハンドル。
DeviceInfoDataSP_DEVINFO_DATA*inDeviceInfoSet 内のデバイス情報要素を指定する SP_DEVINFO_DATA 構造体へのポインター。
ScopeDWORDin

作成するレジストリキーのスコープ。スコープによって情報の格納先が決まります。作成されるキーは、グローバルなキーまたはハードウェアプロファイル固有のキーのいずれかになります。次の値のいずれかを指定できます。

DICS_FLAG_GLOBAL

グローバルな構成情報を格納するキーを作成します。この情報は特定のハードウェアプロファイルに固有のものではありません。NT ベースのオペレーティングシステムでは、HKEY_LOCAL_MACHINE をルートとするキーが作成されます。実際に開かれるキーは KeyType パラメーターの値によって異なります。

DICS_FLAG_CONFIGSPECIFIC

ハードウェアプロファイル固有の構成情報を格納するキーを作成します。このキーは HKEY_LOCAL_MACHINE ではなく、ハードウェアプロファイル固有のブランチのいずれかをルートとします。

HwProfileDWORDinHwProfileFlags に SPDICS_FLAG_CONFIGSPECIFIC が設定されている場合に、キーを作成する対象のハードウェアプロファイル。HwProfile が 0 の場合は、現在のハードウェアプロファイル用のキーが作成されます。HwProfileFlags が SPDICS_FLAG_GLOBAL の場合、HwProfile は無視されます。
KeyTypeDWORDin

作成するレジストリ格納キーの種類。次の値のいずれかを指定できます。

DIREG_DEV

デバイスのハードウェアキーを作成します。

DIREG_DRV

デバイスのソフトウェアキーを作成します。

InfHandlevoid*inoptional新しく作成されたキーに対して実行する INF DDInstall セクションを含む、開かれた INF ファイルへのハンドル。このパラメーターは省略可能で、NULL を指定できます。このパラメーターを指定する場合は、InfSectionName も指定する必要があります。
InfSectionNameLPCSTRinoptionalInfHandle で指定された INF ファイル内の INF DDInstall セクションの名前。このセクションは新しく作成されたキーに対して実行されます。このパラメーターは省略可能で、NULL を指定できます。このパラメーターを指定する場合は、InfHandle も指定する必要があります。

戻り値の型: HKEY

公式ドキュメント

SetupDiCreateDevRegKey 関数は、デバイス固有の構成情報を格納するレジストリキーを作成し、そのキーへのハンドルを返します。(ANSI)

戻り値

SetupDiCreateDevRegKey が成功した場合、デバイス固有の構成データを格納および取得できる、指定されたレジストリキーへのハンドルを返します。SetupDiCreateDevRegKey が失敗した場合は、INVALID_HANDLE_VALUE を返します。拡張エラー情報を取得するには、GetLastError を呼び出します。

解説(Remarks)

SetupDiCreateDevRegKey の呼び出し元は、Administrators グループのメンバーである必要があります。

SetupDiCreateDevRegKey から返されたハンドルは、RegCloseKey を呼び出して閉じてください。

指定されたキーが既に存在する場合、SetupDiCreateDevRegKey はそのキーへのハンドルを返します。存在しない場合、SetupDiCreateDevRegKey は指定されたキーを作成し、新しいキーへのハンドルを返します。Windows Server 2003 以降のバージョンの Windows では、このキーハンドルは KEY_READ アクセスと KEY_WRITE アクセスのみを持ちます。それ以前のバージョンの Windows では、このハンドルは KEY_ALL_ACCESS アクセスを持ちます。

SetupDiCreateDevRegKey を呼び出す前に、指定するデバイスインスタンスが登録されている必要があります。ただし、PnP デバイスインスタンスはオペレーティングシステムによって自動的に登録されることに注意してください。非 PnP デバイスインスタンスを登録する方法については、SetupDiRegisterDeviceInfo を参照してください。

レイアウトファイル(INF Version セクションLayoutFile エントリで指定されます)を使用するインストールでは、SetupDiCreateDevRegKey を呼び出す前に、SetupOpenAppendInfFile(Microsoft Windows SDK のドキュメントで説明されています)を呼び出してレイアウトファイルを開いておく必要があります。

指定したデバイス情報セットにリモートシステムのデバイス情報要素が含まれており、かつ InfHandleInfSectionName も指定されている場合、作成要求は失敗し、その後の GetLastError の呼び出しは ERROR_REMOTE_REQUEST_UNSUPPORTED を返します。

メモ

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

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

各言語での呼び出し定義

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

HKEY SetupDiCreateDevRegKeyA(
    HDEVINFO DeviceInfoSet,
    SP_DEVINFO_DATA* DeviceInfoData,
    DWORD Scope,
    DWORD HwProfile,
    DWORD KeyType,
    void* InfHandle,   // optional
    LPCSTR InfSectionName   // optional
);
[DllImport("SETUPAPI.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
static extern IntPtr SetupDiCreateDevRegKeyA(
    IntPtr DeviceInfoSet,   // HDEVINFO
    IntPtr DeviceInfoData,   // SP_DEVINFO_DATA*
    uint Scope,   // DWORD
    uint HwProfile,   // DWORD
    uint KeyType,   // DWORD
    IntPtr InfHandle,   // void* optional
    [MarshalAs(UnmanagedType.LPStr)] string InfSectionName   // LPCSTR optional
);
<DllImport("SETUPAPI.dll", CharSet:=CharSet.Ansi, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetupDiCreateDevRegKeyA(
    DeviceInfoSet As IntPtr,   ' HDEVINFO
    DeviceInfoData As IntPtr,   ' SP_DEVINFO_DATA*
    Scope As UInteger,   ' DWORD
    HwProfile As UInteger,   ' DWORD
    KeyType As UInteger,   ' DWORD
    InfHandle As IntPtr,   ' void* optional
    <MarshalAs(UnmanagedType.LPStr)> InfSectionName As String   ' LPCSTR optional
) As IntPtr
End Function
' DeviceInfoSet : HDEVINFO
' DeviceInfoData : SP_DEVINFO_DATA*
' Scope : DWORD
' HwProfile : DWORD
' KeyType : DWORD
' InfHandle : void* optional
' InfSectionName : LPCSTR optional
Declare PtrSafe Function SetupDiCreateDevRegKeyA Lib "setupapi" ( _
    ByVal DeviceInfoSet As LongPtr, _
    ByVal DeviceInfoData As LongPtr, _
    ByVal Scope As Long, _
    ByVal HwProfile As Long, _
    ByVal KeyType As Long, _
    ByVal InfHandle As LongPtr, _
    ByVal InfSectionName As String) As LongPtr
' VBA7前提(PtrSafe)。32bit Office では LongPtr→Long。Integer=16bit / Long=32bit / LongLong=64bit。
import ctypes
from ctypes import wintypes

SetupDiCreateDevRegKeyA = ctypes.windll.setupapi.SetupDiCreateDevRegKeyA
SetupDiCreateDevRegKeyA.restype = ctypes.c_void_p
SetupDiCreateDevRegKeyA.argtypes = [
    ctypes.c_ssize_t,  # DeviceInfoSet : HDEVINFO
    ctypes.c_void_p,  # DeviceInfoData : SP_DEVINFO_DATA*
    wintypes.DWORD,  # Scope : DWORD
    wintypes.DWORD,  # HwProfile : DWORD
    wintypes.DWORD,  # KeyType : DWORD
    ctypes.POINTER(None),  # InfHandle : void* optional
    wintypes.LPCSTR,  # InfSectionName : LPCSTR optional
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SETUPAPI.dll')
SetupDiCreateDevRegKeyA = Fiddle::Function.new(
  lib['SetupDiCreateDevRegKeyA'],
  [
    Fiddle::TYPE_INTPTR_T,  # DeviceInfoSet : HDEVINFO
    Fiddle::TYPE_VOIDP,  # DeviceInfoData : SP_DEVINFO_DATA*
    -Fiddle::TYPE_INT,  # Scope : DWORD
    -Fiddle::TYPE_INT,  # HwProfile : DWORD
    -Fiddle::TYPE_INT,  # KeyType : DWORD
    Fiddle::TYPE_VOIDP,  # InfHandle : void* optional
    Fiddle::TYPE_VOIDP,  # InfSectionName : LPCSTR optional
  ],
  Fiddle::TYPE_VOIDP)
#[link(name = "setupapi")]
extern "system" {
    fn SetupDiCreateDevRegKeyA(
        DeviceInfoSet: isize,  // HDEVINFO
        DeviceInfoData: *mut SP_DEVINFO_DATA,  // SP_DEVINFO_DATA*
        Scope: u32,  // DWORD
        HwProfile: u32,  // DWORD
        KeyType: u32,  // DWORD
        InfHandle: *mut (),  // void* optional
        InfSectionName: *const u8  // LPCSTR optional
    ) -> *mut core::ffi::c_void;
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SETUPAPI.dll", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr SetupDiCreateDevRegKeyA(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, uint Scope, uint HwProfile, uint KeyType, IntPtr InfHandle, [MarshalAs(UnmanagedType.LPStr)] string InfSectionName);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupDiCreateDevRegKeyA' -Namespace Win32 -PassThru
# $api::SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
#uselib "SETUPAPI.dll"
#func global SetupDiCreateDevRegKeyA "SetupDiCreateDevRegKeyA" sptr, sptr, sptr, sptr, sptr, sptr, sptr
; SetupDiCreateDevRegKeyA DeviceInfoSet, varptr(DeviceInfoData), Scope, HwProfile, KeyType, InfHandle, InfSectionName   ; 戻り値は stat
; DeviceInfoSet : HDEVINFO -> "sptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "sptr"
; Scope : DWORD -> "sptr"
; HwProfile : DWORD -> "sptr"
; KeyType : DWORD -> "sptr"
; InfHandle : void* optional -> "sptr"
; InfSectionName : LPCSTR optional -> "sptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupDiCreateDevRegKeyA "SetupDiCreateDevRegKeyA" sptr, var, int, int, int, sptr, str
; res = SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
; DeviceInfoSet : HDEVINFO -> "sptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "var"
; Scope : DWORD -> "int"
; HwProfile : DWORD -> "int"
; KeyType : DWORD -> "int"
; InfHandle : void* optional -> "sptr"
; InfSectionName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; HKEY SetupDiCreateDevRegKeyA(HDEVINFO DeviceInfoSet, SP_DEVINFO_DATA* DeviceInfoData, DWORD Scope, DWORD HwProfile, DWORD KeyType, void* InfHandle, LPCSTR InfSectionName)
#uselib "SETUPAPI.dll"
#cfunc global SetupDiCreateDevRegKeyA "SetupDiCreateDevRegKeyA" intptr, var, int, int, int, intptr, str
; res = SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
; DeviceInfoSet : HDEVINFO -> "intptr"
; DeviceInfoData : SP_DEVINFO_DATA* -> "var"
; Scope : DWORD -> "int"
; HwProfile : DWORD -> "int"
; KeyType : DWORD -> "int"
; InfHandle : void* optional -> "intptr"
; InfSectionName : LPCSTR optional -> "str"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupDiCreateDevRegKeyA = setupapi.NewProc("SetupDiCreateDevRegKeyA")
)

// DeviceInfoSet (HDEVINFO), DeviceInfoData (SP_DEVINFO_DATA*), Scope (DWORD), HwProfile (DWORD), KeyType (DWORD), InfHandle (void* optional), InfSectionName (LPCSTR optional)
r1, _, err := procSetupDiCreateDevRegKeyA.Call(
	uintptr(DeviceInfoSet),
	uintptr(DeviceInfoData),
	uintptr(Scope),
	uintptr(HwProfile),
	uintptr(KeyType),
	uintptr(InfHandle),
	uintptr(unsafe.Pointer(windows.BytePtrFromString(InfSectionName))),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // HKEY
function SetupDiCreateDevRegKeyA(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceInfoData: Pointer;   // SP_DEVINFO_DATA*
  Scope: DWORD;   // DWORD
  HwProfile: DWORD;   // DWORD
  KeyType: DWORD;   // DWORD
  InfHandle: Pointer;   // void* optional
  InfSectionName: PAnsiChar   // LPCSTR optional
): THandle; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiCreateDevRegKeyA';
result := DllCall("SETUPAPI\SetupDiCreateDevRegKeyA"
    , "Ptr", DeviceInfoSet   ; HDEVINFO
    , "Ptr", DeviceInfoData   ; SP_DEVINFO_DATA*
    , "UInt", Scope   ; DWORD
    , "UInt", HwProfile   ; DWORD
    , "UInt", KeyType   ; DWORD
    , "Ptr", InfHandle   ; void* optional
    , "AStr", InfSectionName   ; LPCSTR optional
    , "Ptr")   ; return: HKEY
●SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName) = DLL("SETUPAPI.dll", "void* SetupDiCreateDevRegKeyA(int, void*, dword, dword, dword, void*, char*)")
# 呼び出し: SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
# DeviceInfoSet : HDEVINFO -> "int"
# DeviceInfoData : SP_DEVINFO_DATA* -> "void*"
# Scope : DWORD -> "dword"
# HwProfile : DWORD -> "dword"
# KeyType : DWORD -> "dword"
# InfHandle : void* optional -> "void*"
# InfSectionName : LPCSTR optional -> "char*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
const std = @import("std");

extern "setupapi" fn SetupDiCreateDevRegKeyA(
    DeviceInfoSet: isize, // HDEVINFO
    DeviceInfoData: [*c]SP_DEVINFO_DATA, // SP_DEVINFO_DATA*
    Scope: u32, // DWORD
    HwProfile: u32, // DWORD
    KeyType: u32, // DWORD
    InfHandle: ?*anyopaque, // void* optional
    InfSectionName: [*c]const u8 // LPCSTR optional
) callconv(std.os.windows.WINAPI) ?*anyopaque;
proc SetupDiCreateDevRegKeyA(
    DeviceInfoSet: int,  # HDEVINFO
    DeviceInfoData: ptr SP_DEVINFO_DATA,  # SP_DEVINFO_DATA*
    Scope: uint32,  # DWORD
    HwProfile: uint32,  # DWORD
    KeyType: uint32,  # DWORD
    InfHandle: pointer,  # void* optional
    InfSectionName: cstring  # LPCSTR optional
): pointer {.importc: "SetupDiCreateDevRegKeyA", stdcall, dynlib: "SETUPAPI.dll".}
pragma(lib, "setupapi");
extern(Windows)
void* SetupDiCreateDevRegKeyA(
    ptrdiff_t DeviceInfoSet,   // HDEVINFO
    SP_DEVINFO_DATA* DeviceInfoData,   // SP_DEVINFO_DATA*
    uint Scope,   // DWORD
    uint HwProfile,   // DWORD
    uint KeyType,   // DWORD
    void* InfHandle,   // void* optional
    const(char)* InfSectionName   // LPCSTR optional
);
ccall((:SetupDiCreateDevRegKeyA, "SETUPAPI.dll"), stdcall, Ptr{Cvoid},
      (Int, Ptr{SP_DEVINFO_DATA}, UInt32, UInt32, UInt32, Ptr{Cvoid}, Cstring),
      DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
# DeviceInfoSet : HDEVINFO -> Int
# DeviceInfoData : SP_DEVINFO_DATA* -> Ptr{SP_DEVINFO_DATA}
# Scope : DWORD -> UInt32
# HwProfile : DWORD -> UInt32
# KeyType : DWORD -> UInt32
# InfHandle : void* optional -> Ptr{Cvoid}
# InfSectionName : LPCSTR optional -> Cstring
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
local ffi = require("ffi")
ffi.cdef[[
void* SetupDiCreateDevRegKeyA(
    intptr_t DeviceInfoSet,
    void* DeviceInfoData,
    uint32_t Scope,
    uint32_t HwProfile,
    uint32_t KeyType,
    void* InfHandle,
    const char* InfSectionName);
]]
local setupapi = ffi.load("setupapi")
-- setupapi.SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
-- DeviceInfoSet : HDEVINFO
-- DeviceInfoData : SP_DEVINFO_DATA*
-- Scope : DWORD
-- HwProfile : DWORD
-- KeyType : DWORD
-- InfHandle : void* optional
-- InfSectionName : LPCSTR optional
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
const koffi = require('koffi');
const lib = koffi.load('SETUPAPI.dll');
const SetupDiCreateDevRegKeyA = lib.func('__stdcall', 'SetupDiCreateDevRegKeyA', 'void *', ['intptr_t', 'void *', 'uint32_t', 'uint32_t', 'uint32_t', 'void *', 'str']);
// SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
// DeviceInfoSet : HDEVINFO -> 'intptr_t'
// DeviceInfoData : SP_DEVINFO_DATA* -> 'void *'
// Scope : DWORD -> 'uint32_t'
// HwProfile : DWORD -> 'uint32_t'
// KeyType : DWORD -> 'uint32_t'
// InfHandle : void* optional -> 'void *'
// InfSectionName : LPCSTR optional -> 'str'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SETUPAPI.dll", {
  SetupDiCreateDevRegKeyA: { parameters: ["isize", "pointer", "u32", "u32", "u32", "pointer", "buffer"], result: "pointer" },
});
// lib.symbols.SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName)
// DeviceInfoSet : HDEVINFO -> "isize"
// DeviceInfoData : SP_DEVINFO_DATA* -> "pointer"
// Scope : DWORD -> "u32"
// HwProfile : DWORD -> "u32"
// KeyType : DWORD -> "u32"
// InfHandle : void* optional -> "pointer"
// InfSectionName : LPCSTR optional -> "buffer"
// 文字列は "buffer"。ANSI(-A) は new TextEncoder() で UTF-8/ANSI バイト列(末尾に \x00)を渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* SetupDiCreateDevRegKeyA(
    intptr_t DeviceInfoSet,
    void* DeviceInfoData,
    uint32_t Scope,
    uint32_t HwProfile,
    uint32_t KeyType,
    void* InfHandle,
    const char* InfSectionName);
C, "SETUPAPI.dll");
// $ffi->SetupDiCreateDevRegKeyA(DeviceInfoSet, DeviceInfoData, Scope, HwProfile, KeyType, InfHandle, InfSectionName);
// DeviceInfoSet : HDEVINFO
// DeviceInfoData : SP_DEVINFO_DATA*
// Scope : DWORD
// HwProfile : DWORD
// KeyType : DWORD
// InfHandle : void* optional
// InfSectionName : LPCSTR optional
// 構造体/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 Setupapi extends StdCallLibrary {
    Setupapi INSTANCE = Native.load("setupapi", Setupapi.class, W32APIOptions.ASCII_OPTIONS);
    Pointer SetupDiCreateDevRegKeyA(
        long DeviceInfoSet,   // HDEVINFO
        Pointer DeviceInfoData,   // SP_DEVINFO_DATA*
        int Scope,   // DWORD
        int HwProfile,   // DWORD
        int KeyType,   // DWORD
        Pointer InfHandle,   // void* optional
        String InfSectionName   // LPCSTR optional
    );
}
@[Link("setupapi")]
lib LibSETUPAPI
  fun SetupDiCreateDevRegKeyA = SetupDiCreateDevRegKeyA(
    DeviceInfoSet : LibC::SSizeT,   # HDEVINFO
    DeviceInfoData : SP_DEVINFO_DATA*,   # SP_DEVINFO_DATA*
    Scope : UInt32,   # DWORD
    HwProfile : UInt32,   # DWORD
    KeyType : UInt32,   # DWORD
    InfHandle : Void*,   # void* optional
    InfSectionName : UInt8*   # LPCSTR optional
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetupDiCreateDevRegKeyANative = Pointer<Void> Function(IntPtr, Pointer<Void>, Uint32, Uint32, Uint32, Pointer<Void>, Pointer<Utf8>);
typedef SetupDiCreateDevRegKeyADart = Pointer<Void> Function(int, Pointer<Void>, int, int, int, Pointer<Void>, Pointer<Utf8>);
final SetupDiCreateDevRegKeyA = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupDiCreateDevRegKeyANative, SetupDiCreateDevRegKeyADart>('SetupDiCreateDevRegKeyA');
// DeviceInfoSet : HDEVINFO -> IntPtr
// DeviceInfoData : SP_DEVINFO_DATA* -> Pointer<Void>
// Scope : DWORD -> Uint32
// HwProfile : DWORD -> Uint32
// KeyType : DWORD -> Uint32
// InfHandle : void* optional -> Pointer<Void>
// InfSectionName : LPCSTR optional -> Pointer<Utf8>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupDiCreateDevRegKeyA(
  DeviceInfoSet: NativeInt;   // HDEVINFO
  DeviceInfoData: Pointer;   // SP_DEVINFO_DATA*
  Scope: DWORD;   // DWORD
  HwProfile: DWORD;   // DWORD
  KeyType: DWORD;   // DWORD
  InfHandle: Pointer;   // void* optional
  InfSectionName: PAnsiChar   // LPCSTR optional
): THandle; stdcall;
  external 'SETUPAPI.dll' name 'SetupDiCreateDevRegKeyA';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupDiCreateDevRegKeyA"
  c_SetupDiCreateDevRegKeyA :: CIntPtr -> Ptr () -> Word32 -> Word32 -> Word32 -> Ptr () -> CString -> IO (Ptr ())
-- DeviceInfoSet : HDEVINFO -> CIntPtr
-- DeviceInfoData : SP_DEVINFO_DATA* -> Ptr ()
-- Scope : DWORD -> Word32
-- HwProfile : DWORD -> Word32
-- KeyType : DWORD -> Word32
-- InfHandle : void* optional -> Ptr ()
-- InfSectionName : LPCSTR optional -> CString
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setupdicreatedevregkeya =
  foreign "SetupDiCreateDevRegKeyA"
    (intptr_t @-> (ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> (ptr void) @-> string @-> returning (ptr void))
(* DeviceInfoSet : HDEVINFO -> intptr_t *)
(* DeviceInfoData : SP_DEVINFO_DATA* -> (ptr void) *)
(* Scope : DWORD -> uint32_t *)
(* HwProfile : DWORD -> uint32_t *)
(* KeyType : DWORD -> uint32_t *)
(* InfHandle : void* optional -> (ptr void) *)
(* InfSectionName : LPCSTR optional -> string *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupDiCreateDevRegKeyA" setup-di-create-dev-reg-key-a :convention :stdcall) :pointer
  (device-info-set :int64)   ; HDEVINFO
  (device-info-data :pointer)   ; SP_DEVINFO_DATA*
  (scope :uint32)   ; DWORD
  (hw-profile :uint32)   ; DWORD
  (key-type :uint32)   ; DWORD
  (inf-handle :pointer)   ; void* optional
  (inf-section-name :string))   ; LPCSTR optional
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupDiCreateDevRegKeyA = Win32::API::More->new('SETUPAPI',
    'HANDLE SetupDiCreateDevRegKeyA(LPARAM DeviceInfoSet, LPVOID DeviceInfoData, DWORD Scope, DWORD HwProfile, DWORD KeyType, LPVOID InfHandle, LPCSTR InfSectionName)');
# my $ret = $SetupDiCreateDevRegKeyA->Call($DeviceInfoSet, $DeviceInfoData, $Scope, $HwProfile, $KeyType, $InfHandle, $InfSectionName);
# DeviceInfoSet : HDEVINFO -> LPARAM
# DeviceInfoData : SP_DEVINFO_DATA* -> LPVOID
# Scope : DWORD -> DWORD
# HwProfile : DWORD -> DWORD
# KeyType : DWORD -> DWORD
# InfHandle : void* optional -> LPVOID
# InfSectionName : LPCSTR optional -> LPCSTR
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

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