• GetPrivateProfileSection使用


    基本语法

    GetPrivateProfileSection 是一个 Windows API 函数,用于检索指定 INI 文件中特定节的所有键值对。它可以读取INI文件中指定节所有的键值对并将结果存储在指定的缓冲区中。

    以下是 GetPrivateProfileSection 函数的基本语法:

    DWORD GetPrivateProfileSection(
      LPCWSTR lpAppName,
      LPWSTR  lpReturnedString,
      DWORD   nSize,
      LPCWSTR lpFileName
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参数说明:

    1. lpAppName:INI 文件中要读取的节名。如果为 NULL 则返回所有节。
    2. lpReturnedString:指向用于接收键值对字符串的缓冲区的指针。
    3. nSize:指定缓冲区大小,以字符为单位。
    4. lpFileName:INI 文件路径和文件名。

    此函数返回一个 DWORD 类型的值,指定写入缓冲区的字节数(以字节为单位),不包括 NULL 结尾字符。

    GetPrivateProfileSection 函数读取指定节中所有的键值对,每个键值对之间是用 NULL 字符分隔的。因此,lpReturnedString 缓冲区中的字符串格式为:key1=value1\0key2=value2\0…\0keyn=valuen\0\0。

    举例说明:

    以下是使用 GetPrivateProfileSection 函数检索 INI 文件中特定节的键值对的两个场景示例:

    假设有一个 INI 文件 “test.ini” 包含以下内容:

    [Section1]
    Key1=Value1
    Key2=Value2
    
    [Section2]
    Key3=Value3
    Key4=Value4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    例1:读取 INI 文件中特定节的所有键值对

    以下示例演示了如何使用 GetPrivateProfileSection 函数检索 “test.ini” 文件中 “Section2” 节的所有键值对,并将结果存储在缓冲区中:

    Imports System.Runtime.InteropServices
    
    Module Module1
        <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)>
        Public Function GetPrivateProfileSection(ByVal lpAppName As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
        End Function
    
        Sub Main()
            Dim filename As String = "test.ini"
            Dim section As String = "Section2"
            Dim buffer As New System.Text.StringBuilder(2048)
    
            Dim size As Integer = GetPrivateProfileSection(section, buffer, 2048, filename)
            If size <= 0 Then
                Console.WriteLine("Failed to read section.")
                Return
            End If
    
            Dim result As String = buffer.ToString().Substring(0, size)
            Console.WriteLine("Section2: " & result)
        End Sub
    End Module
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在上面的代码示例中,我们指定了要读取的 INI 文件名 “test.ini” 和要读取的节名 “Section2”。缓冲区大小为 2048 个字符。当 GetPrivateProfileSection 函数成功调用后,我们使用输出流在控制台窗口中显示读取到的结果字符串。

    输出结果将会是:

    Section2: Key3=Value3
    Key4=Value4

    例2: 枚举INI文件中所有的节名和键值

    以下示例演示了如何枚举 “test.ini” 文件中所有的节(Section1和Section2)和它们的键值对,并将结果存储在缓冲区中:

    Imports System.Runtime.InteropServices
    
    Module Module1
        <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)>
        Public Function GetPrivateProfileSection(ByVal lpAppName As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
        End Function
    
        Sub Main()
            Dim filename As String = "test.ini"
            Dim buffer As New System.Text.StringBuilder(2048)
    
            Dim size As Integer = GetPrivateProfileSection(Nothing, buffer, 2048, filename)
            If size <= 0 Then
                Console.WriteLine("Failed to read INI file.")
                Return
            End If
    
            Dim result As String = buffer.ToString().Substring(0, size)
            Console.WriteLine(result)
        End Sub
    End Module
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在上面的代码示例中,我们没有指定要读取的节名,因此 GetPrivateProfileSection 函数将返回 “test.ini” 文件中所有的节名和它们的键值对。缓冲区大小为 2048 个字符。当 GetPrivateProfileSection 函数成功调用后,我们使用输出流在控制台窗口中显示读取到的结果字符串。

    输出结果将会是:

    Section1
    Key1=Value1
    Key2=Value2
    Section2
    Key3=Value3
    Key4=Value4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    请注意,这是用于 Windows 平台的 API 函数。如果你需要在 VBA 中使用 INI 文件,请使用内置函数 GetPrivateProfileString 、 GetAllSettings 或 GetSetting。

    GetPrivateProfileString 使用请参照:GetPrivateProfileString 使用说明






    大鹏一日同风起 扶摇直上九万里 加油!!!!

    End

  • 相关阅读:
    iNFTnews | 国内NFT发展仅限于数字藏品吗?
    Java:软件开发中最流行的Java框架是什么?
    现在的湖仓一体像是个伪命题
    Web漏洞之文件上传(方式总结)
    JDK动态代理原理解析
    Jenkins 使用 Description Setter
    node 第十三天 express初见
    Flutter插件开发流程
    js检测一个元素是否在另一个元素内部的原生方法document.caretRangeFromPoint
    locust与jmeter测试过程及结果对比
  • 原文地址:https://blog.csdn.net/qqq1994_0810/article/details/134310657