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

SetupOpenInfFileW

関数
INFファイルを開いてハンドルを取得する(Unicode)。
DLLSETUPAPI.dll文字セットUnicode (-W)呼出規約winapiSetLastErrorあり対応OSWindows XP 以降

シグネチャ

// SETUPAPI.dll  (Unicode / -W)
#include <windows.h>

void* SetupOpenInfFileW(
    LPCWSTR FileName,
    LPCWSTR InfClass,   // optional
    INF_STYLE InfStyle,
    DWORD* ErrorLine   // optional
);

パラメーター

名前方向説明
FileNameLPCWSTRin開く INF ファイルの名前(および省略可能なパス)を格納した、null で終わる文字列へのポインター。ファイル名にパス区切り文字が含まれていない場合は、最初に %windir%\inf ディレクトリ、次に %windir%\system32 ディレクトリの順に検索されます。ファイル名にパス区切り文字が含まれている場合は、完全パス指定と見なされ、それ以上の処理は行われません。
InfClassLPCWSTRinoptional目的の INF ファイルのクラスを格納した、null で終わる文字列への省略可能なポインター。この文字列は、Version セクションの Class 値と一致する必要があります(例: Class=Net)。Class 値のエントリが存在せず、Version セクションに ClassGUID のエントリが存在する場合は、その GUID に対応するクラス名が取得され、比較に使用されます。
InfStyleINF_STYLEin

開く、または検索する INF ファイルのスタイル。このパラメーターには、次のフラグを組み合わせて指定できます。

INF_STYLE_OLDNT

従来の INF ファイル形式です。

INF_STYLE_WIN4

Windows の INF ファイル形式です。

ErrorLineDWORD*outoptional

INF ファイルの読み込み中にエラーが発生した行番号(1 から始まる)を、この関数が返す先の変数への省略可能なポインター。この値は一般に、 GetLastErrorERROR_NOT_ENOUGH_MEMORY を返さない場合にのみ信頼できます。メモリ不足の状態が発生した場合、ErrorLine は 0 になることがあります。

- InfStyle.INF_STYLE_OLDNT

従来の INF ファイル形式です。

- InfStyle.INF_STYLE_WIN4

Windows の INF ファイル形式です。

戻り値の型: void*

公式ドキュメント

SetupOpenInfFile 関数は、INF ファイルを開き、そのハンドルを返します。(Unicode)

戻り値

関数が成功した場合は、開いた INF ファイルのハンドルを返します。それ以外の場合、戻り値は INVALID_HANDLE_VALUE です。拡張エラー情報は、 GetLastError を呼び出すことで取得できます。

解説(Remarks)

INF ファイルの種類が InfClass と一致しないために読み込みが失敗した場合、関数は INVALID_HANDLE_VALUE を返し、 GetLastError の呼び出しは ERROR_CLASS_MISMATCH を返します。

複数の INF ファイル スタイルを指定した場合、開かれた INF ファイルのスタイルは SetupGetInfInformation 関数を呼び出すことで判別できます。

同じクラス名を持つクラス GUID が複数存在する可能性があるため、特定のクラス(つまり特定のクラス GUID)の INF ファイルを対象とする呼び出し元は、 SetupQueryInfVersionInformation を呼び出して INF ファイルから ClassGUID 値を取得してください。

従来の INF ファイルの場合、InfClass 文字列は、INF ファイルの Identification セクションの OptionType 値で指定された種類と一致する必要があります(例: OptionType=NetAdapter)。

メモ

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

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

各言語での呼び出し定義

// SETUPAPI.dll  (Unicode / -W)
#include <windows.h>

