Folk,<br>
<br>
Sorry, thought I posted this last time, but apparently got confused after a system crash (cherry-picker took out a power line <grin!>

.<br>
<br>
'Declares for INI read/write<br>
'16-Declare Function GetPPI% Lib "Kernel" Alias "GetPrivateProfileInt" (ByVal lpAppName$, ByVal lpKeyName As Any, ByVal nDefault%, ByVal lpFileName$)<br>
Declare Function GetPPI& Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpAppName$, ByVal lpKeyName As Any, ByVal nDefault&, ByVal lpFileName$)<br>
'16-Declare Function GetPPS% Lib "Kernel" Alias "GetPrivateProfileString" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnedString$, ByVal nSize%, ByVal lpFileName$)<br>
Declare Function GetPPS& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, ByVal lpReturnedString$, ByVal nSize&, ByVal lpFileName$)<br>
Declare Function GetKeyNames& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpAppName$, ByVal lpKeyName As Any, ByVal lpDefault$, ByVal lpReturnedString$, ByVal nSize&, ByVal lpFileName$)<br>
'16-Declare Function WritePPS% Lib "Kernel" Alias "WritePrivateProfileString" (ByVal lpApplicationName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName$)<br>
Declare Function WritePPS& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpAppName$, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName$)<br>
<br>
The above are the declarations that allow reading/writing .ini files, both the 16-bit - commented out - and the 32-bit. As you may note, I've abbreviated the declaration name, but you can see it in the 'Alias' element of each declare.<br>
<br>
If you happen to note duplicate declarations with different names, that will be because of allow use of 0& as a Null value (type declared As Any). Reason for this is that if I declare a Section or a KeyName As Any, I could accidentally delete that Section or KeyName if my code ended up sending a Null. However, if the Section or KeyName is declared As String, a Null entry will simply cause the call to fail. So, the only time I want to delete a Section or KeyName, I use the alternate declaration - no possibility of confusion, as I only use that declare for deletions.
<br>
Now, before I get into usage, since I don't know what the limits are here for a post - must have overlooked it in the how-to - let me say that I have a functioning .ini editor just converted from VB 3.0 to 5.0. I have the Enterprise edition, but I don't think anything is being used that won't run in the lesser versions. I'll have to pull out references to a print-preview unit, since that uses the VSView OCX(s), but otherwise, I'd be more than happy to mail it to you privately. Only problem might be the Common Controls, as I'm using that for a status bar, but that won't be a show stopper.
<br>
OK, now let's look at usage. I normally declare these variable as Global (or Public) in Utiliy.bas, a module of generic routines that I plug into all my applications (user-color routines, MRU file lists, control focus color changes, etc.). The variables are gIniFile$ (the $ sign is a string type declaration character that I got into the habit of using a long time ago - started VB with v.1.0), gSection$ (or gApplication$ in older apps), gKeyName$, and gKey$. <br>
I usually put the .ini file in the application directory, so gIniFile$ can be popoulated using App.Path and .ExeName, adding a '\' as necessary.<br>
gSection$ is the part of the .ini file that shows as [Section] in the actual file. This is the Application or Section to which the folowing KeyName/Key pairs apply.<br>
gKeyName$ is the left side of the '=', and<br>
gKey$ is the right side of the '='.<br>
<br>
For instance,<br>
[Microsoft Word]<br>
Initial = jqp<br>
<br>
might be an entry for some version of Word, and the Initial=jqp pair would hold the initials of the Word user for summary purposes.<br>
<br>
So, gSection$ would be "Microsoft Word" (spaces ok, and Windows adds the '[ . . . ]'),<br>
gKeyName$ would be 'Initial', and<br>
gKey$ would be 'jqp', for John Q. Public. Ok, with me so far?<br>
<br>
Now, to write this data to the .ini file code would be something like this.<br>
<br>
gSection$ = txtSection<br>
gKeyName$ = txtKeyName<br>
gKey$ = txtKey<br>
gIniFile$ = Me.Caption<br>
Debug.Print gSection$, gKeyName$, gKey$, gFilename$<br>
res% = WritePPS(gSection$, gKeyName$, gKey$, gFilename$)<br>
Debug.Print res%<br>
<br>
res% provides the return value which indicates whether the write was successful. Note that if the Section or KeyName do not exist, they will be created, so a successful write does not necessarily mean a successful edit. A type could cause the addition of a new key instead of changing the value of an existing one.<br>
<br>
OK, but what about reading the .ini file?<br>
<br>
This,<br>
Private Sub cboSection_click()<br>
txtSection = cboSection.Text '(cboSection.ListIndex)<br>
lstKeyName.Clear<br>
gSection$ = txtSection<br>
txtKeyName = ""<br>
txtKey = ""<br>
gFilename$ = Me.Caption<br>
Debug.Print "Section Name: " & gSection$, "FileName: " & gFilename$<br>
ReDim GetKeyName$(1000)<br>
Dim res%, ind%<br>
res% = GetAppKeyNames%(gSection$, GetKeyName$(), gFilename$)<br>
ReDim Preserve GetKeyName$(res%)<br>
For ind% = 0 To res%<br>
lstKeyName.AddItem GetKeyName$(ind%)<br>
Next ind%<br>
lstKeyName.ListIndex = 0<br>
End Sub<br>
is a subroutine that readsall the KeyNames in a Section and populates a listbox with them - we've gotten into a special case already! - but that can wait for a bit. What we really want is this next one,<br>
Private Sub lstKeyName_click()<br>
'txtSection = "[" & cboSection.Text & "] " & lstKeyName.Text<br>
txtKeyName = lstKeyName.Text '(lstKeyname.ListIndex)<br>
gFilename$ = Me.Caption<br>
txtKey = GetPKeyString$(txtSection, txtKeyName, "no value", 2048, gFilename$)<br>
End Sub<br>
which reads a discrete KeyName from a Section in the .ini file. The '2048' in GetPKeyString sets a large buffer length. If the buffer is set too small, say 10, a Key value that is greater than 10 characters long will be truncated to 10 characters.<br>
<br>
So, now you need to see the GetPKeyString function.<br>
Function GetPKeyString$(ByVal AppName$, ByVal KeyName$, Default$, Size&, Filename$)<br>
'this function retrieves string from private INI & manages string handling tasks<br>
ReturnString$ = Space$(Size&)<br>
ValidLength& = GetPPS(AppName$, KeyName$, Default$, ReturnString$, Size&, Filename$)<br>
GetPKeyString$ = Left$(ReturnString$, ValidLength&)<br>
End Function<br>
<br>
The GetPPS (GetPrivateProfileString) function returns the length of the string stored in ReturnString$. This allows use of Left$ to get rid of any garbage, such as ASCIIZ or Nulls after the returned text value. Remember, the returned buffer is still as large as it was when you set it, with only the left-most spaces having been replaced by the string value being returned.<br>
<br>
That's a quick-n'-dirty explanation, and may leave much to be desired, but I was afraid of running up against some posting limitation. If clarification is required, post here or e-mail me. Guess we need to see if there is an 'archive' space for such code snippets.<br>
<br>
Make a good day .<br> . .
. . . barn<br>