SetupGetStringField (setupapi)
Last changed: Andriy Klyuchevskyy-131.107.0.111

.
Summary
retrieves a string from the specified field of a line in an INF file.

C# Signature:

[DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetupGetStringField(ref INFCONTEXT Context, Int32 FieldIndex, [MarshalAs(UnmanagedType.LPTStr)] string ReturnBuffer, Int32 ReturnBufferSize, out Int32 RequiredSize);

VB Signature:

Public Declare Auto Function SetupGetStringField Lib "setupapi.dll" (ByRef Context As INFCONTEXT, ByVal FieldIndex As Int32, ByVal ReturnBuffer As String, ByVal ReturnBufferSize As Int32, ByRef RequiredSize As Int32) As Boolean

User-Defined Types:

INFCONTEXT

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Parameters

Context Pointer to the context for a line in an INF file.
FieldIndex The 1-based index of the field within the specified line from which the string should be retrieved. Use a FieldIndex of 0 to retrieve a string key, if present.
ReturnBuffer Optional pointer to a buffer that receives the null-terminated string. You should ensure the destination buffer is the same size or larger than the source buffer. This parameter can be NULL. See the Remarks section.
ReturnBufferSize Size of the buffer pointed to by ReturnBuffer, in characters. This includes the null terminator.
RequiredSize Optional pointer to a variable that receives the required size for the buffer pointed to by the ReturnBuffer parameter, in characters. If ReturnBuffer is specified and the actual size needed is larger than the value specified by ReturnBufferSize, the function fails and does not store the string in the buffer. In this case, a call to GetLastError returns ERROR_INSUFFICIENT_BUFFER. For the Unicode version of this function, the required size is in characters. This includes the null terminator.

Return Value

If the function succeeds, the return value is a nonzero value.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Tips & Tricks:

If this function is called with a ReturnBuffer of NULL and a ReturnBufferSize of zero, the function puts the buffer size needed to hold the specified data into the variable pointed to by RequiredSize. If the function succeeds in this, the return value is a nonzero value. Otherwise, the return value is zero and extended error information can be obtained by calling GetLastError. You can call the function once to get the required buffer size, allocate the necessary memory, and then call the function a second time to retrieve the data. Using this technique, you can avoid errors due to an insufficient buffer size.

Note that the maximum length of any single string specified in an INF Strings section is 512 characters, including the terminating NULL. If the string length is greater than 512 it will be truncated and no error will be returned. The maximum length of any concatenated string created from one or more tokens is 4096 characters.

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