void* SetupOpenInfFileW(
    LPCWSTR FileName,
    LPCWSTR InfClass,   // optional
    INF_STYLE InfStyle,
    DWORD* ErrorLine   // optional
);
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
static extern IntPtr SetupOpenInfFileW(
    [MarshalAs(UnmanagedType.LPWStr)] string FileName,   // LPCWSTR
    [MarshalAs(UnmanagedType.LPWStr)] string InfClass,   // LPCWSTR optional
    uint InfStyle,   // INF_STYLE
    IntPtr ErrorLine   // DWORD* optional, out
);
<DllImport("SETUPAPI.dll", CharSet:=CharSet.Unicode, SetLastError:=True, ExactSpelling:=True)>
Public Shared Function SetupOpenInfFileW(
    <MarshalAs(UnmanagedType.LPWStr)> FileName As String,   ' LPCWSTR
    <MarshalAs(UnmanagedType.LPWStr)> InfClass As String,   ' LPCWSTR optional
    InfStyle As UInteger,   ' INF_STYLE
    ErrorLine As IntPtr   ' DWORD* optional, out
) As IntPtr
End Function
' FileName : LPCWSTR
' InfClass : LPCWSTR optional
' InfStyle : INF_STYLE
' ErrorLine : DWORD* optional, out
Declare PtrSafe Function SetupOpenInfFileW Lib "setupapi" ( _
    ByVal FileName As LongPtr, _
    ByVal InfClass As LongPtr, _
    ByVal InfStyle As Long, _
    ByVal ErrorLine As LongPtr) As LongPtr
' 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

SetupOpenInfFileW = ctypes.windll.setupapi.SetupOpenInfFileW
SetupOpenInfFileW.restype = ctypes.c_void_p
SetupOpenInfFileW.argtypes = [
    wintypes.LPCWSTR,  # FileName : LPCWSTR
    wintypes.LPCWSTR,  # InfClass : LPCWSTR optional
    wintypes.DWORD,  # InfStyle : INF_STYLE
    ctypes.POINTER(wintypes.DWORD),  # ErrorLine : DWORD* optional, out
]
# GetLastError: use ctypes.GetLastError() (or ctypes.WinDLL(use_last_error=True))
require 'fiddle'
require 'fiddle/import'

lib = Fiddle.dlopen('SETUPAPI.dll')
SetupOpenInfFileW = Fiddle::Function.new(
  lib['SetupOpenInfFileW'],
  [
    Fiddle::TYPE_VOIDP,  # FileName : LPCWSTR
    Fiddle::TYPE_VOIDP,  # InfClass : LPCWSTR optional
    -Fiddle::TYPE_INT,  # InfStyle : INF_STYLE
    Fiddle::TYPE_VOIDP,  # ErrorLine : DWORD* optional, out
  ],
  Fiddle::TYPE_VOIDP)
# Wide strings: pass str.encode("UTF-16LE") + "\x00\x00"
#[link(name = "setupapi")]
extern "system" {
    fn SetupOpenInfFileW(
        FileName: *const u16,  // LPCWSTR
        InfClass: *const u16,  // LPCWSTR optional
        InfStyle: u32,  // INF_STYLE
        ErrorLine: *mut u32  // DWORD* optional, out
    ) -> *mut ();
}
// crates: windows-sys provides ready-made bindings for this API.
$sig = @"
[DllImport("SETUPAPI.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr SetupOpenInfFileW([MarshalAs(UnmanagedType.LPWStr)] string FileName, [MarshalAs(UnmanagedType.LPWStr)] string InfClass, uint InfStyle, IntPtr ErrorLine);
"@
$api = Add-Type -MemberDefinition $sig -Name 'SETUPAPI_SetupOpenInfFileW' -Namespace Win32 -PassThru
# $api::SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
#uselib "SETUPAPI.dll"
#func global SetupOpenInfFileW "SetupOpenInfFileW" wptr, wptr, wptr, wptr
; SetupOpenInfFileW FileName, InfClass, InfStyle, varptr(ErrorLine)   ; 戻り値は stat
; FileName : LPCWSTR -> "wptr"
; InfClass : LPCWSTR optional -> "wptr"
; InfStyle : INF_STYLE -> "wptr"
; ErrorLine : DWORD* optional, out -> "wptr"
; ※HSP3.7は #func のため戻り値はシステム変数 stat に格納されます。
出力引数:
#uselib "SETUPAPI.dll"
#cfunc global SetupOpenInfFileW "SetupOpenInfFileW" wstr, wstr, int, var
; res = SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
; FileName : LPCWSTR -> "wstr"
; InfClass : LPCWSTR optional -> "wstr"
; InfStyle : INF_STYLE -> "int"
; ErrorLine : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
出力引数:
; void* SetupOpenInfFileW(LPCWSTR FileName, LPCWSTR InfClass, INF_STYLE InfStyle, DWORD* ErrorLine)
#uselib "SETUPAPI.dll"
#cfunc global SetupOpenInfFileW "SetupOpenInfFileW" wstr, wstr, int, var
; res = SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
; FileName : LPCWSTR -> "wstr"
; InfClass : LPCWSTR optional -> "wstr"
; InfStyle : INF_STYLE -> "int"
; ErrorLine : DWORD* optional, out -> "var"
; ※出力/バッファ引数は var 方式(変数を直接渡す)。varptr 方式にも切替可。
import (
	"golang.org/x/sys/windows"
	"unsafe"
)

var (
	setupapi = windows.NewLazySystemDLL("SETUPAPI.dll")
	procSetupOpenInfFileW = setupapi.NewProc("SetupOpenInfFileW")
)

// FileName (LPCWSTR), InfClass (LPCWSTR optional), InfStyle (INF_STYLE), ErrorLine (DWORD* optional, out)
r1, _, err := procSetupOpenInfFileW.Call(
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(FileName))),
	uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(InfClass))),
	uintptr(InfStyle),
	uintptr(ErrorLine),
)
_ = err  // syscall.Errno (valid when the call sets last-error)
_ = r1   // void*
function SetupOpenInfFileW(
  FileName: PWideChar;   // LPCWSTR
  InfClass: PWideChar;   // LPCWSTR optional
  InfStyle: DWORD;   // INF_STYLE
  ErrorLine: Pointer   // DWORD* optional, out
): Pointer; stdcall;
  external 'SETUPAPI.dll' name 'SetupOpenInfFileW';
