Option Explicit
'Form level code.
'Just put a 3 command button array (cmdPPStruct) on a form.
'Note: YOU should add code to check the return values of the calls for errors :-)
Private Declare Function WritePrivateProfileStruct Lib "kernel32.dll" Alias "WritePrivateProfileStructA" _
(ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
ByVal uSizeStruct As Long, ByVal szFile As String) As Long
Private Declare Function GetPrivateProfileStruct Lib "kernel32.dll" Alias "GetPrivateProfileStructA" _
(ByVal lpszSection As String, ByVal lpszKey As String, lpStruct As Any, _
ByVal uSizeStruct As Long, ByVal szFile As String) As Long
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" _
(ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Type MyUDT
Username As String * 20
Password As String * 20
End Type
Private Sub cmdPPStruct_Click(index As Integer)
Dim lngRetval As Long
Dim strUDT As String
Dim MyNewUDT As MyUDT
Select Case index
Case 0 'Write it...
MyNewUDT.Username = "MySillyUserName"
MyNewUDT.Password = "MyStrangePassword"
Call WritePrivateProfileStruct("WritePrivateProfileStruct", "Key", _
MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
Case 1 'Read it...
'No need to erase MyNewUDT. It's recreated whenever cmdPPStruct is clicked...
Call GetPrivateProfileStruct("WritePrivateProfileStruct", "Key", _
MyNewUDT, Len(MyNewUDT), App.Path & "\INITest.ini")
strUDT = MyNewUDT.Username & vbCrLf & MyNewUDT.Password & vbCrLf
'Display it...
MsgBox strUDT
Case 2 'Delete the section...
Call WritePrivateProfileSection("WritePrivateProfileStruct", vbNullString, App.Path & "\INITest.ini")
End Select
End Sub