4

INI ファイルへの書き込みは非常に簡単ですが、作成済みの INI ファイルからデータを取得する際に問題が発生しています。

私はこの機能を使用しています:

    Public Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Int32, _
    ByVal lpFileName As String) As Int32

「c:\temp\test.ini」という名前の INI ファイルがあり、次のデータが含まれているとします。

[testApp]
KeyName=keyValue
KeyName2=keyValue2

KeyName と KeyName2 の値を取得するにはどうすればよいですか?

このコードを試しましたが、成功しませんでした:

    Dim strData As String
    GetPrivateProfileString("testApp", "KeyName", "Nothing", strData, Len(strData), "c:\temp\test.ini")
    MsgBox(strData)
4

1 に答える 1

6

Pinvoke.Net Web サイトにアクセスしサンプルを修正すると、関数の宣言が異なります。

修正例

Imports System.Runtime.InteropServices
Imports System.Text
Module Module1
    Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (ByVal lpAppName As String, _
            ByVal lpKeyName As String, _
            ByVal lpDefault As String, _
            ByVal lpReturnedString As StringBuilder, _
            ByVal nSize As Integer, _
            ByVal lpFileName As String) As Integer

    Sub Main()

        Dim res As Integer
        Dim sb As StringBuilder

        sb = New StringBuilder(500)
        res = GetPrivateProfileString("testApp", "KeyName", "", sb, sb.Capacity, "c:\temp\test.ini")
        Console.WriteLine("GetPrivateProfileStrng returned : " & res.ToString())
        Console.WriteLine("KeyName is : " & sb.ToString())
        Console.ReadLine();

    End Sub
End Module
于 2012-06-28T06:40:04.503 に答える