result := DllCall("SETUPAPI\SetupOpenInfFileW"
    , "WStr", FileName   ; LPCWSTR
    , "WStr", InfClass   ; LPCWSTR optional
    , "UInt", InfStyle   ; INF_STYLE
    , "Ptr", ErrorLine   ; DWORD* optional, out
    , "Ptr")   ; return: void*
●SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine) = DLL("SETUPAPI.dll", "void* SetupOpenInfFileW(char*, char*, dword, void*)")
# 呼び出し: SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
# FileName : LPCWSTR -> "char*"
# InfClass : LPCWSTR optional -> "char*"
# InfStyle : INF_STYLE -> "dword"
# ErrorLine : DWORD* optional, out -> "void*"
# なでしこ1は32bit・ANSI(Shift_JIS)。文字列=char*(ANSI)、ポインタ/ハンドル=void*(4byte)。
# ※-W(Unicode)関数。なでしこ1はANSIのため -A 版の利用を推奨。
const std = @import("std");

extern "setupapi" fn SetupOpenInfFileW(
    FileName: [*c]const u16, // LPCWSTR
    InfClass: [*c]const u16, // LPCWSTR optional
    InfStyle: u32, // INF_STYLE
    ErrorLine: [*c]u32 // DWORD* optional, out
) callconv(std.os.windows.WINAPI) ?*anyopaque;
// Unicode(-W): UTF-16LE のヌル終端バッファ([*c]const u16)を渡す。
proc SetupOpenInfFileW(
    FileName: WideCString,  # LPCWSTR
    InfClass: WideCString,  # LPCWSTR optional
    InfStyle: uint32,  # INF_STYLE
    ErrorLine: ptr uint32  # DWORD* optional, out
): pointer {.importc: "SetupOpenInfFileW", stdcall, dynlib: "SETUPAPI.dll".}
# Unicode(-W): WideCString は newWideCString("...") で生成。
pragma(lib, "setupapi");
extern(Windows)
void* SetupOpenInfFileW(
    const(wchar)* FileName,   // LPCWSTR
    const(wchar)* InfClass,   // LPCWSTR optional
    uint InfStyle,   // INF_STYLE
    uint* ErrorLine   // DWORD* optional, out
);
ccall((:SetupOpenInfFileW, "SETUPAPI.dll"), stdcall, Ptr{Cvoid},
      (Cwstring, Cwstring, UInt32, Ptr{UInt32}),
      FileName, InfClass, InfStyle, ErrorLine)
