views:

32

answers:

4
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
        "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _
            ByVal lpKeyName As String, _
            ByVal lpDefault As String, _
            ByVal lpReturnedString As String, _
            ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Dim value As String = ""
Dim length As Integer
Dim IniFileName As String

GetPrivateProfileString("Config", "UserName", "None", value, length, IniFileName)

but value contains an empty string!?

I confes to being a VB n00b, less than a week, in fact, but I can't see what's wrong there. The file exists, it contains a section called "Config" which has an entry called "UserName" with a value - but even if not, wouldn't value take the default?

(And, no, I don't want to use the registry, thanks ;-)


Edit: It's not returning an empty string - it's returning whatever I initialize value to before calling GetPrivateProfileString().

Which is to say that if I

Dim value As String = "xxx"

then it stil contains "xxx" after the call and not the default value.

+1  A: 

Try to find out code samples of using that function at this link

Hope it will helps.

Eugene Cheverda
+1  A: 

Initialize length and value as following:

Dim length As Integer = 255
Dim value As String = New String(" "c, length)

Also, according the code you posted, IniFileName seems empty. Maybe you need to assign a value there as well?

Anax
+1  A: 

You have to set the size in the params, and reserve space for the result. Insert this lines before the call to the function:

value = space(255)
length = len(value)

Forgot something: you have to use the return value of the function, as this is the length of the actual value you get, and use a Left(value,length) to get your real answer.

dwo
+1  A: 

Take a look at the example at PInvoke.net, the signature you're using for calling GetPrivateProfileString isn't quite right. Specifically, the signature shown at PInvoke.net passes through a StringBuilder and the size of the stringbuilder.

In your example, you're passing through an empty string and a zero length, so the call to GetPrivateProfileString will only ever return an empty (zero length) string, because you're telling it that the the output buffer (value) you're passing in is suitable for a zero length string.

To get the result you're expecting, your code therefore needs to be:

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 value as String
        Dim result As Integer
        Dim sb As StringBuilder
        Dim IniFileName As String = "PathAndFilenameGoesHere"

        sb = New StringBuilder(300)
        result = GetPrivateProfileString("Config", "UserName", "", sb, sb.Capacity,IniFileName)
        value = sb.ToString()

    End Sub
End Module
Rob