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

SetupDiOpenDevRegKey

関数
デバイス用のレジストリキーをスコープ指定で開く。
DLLSETUPAPI.dll呼出規約winapiSetLastErrorあり対応OSWindows 2000 以降

シグネチャ

// SETUPAPI.dll
#include <windows.h>

HKEY SetupDiOpenDevRegKey(
    HDEVINFO DeviceInfoSet,
    SP_DEVINFO_DATA* DeviceInfoData,
    DWORD Scope,
    DWORD HwProfile,
    DWORD KeyType,
    DWORD samDesired
);

パラメーター

名前方向説明
DeviceInfoSetHDEVINFOin対象のデバイス情報セットを指すハンドル。
DeviceInfoDataSP_DEVINFO_DATA*inレジストリキーを開く対象デバイスの構造体。
ScopeDWORDinキーの適用範囲。DICS_FLAG_GLOBALかDICS_FLAG_CONFIGSPECIFICを指定する。
HwProfileDWORDin対象ハードウェアプロファイル番号。0で現在のプロファイルを指す。
KeyTypeDWORDin開くキー種別。DIREG_DEV(ソフトウェア)かDIREG_DRV(ドライバ)を指定する。
samDesiredDWORDin要求するレジストリアクセス権。KEY_READ等を指定する。

戻り値の型: HKEY

各言語での呼び出し定義

// SETUPAPI.dll
#include <windows.h>

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

SetupDiOpenDevRegKey = ctypes.windll.setupapi.SetupDiOpenDevRegKey
SetupDiOpenDevRegKey.restype = ctypes.c_void_p
SetupDiOpenDevRegKey.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
    wintypes.DWORD,  # samDesired : DWORD
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

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

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupDiOpenDevRegKey = setupapi.NewProc("SetupDiOpenDevRegKey")
)

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

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

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

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

let setupdiopendevregkey =
  foreign "SetupDiOpenDevRegKey"
    (intptr_t @-> (ptr void) @-> uint32_t @-> uint32_t @-> uint32_t @-> uint32_t @-> 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 *)
(* samDesired : DWORD -> uint32_t *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupDiOpenDevRegKey" setup-di-open-dev-reg-key :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
  (sam-desired :uint32))   ; DWORD
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupDiOpenDevRegKey = Win32::API::More->new('SETUPAPI',
    'HANDLE SetupDiOpenDevRegKey(LPARAM DeviceInfoSet, LPVOID DeviceInfoData, DWORD Scope, DWORD HwProfile, DWORD KeyType, DWORD samDesired)');
# my $ret = $SetupDiOpenDevRegKey->Call($DeviceInfoSet, $DeviceInfoData, $Scope, $HwProfile, $KeyType, $samDesired);
# DeviceInfoSet : HDEVINFO -> LPARAM
# DeviceInfoData : SP_DEVINFO_DATA* -> LPVOID
# Scope : DWORD -> DWORD
# HwProfile : DWORD -> DWORD
# KeyType : DWORD -> DWORD
# samDesired : DWORD -> DWORD
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。

関連項目

使用する型