Writing to and reading from ini files

L

lgewirtz5

I'm using the below code to write to and read from 2 ini files. My
problem is that the function, GetPrivateProfileString32, strips out all
code it thinks is extraneous from the end of the string I'm reading
from the ini file. However, when i save a string with a space at the
end, I need to read the string back including the end space. Does
anyone know a succint and reliable way to keep this space?

Thanks,

Liz

Private Declare Function GetPrivateProfileStringA Lib _
"Kernel32" (ByVal strSection As String, _
ByVal strKey As String, ByVal strDefault As String, _
ByVal strReturnedString As String, _
ByVal lngSize As Long, ByVal strFileNameName As String) As Long

Private Declare Function WritePrivateProfileStringA Lib _
"Kernel32" (ByVal strSection As String, _
ByVal strKey As String, ByVal strString As String, _
ByVal strFileNameName As String) As Long

Public Function GetPrivateProfileString32(ByVal strFileName As String,
_
ByVal strSection As String, ByVal strKey As String, _
Optional strDefault) As String
Dim strReturnString As String, lngSize As Long, lngValid As Long
On Error Resume Next
If IsMissing(strDefault) Then strDefault = ""
strReturnString = Space(1024)
lngSize = Len(strReturnString)
lngValid = GetPrivateProfileStringA(strSection, strKey, _
strDefault, strReturnString, lngSize, strFileName)
If lngValid = 0 Then
LoopCounter = 0
End If
GetPrivateProfileString32 = Left(strReturnString, lngValid)
On Error GoTo 0
End Function

Public Function WritePrivateProfileString32(ByVal strFileName As
String, _
ByVal strSection As String, ByVal strKey As String, _
ByVal strValue As String) As Boolean
Dim lngValid As Long
On Error Resume Next
lngValid = WritePrivateProfileStringA(strSection, strKey, _
strValue, strFileName)
If lngValid > 0 Then WritePrivateProfileString32 = True
On Error GoTo 0
End Function
 
J

Jezebel

You can't do it using these functions. INI file settings are, by definition,
white-space-delimited strings. There are two obvious work-arounds --

1. Read the file as a text file and extract the line you want.

2. Modify your read/write functions to include Replace() statements, to
substitute something else for spaces within the value argument.


BTW, Word has a built-in function for INI files: look at
System.PrivateProfileString
 
J

Jonathan West

Jezebel said:
You can't do it using these functions. INI file settings are, by
definition, white-space-delimited strings. There are two obvious
work-arounds --

1. Read the file as a text file and extract the line you want.

2. Modify your read/write functions to include Replace() statements, to
substitute something else for spaces within the value argument.


BTW, Word has a built-in function for INI files: look at
System.PrivateProfileString

Hi Jezebel,

I have never found Word's System.PrivateProfileString property to be very
reliable. If I want to manipluate INI files, I use the class Karl Peterson
has created for the purpose, which is available from here
http://vb.mvps.org/samples/project.asp?id=kpIni. It uses the Windows API
calls which Liz has mentioned in her post, but everything is already neatly
wrapped & ready to use.

Liz, if you use the API commands, the only way of handling the issue of
holding leading & trailing spaces is to enclose the value in a pair of
characters (e.g. quotes) when saving, and to strip the first and last
characters off the string when you read it.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
L

lgewirtz5

Yes, Word's System.PrivateProfileString inconsistently bombed on me.

I ended up doing something very close to what you suggested. I
enclosed the string in quotes (I had to use double quotes because the
function automatically strips one set of quotes) and the get function
automatically strips the quotes when reading the values back. Here's
the code I'm using. It doesn't seem very elegant but so far it's
working.


Public Function WritePrivateProfileString32(ByVal strFileName As
String, _
ByVal strSection As String, ByVal strKey As String, _
ByVal strValue As String) As Boolean
Dim lngValid As Long
Dim strValueTmp As String
On Error Resume Next
strValueTmp = """" & strValue & """"
lngValid = WritePrivateProfileStringA(strSection, strKey, _
strValueTmp, strFileName)
If lngValid > 0 Then WritePrivateProfileString32 = True
On Error GoTo 0
End Function
 
J

Jezebel

Liz, if you use the API commands, the only way of handling the issue of
holding leading & trailing spaces is to enclose the value in a pair of
characters (e.g. quotes) when saving, and to strip the first and last
characters off the string when you read it.

Hardly the only way. Another common method is to pick a character that you
don't otherwise need, and use Replace$() --

Const mSwapChar as string = "^"

:
WriteProfileString Key, Replace$(Value, " ",mSwapChar)
:

Value = Replace$(ReadProfileString(Key) , mSwapChar, " ")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top