# FileName : LPCWSTR -> Cwstring
# InfClass : LPCWSTR optional -> Cwstring
# InfStyle : INF_STYLE -> UInt32
# ErrorLine : DWORD* optional, out -> Ptr{UInt32}
# stdcall は 32bit のみ意味を持つ(x64 では無視)。
# Unicode(-W): Cwstring には transcode(UInt16, "...") 等で UTF-16 を渡す。
local ffi = require("ffi")
ffi.cdef[[
void* SetupOpenInfFileW(
    const uint16_t* FileName,
    const uint16_t* InfClass,
    uint32_t InfStyle,
    uint32_t* ErrorLine);
]]
local setupapi = ffi.load("setupapi")
-- setupapi.SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
-- FileName : LPCWSTR
-- InfClass : LPCWSTR optional
-- InfStyle : INF_STYLE
-- ErrorLine : DWORD* optional, out
-- 構造体/GUIDへのポインタは cdef が通るよう void* で表記(実型は各引数コメント参照)。値渡し構造体・enum は対応する typedef を cdef に追加すること。
-- Unicode(-W): uint16_t* には UTF-16LE のバッファ(ffi.new("uint16_t[?]", ...))を渡す。
const koffi = require('koffi');
const lib = koffi.load('SETUPAPI.dll');
const SetupOpenInfFileW = lib.func('__stdcall', 'SetupOpenInfFileW', 'void *', ['str16', 'str16', 'uint32_t', 'uint32_t *']);
// SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
// FileName : LPCWSTR -> 'str16'
// InfClass : LPCWSTR optional -> 'str16'
// InfStyle : INF_STYLE -> 'uint32_t'
// ErrorLine : DWORD* optional, out -> 'uint32_t *'
// 出力ポインタは koffi.out(...) で包む。構造体は koffi.struct で定義。
const lib = Deno.dlopen("SETUPAPI.dll", {
  SetupOpenInfFileW: { parameters: ["buffer", "buffer", "u32", "pointer"], result: "pointer" },
});
// lib.symbols.SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine)
// FileName : LPCWSTR -> "buffer"
// InfClass : LPCWSTR optional -> "buffer"
// InfStyle : INF_STYLE -> "u32"
// ErrorLine : DWORD* optional, out -> "pointer"
// 文字列は "buffer"。Unicode(-W) は new TextEncoder() ではなく UTF-16LE のバイト列(末尾に \x00\x00)を Uint8Array で渡す。
// 値渡し構造体は { struct: [ ...field types... ] } を使用。
<?php
$ffi = FFI::cdef(<<<C
void* SetupOpenInfFileW(
    const uint16_t* FileName,
    const uint16_t* InfClass,
    uint32_t InfStyle,
    uint32_t* ErrorLine);
C, "SETUPAPI.dll");
// $ffi->SetupOpenInfFileW(FileName, InfClass, InfStyle, ErrorLine);
// FileName : LPCWSTR
// InfClass : LPCWSTR optional
// InfStyle : INF_STYLE
// ErrorLine : DWORD* optional, out
// 構造体/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.UNICODE_OPTIONS);
    Pointer SetupOpenInfFileW(
        WString FileName,   // LPCWSTR
        WString InfClass,   // LPCWSTR optional
        int InfStyle,   // INF_STYLE
        IntByReference ErrorLine   // DWORD* optional, out
    );
}
// Unicode(-W): WString(入力)/char[](出力)で UTF-16 をマーシャリング。
@[Link("setupapi")]
lib LibSETUPAPI
  fun SetupOpenInfFileW = SetupOpenInfFileW(
    FileName : UInt16*,   # LPCWSTR
    InfClass : UInt16*,   # LPCWSTR optional
    InfStyle : UInt32,   # INF_STYLE
    ErrorLine : UInt32*   # DWORD* optional, out
  ) : Void*
end
# 構造体/GUID/enum は lib 内に対応する型定義が必要。
# 呼出規約: x64 は規約統一のため OK。x86(32bit)は WINAPI=stdcall だが Crystal の fun に stdcall 付与構文がなく非対応。
import 'dart:ffi';
import 'package:ffi/ffi.dart';

