Valhalla Legends Forums Archive | Visual Basic Programming | Need help using INI files

AuthorMessageTime
Sorc_Polgara
I already know how to use the WritePrivateProfileString and GetPrivateProfileString API stuff... and that isn't my problem.

I need to know if there is a way to delete a [section] from an INI file or a way to overwrite/replace a [section] from an INI file in VB6?

If not, is there perhaps another way I could go about doing so...

I've tried creating FileSystemObject TextStream object thingy, you know, the VBScript way of Reading, Writing, and Appending a file. However I can't seem overwrite or delete the line with the [section] without deleting the whole text/INI file. I can't use the SkipLine method either...

Is there a way of using the FileSystemObject TextStream object thingy and I am just doing something wrong? Is there some other way of doing File I/O which would work?

I'm open to any way you think I might be able to accomplish this. Thanks


I'm kind of limited on what different APIs and Object usages out there... btw, I only learned about the GetPrivateProfileString thingy because it was a module that someone made -_-... and I'm a noob to this forum :\
June 24, 2004, 1:03 AM
kamakazie
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
June 24, 2004, 1:22 AM
Sorc_Polgara
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66921 date=1088040140]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
[/quote]

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

[quote]
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.[/quote]
June 24, 2004, 1:40 AM
Stealth
If worse comes to worst, read the whole file in, erase the parts you don't want, and spit it back out.
June 24, 2004, 2:19 AM
kamakazie
[quote author=Sorc_Polgara link=board=31;threadid=7416;start=0#msg66924 date=1088041217]
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66921 date=1088040140]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
[/quote]

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

[quote]
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.[/quote]
[/quote]

Delete the whole section including keys? Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".
June 24, 2004, 2:41 AM
Newby
[quote]Note This function is provided only for compatibility with 16-bit versions of Windows. Applications should store initialization information in the registry.[/quote]
June 24, 2004, 4:58 AM
Sorc_Polgara
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66940 date=1088044903]
[quote author=Sorc_Polgara link=board=31;threadid=7416;start=0#msg66924 date=1088041217]
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66921 date=1088040140]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
[/quote]

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

[quote]
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.[/quote]
[/quote]

Delete the whole section including keys? Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".
[/quote]

Ok, I'll try using the WritePrivateProfileSection method. I'm not quite sure on how exactly I would implement the WritePrivateProfileSection method in VB though.

I'm guessing that I would do the same thing as using the WritePrivateProfileString method which I'm using from a module...

[code]
Private Declare Function WriteIniInfo Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
[/code]

I'd do basically the same thing as this?

[code]
Private Declare Function WriteIniSection Lib "kernal32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
[/code]

Would this be correct?
June 24, 2004, 12:16 PM
kamakazie
[quote author=Sorc_Polgara link=board=31;threadid=7416;start=0#msg66980 date=1088079393]
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66940 date=1088044903]
[quote author=Sorc_Polgara link=board=31;threadid=7416;start=0#msg66924 date=1088041217]
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66921 date=1088040140]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
[/quote]

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

[quote]
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.[/quote]
[/quote]

Delete the whole section including keys? Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".
[/quote]

Ok, I'll try using the WritePrivateProfileSection method. I'm not quite sure on how exactly I would implement the WritePrivateProfileSection method in VB though.

I'm guessing that I would do the same thing as using the WritePrivateProfileString method which I'm using from a module...

[code]
Private Declare Function WriteIniInfo Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
[/code]

I'd do basically the same thing as this?

[code]
Private Declare Function WriteIniSection Lib "kernal32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
[/code]

Would this be correct?
[/quote]


That looks correct. Additionally, you could use to API Text Viewer (located in the tools directory) to get the Function declaration of WritePrivateProfileSection; there are however off-by-1 bugs in some of the Type declarations it generates.
June 24, 2004, 9:32 PM
Sorc_Polgara
This is a totally different question but still concerns INI handling in VB6

I searched the MSDN Library and found a function called GetPrivateProfileSectionNames that I want to use. However, it is not available for use in the API Text Viewer like the rest of the Profile functions. Can it still be used/declared?

I mean, using the other working INI functions like GetPrivateProfileSection that I declared and wrote Public functions for as a template, I went and tried to declare it myself. However it doesn't seem to work...

Here is my declaration and function which I put in my module. It is almost identical to GetPrivateProfileSection and GetPrivateProfileString which I have put in my module and both work.

[code]
Private Declare Function GetIniSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnedBuffer As String, ByVal nSize As Long, lpFileName As String) As Long

Public Function GetSectionNames(FileName$)
Dim INIBuffer As String
Dim INIBufferSize As Long
Dim BytesReturned As Integer

INIBuffer = Space$(1024)
INIBufferSize = Len(INIBuffer)
BytesReturned = GetIniSectionNames(INIBuffer, INIBufferSize, FileName$)
If BytesReturned = 0 Then
GetSectionNames = ""
Else
GetSectionNames = Mid$(INIBuffer, 1, BytesReturned)
End If
End Function
[/code]

This is how I try and use it and again, is almost identical to the other two.

[code]
ReturnedString = GetSectionNames(iniFileName)
[/code]

The string "iniFileName" is a working directory which I also use with the GetPrivateProfileString and GetPrivateProfileSection functions successfully.

Is it some flaw in my code, or is it what I am thinking it is... that it cannot be declared in VB6...
June 29, 2004, 12:59 AM
Eli_1
lpFilename needs to be passed 'ByVal'.
June 29, 2004, 10:08 PM
Sorc_Polgara
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66940 date=1088044903]
[quote author=Sorc_Polgara link=board=31;threadid=7416;start=0#msg66924 date=1088041217]
[quote author=dxoigmn link=board=31;threadid=7416;start=0#msg66921 date=1088040140]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/writeprivateprofilesection.asp

Take a look at that. Passing null to lpString parameter should delete the section, or atleast delete the contents of the section.
[/quote]

Doesn't look like it would do what I need it to... delete the [Section] not just its keys

[quote]
If no section name matches the string pointed to by the lpAppName parameter, WritePrivateProfileSection creates the section at the end of the specified initialization file and initializes the new section with the specified key name and value pairs.

WritePrivateProfileSection deletes the existing keys and values for the named section and inserts the key names and values in the buffer pointed to by the lpString parameter. The function does not attempt to correlate old and new key names; if the new names appear in a different order from the old names, any comments associated with preexisting keys and values in the initialization file will probably be associated with incorrect keys and values.[/quote]
[/quote]

Delete the whole section including keys? Then pass null (vbNullString) to lpString and it will delete everything in [section] including the "[section]".
[/quote]

Yes it works! Thanks

[quote]
Posted by: Eli_1 Posted on: June 29, 2004, 05:08 pm
lpFilename needs to be passed 'ByVal'.
[/quote]

Yes, fixes it too! Thanks
July 2, 2004, 5:43 PM

Search