SetupOpenInfFile (setupapi)
Last changed: Andriy Klyuchevskyy-131.107.0.111

.
Summary
opens an INF file and returns a handle to it.

C# Signature:

[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SetupOpenInfFile([MarshalAs(UnmanagedType.LPTStr)] string FileName, [MarshalAs(UnmanagedType.LPTStr)] string InfClass, Int32 InfStyle, out uint ErrorLine);

VB Signature:

Public Declare Auto Function SetupOpenInfFile Lib "setupapi.dll" (ByVal FileName As String, ByVal InfClass As String, ByVal InfStyle As Int32, ByRef ErrorLine As UInteger) As IntPtr

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Parameters

FileName Pointer to a null-terminated string containing the name (and optional path) of the INF file to be opened. If the filename does not contain path separator characters, it is searched for, first in the \inf directory, and then in the \system32 directory. If the filename contains path separator characters, it is assumed to be a full path specification and no further processing is performed on it.
InfClass Optional pointer to a null-terminated string containing the class of INF file desired. This string must match the Class value of the Version section (for example, Class=Net). If there is no entry in the Class value, but there is an entry for ClassGUID in the Version section, the corresponding class name for that GUID is retrieved and used for the comparison.
InfStyle Style of INF file to open or search for. This parameter can be a combination of the following flags: INF_STYLE_OLDNT - a legacy INF file format, INF_STYLE_WIN4 - a Windows INF file format.
ErrorLine Optional pointer to a variable to which this function returns the (1-based) line number where an error occurred during loading of the INF file. This value is generally reliable only if GetLastError does not return ERROR_NOT_ENOUGH_MEMORY. If an out-of-memory condition does occur, ErrorLine may be 0.

Return Value

The function returns a handle to the opened INF file if it is successful. Otherwise, the return value is INVALID_HANDLE_VALUE. Extended error information can be retrieved by a call to GetLastError.

Tips & Tricks:

If the load fails because the INF file type does not match InfClass, the function returns INVALID_HANDLE_VALUE and a call to GetLastError returns ERROR_CLASS_MISMATCH.

If multiple INF file styles are specified, the style of the INF file opened can be determined by calling the SetupGetInfInformation function.

Since there may be more than one class GUID with the same class name, callers interested in INF files of a particular class (that is, a particular class GUID) should retrieve the ClassGUID value from the INF file by calling SetupQueryInfVersionInformation.

Sample Code:

C#

string infFile = <INF file full path>;
uint ErrorLine = 0;
IntPtr infHandle = SetupOpenInfFile(infFile, null, INF_STYLE_OLDNT | INF_STYLE_WIN4, out ErrorLine);
int iCode = Marshal.GetLastWin32Error();
if (infHandle.ToInt64() != INVALID_HANDLE_VALUE)
{
     Console.WriteLine("INF file was opened successfully.");
     INFCONTEXT Context = new INFCONTEXT();
     if (SetupFindFirstLine(infHandle, "Manufacturer", null, ref Context) == true)
     {
        Console.WriteLine("Manufacturers list:");
        string manufacturerName = "";
        manufacturerName = manufacturerName.PadLeft(256, ' ');
        Int32 requiredSize = manufacturerName.Length;
        SetupGetStringField(ref Context, 1, manufacturerName, manufacturerName.Length, out requiredSize);
        manufacturerName = manufacturerName.Substring(0, requiredSize-1);
        Console.WriteLine(manufacturerName);
        while(SetupFindNextLine(ref Context, out Context) == true)
        {
            manufacturerName = manufacturerName.PadLeft(256, ' ');
            requiredSize = manufacturerName.Length;
            SetupGetStringField(ref Context, 1, manufacturerName, manufacturerName.Length, out requiredSize);
            manufacturerName = manufacturerName.Substring(0, requiredSize - 1);
            Console.WriteLine(manufacturerName);
        }
     }
     else
     {
        Console.WriteLine("Can't find [Manufacturer] section.");
     }
     SetupCloseInfFile(infHandle);
}
else
{
     Console.WriteLine("Failed to open INF file. Error code - {0}.", iCode);
     if (ErrorLine != 0)
     {
        Console.WriteLine("Failure line - {0}.", ErrorLine);
     }
}

VB

Dim infFile As String = <INF file full path>
Dim ErrorLine As UInt32 = 0
Dim infHandle As IntPtr = SetupOpenInfFile(infFile, Nothing, INF_STYLE_OLDNT Or INF_STYLE_WIN4, ErrorLine)
Dim iCode As Integer = Marshal.GetLastWin32Error()
If infHandle.ToInt64() <> INVALID_HANDLE_VALUE Then
    Console.WriteLine("INF file was opened successfully.")
    Dim Context As INFCONTEXT = New INFCONTEXT()
    If SetupFindFirstLine(infHandle, "Manufacturer", Nothing, Context) = True Then
        Console.WriteLine("Manufacturers list:")
        Dim manufacturerName As String = ""
        manufacturerName = manufacturerName.PadLeft(256, " "c)
        Dim requiredSize As Int32 = manufacturerName.Length
        SetupGetStringField(Context, 1, manufacturerName, manufacturerName.Length, requiredSize)
        manufacturerName = manufacturerName.Substring(0, requiredSize - 1)
        Console.WriteLine(manufacturerName)
        While SetupFindNextLine(Context, Context) = True
            manufacturerName = manufacturerName.PadLeft(256, " "c)
            requiredSize = manufacturerName.Length
            SetupGetStringField(Context, 1, manufacturerName, manufacturerName.Length, requiredSize)
            manufacturerName = manufacturerName.Substring(0, requiredSize - 1)
            Console.WriteLine(manufacturerName)
        End While
    Else
        Console.WriteLine("Can't find [Manufacturer] section.")
    End If
    SetupCloseInfFile(infHandle)
Else
    Console.WriteLine("Failed to open INF file. Error code - {0}.", iCode)
    If ErrorLine <> 0 Then
        Console.WriteLine("Failure line - {0}.", ErrorLine)
    End If
End If

Documentation