typedef SetupOpenInfFileWNative = Pointer<Void> Function(Pointer<Utf16>, Pointer<Utf16>, Uint32, Pointer<Uint32>);
typedef SetupOpenInfFileWDart = Pointer<Void> Function(Pointer<Utf16>, Pointer<Utf16>, int, Pointer<Uint32>);
final SetupOpenInfFileW = DynamicLibrary.open('SETUPAPI.dll')
    .lookupFunction<SetupOpenInfFileWNative, SetupOpenInfFileWDart>('SetupOpenInfFileW');
// FileName : LPCWSTR -> Pointer<Utf16>
// InfClass : LPCWSTR optional -> Pointer<Utf16>
// InfStyle : INF_STYLE -> Uint32
// ErrorLine : DWORD* optional, out -> Pointer<Uint32>
// 文字列は package:ffi の "...".toNativeUtf16()/toNativeUtf8() で変換。
{$mode objfpc}{$H+}
function SetupOpenInfFileW(
  FileName: PWideChar;   // LPCWSTR
  InfClass: PWideChar;   // LPCWSTR optional
  InfStyle: DWORD;   // INF_STYLE
  ErrorLine: Pointer   // DWORD* optional, out
): Pointer; stdcall;
  external 'SETUPAPI.dll' name 'SetupOpenInfFileW';
import Foreign
import Foreign.C.Types
import Foreign.C.String

foreign import stdcall safe "SetupOpenInfFileW"
  c_SetupOpenInfFileW :: CWString -> CWString -> Word32 -> Ptr Word32 -> IO (Ptr ())
-- FileName : LPCWSTR -> CWString
-- InfClass : LPCWSTR optional -> CWString
-- InfStyle : INF_STYLE -> Word32
-- ErrorLine : DWORD* optional, out -> Ptr Word32
-- 要 GHC(Windows)。stdcall は x64 では ccall として扱われる。ブロックする API は safe 呼び出し推奨。
open Ctypes
open Foreign

let setupopeninffilew =
  foreign "SetupOpenInfFileW"
    ((ptr uint16_t) @-> (ptr uint16_t) @-> uint32_t @-> (ptr uint32_t) @-> returning (ptr void))
(* FileName : LPCWSTR -> (ptr uint16_t) *)
(* InfClass : LPCWSTR optional -> (ptr uint16_t) *)
(* InfStyle : INF_STYLE -> uint32_t *)
(* ErrorLine : DWORD* optional, out -> (ptr uint32_t) *)
(* foreign は cdecl 前提。x64 Windows では WINAPI と一致。構造体は ctypes structure を定義のこと。 *)
(cffi:define-foreign-library setupapi (t "SETUPAPI.dll"))
(cffi:use-foreign-library setupapi)

(cffi:defcfun ("SetupOpenInfFileW" setup-open-inf-file-w :convention :stdcall) :pointer
  (file-name (:string :encoding :utf-16le))   ; LPCWSTR
  (inf-class (:string :encoding :utf-16le))   ; LPCWSTR optional
  (inf-style :uint32)   ; INF_STYLE
  (error-line :pointer))   ; DWORD* optional, out
; isize/usize(INT_PTR/SIZE_T)は x64 前提で :int64/:uint64。x86 では :int32/:uint32。
use Win32::API;
my $SetupOpenInfFileW = Win32::API::More->new('SETUPAPI',
    'LPVOID SetupOpenInfFileW(LPCWSTR FileName, LPCWSTR InfClass, DWORD InfStyle, LPVOID ErrorLine)');
# my $ret = $SetupOpenInfFileW->Call($FileName, $InfClass, $InfStyle, $ErrorLine);
# FileName : LPCWSTR -> LPCWSTR
# InfClass : LPCWSTR optional -> LPCWSTR
# InfStyle : INF_STYLE -> DWORD
# ErrorLine : DWORD* optional, out -> LPVOID
# 値渡し構造体は pack() した文字列、または Win32::API::Struct を使用。
# Unicode(-W): LPCWSTR/LPWSTR は Win32::API が UTF-16 変換を行う。

関連項目

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