Jag försöker sätta mig in i DotNet och har dykt på ett problem med strängar som bryts vid null. Hittade en lösningProblem med strängar som bryts vid Null
Jag håller på med en inifil hanterare och lyckas inte få ReadSection i nedanstående kod att returnera
alla sections i inifilen som ser ut så här:
<code>
[SETTINGS]
1
2
3
</code>
Den returnerar bara "1", I VB 6.0 så skulle den ha returnerat en sträng med 1 & chr(0) & 2 & chr(0) & 3 & chr(0)
Är det någon som har en förklaring/lösning ?
Jag undrar också ifall det finns något bättre sätt att skapa en buffer att skicka in i api anrop ?
typ som i vb 6.0 String * 255
<code>
Public Class Ini
Private Declare Ansi Function GetPrivateProfileString Lib "kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Ansi Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As System.Text.StringBuilder, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Ansi Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Ansi Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private ConfigObject As String
Public Sub New(ByVal strPathFileName As String)
ConfigObject = strPathFileName
End Sub
'reads ini string
Public Function Read(ByVal Section As String, ByVal Key As String, Optional ByVal strDefault As String = "") As String
Dim Retval As New System.Text.StringBuilder(256), v As Integer
v = GetPrivateProfileString(Section, Key, strDefault, Retval, Retval.Capacity, ConfigObject)
If v > 0 Then Read = Left(Retval.ToString, v)
End Function
'reads ini section
Public Function ReadSection(ByVal Section As String) As String
Dim Retval As New System.Text.StringBuilder(4096), v As Integer
v = GetPrivateProfileSection(Section, Retval, Retval.Capacity, ConfigObject)
If v > 0 Then ReadSection = Left(Retval.ToString, v)
End Function
'writes ini
Public Sub Save(ByVal Section As String, ByVal Key As String, ByVal Value As String)
WritePrivateProfileString(Section, Key, Value, ConfigObject)
End Sub
'writes ini section
Public Sub SaveSection(ByVal Section As String, ByVal Value As String)
WritePrivateProfileSection(Section, Value, ConfigObject)
End Sub
'removes ini section
Public Sub RemoveSection(ByVal Section As String)
WritePrivateProfileString(Section, vbNullString, "", ConfigObject)
End Sub
'Example
'MessageBox.Show(myIni.ReadSection("SETTINGS"), "Varde", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Class
</code>Sv: Problem med strängar som bryts vid Null
ändrade från System.Text.StringBuilder till String
och i funktionen som krånglade
.PadLeft gjorde susen
<code>
Public Function ReadSection(ByVal Section As String) As String
Dim v As Integer, strTemp As String = ""
strTemp = strTemp.PadLeft(4096, Chr(0))
v = GetPrivateProfileSection(Section, strTemp, 4096, ConfigObject)
If v > 0 Then ReadSection = Left(strTemp.Replace(Chr(0), ""), v)
End Function
</code>