prjGetWriteINIFile - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
prjGetWriteINIFile.vbp
Type=Exe Reference=*/G{00020430-0000-0000-C000-000000000046}#2.0#0#C:/WINDOWS/system32/STDOLE2.TLB#OLE Automation Form=frmTest.frm Module=modEnumType; modEnumType.bas Module=modAPI; modAPI.bas Startup="frmTest" Command32="" Name="prjGetWriteINIFile" HelpContextID="0" CompatibleMode="0" MajorVer=1 MinorVer=0 RevisionVer=0 AutoIncrementVer=0 ServerSupportFiles=0 VersionCompanyName="http://blog.csdn.net/HackerJLY" CompilationType=0 OptimizationType=0 FavorPentiumPro(tm)=0 CodeViewDebugInfo=0 NoAliasing=0 BoundsCheck=0 OverflowCheck=0 FlPointCheck=0 FDIVCheck=0 UnroundedFP=0 StartMode=0 Unattended=0 Retained=0 ThreadPerObject=0 MaxNumberOfThreads=1
[MS Transaction Server] AutoRefresh=1
[RVB] DeleteClass1=modPub
modAPI.bas
Attribute VB_Name = "modAPI" Option Explicit
'带Private :读写自己的ini文件 '不带Private :读写Win.ini文件 'String后缀 :读写指定Section和Key的值 'Section后缀 :读写指定Section的内容,此ini文件中没有“key”,只有Section和它的下面的内容,Write的时候,是追加这个值,而且,这个值排在第一位,Get的时候,只会得到第一条记录 'Int后缀 :读写文件的指定Section和Key的Integer值 '--------------------------------------------------------------------------------------------------------------- 'Ini文件事例 ' ; for 16-bit app support '分号:注释 ' ' [drivers] 'Section ' Timer = Timer.drv 'Key = Value ' ' [mci] 'Section ' [driver32] 'Section ' [386enh] 'Section ' woafont = app936.FON 'Key = Value ' EGA80WOA.FON = EGA80WOA.FON 'Key = Value ' EGA40WOA.FON = EGA40WOA.FON 'Key = Value ' CGA80WOA.FON = CGA80WOA.FON 'Key = Value ' CGA40WOA.FON = CGA40WOA.FON 'Key = Value
'--------------------------------------------------------------------------------------------------------------- '---------------------------------------------------------------------------------------------------------------
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _ ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, _ ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long, _ ByVal lpFileName As String _ ) As Long '--------------------------------------------------------------------------------------------------------------- 'The GetPrivateProfileSection function retrieves all of the keys and values for the specified section from an initialization file. This function is provided for compatibility with 16-bit applications written for Windows. Win32-based applications should store initialization information in the registry. Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long 'The GetProfileString function retrieves the string associated with the specified key in the given section of the WIN.INI file. 'This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long 'The GetProfileSection function retrieves all of the keys and values for the specified section of the WIN.INI file. 'This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. Public Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long 'The GetProfileInt function retrieves an integer from the specified key name in the given section of the WIN.INI file. 'This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. Public Declare Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long 'The GetPrivateProfileInt function retrieves an integer associated with a key in the specified section of the given initialization file. 'This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
'--------------------------------------------------------------------------------------------------------------- '---------------------------------------------------------------------------------------------------------------
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _ ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, _ ByVal lpString As Any, _ ByVal lpFileName As String _ ) As Long '--------------------------------------------------------------------------------------------------------------- 'The WritePrivateProfileSection function replaces the keys and values under the specified section in an initialization file. Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long 'The WriteProfileString function copies a string into the specified section of the WIN.INI file. 'This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long 'The WriteProfileSection function replaces the contents of the specified section in the WIN.INI file with the specified keys and values. Public Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
modEnumType.bas
Attribute VB_Name = "modEnumType" Option Explicit
Public Type T_GetPrivateProfileStringParameters strAppName As String strKeyName As String strDefault As String strReturn As String * 128 '此处必须为:128字符定长字符串 lSize As Long strFileName As String End Type
Public Type T_WritePrivateProfileStringParameters strAppName As String strKeyName As String str As String strFileName As String End Type
Public Type T_GetPrivateProfileSectionParameters strAppName As String strReturn As String * 128 '此处必须为:128字符定长字符串 lSize As Long strFileName As String End Type
Public Type T_WritePrivateProfileSectionParameters strAppName As String str As String strFileName As String End Type
Public Type T_GetPrivateProfileInt strAppName As String strKeyName As String lSize As Long strFileName As String End Type
frmTest.frm
VERSION 5.00 Begin VB.Form frmTest Caption = "frmINITest" ClientHeight = 7095 ClientLeft = 60 ClientTop = 345 ClientWidth = 9135 LinkTopic = "Form1" LockControls = -1 'True ScaleHeight = 7095 ScaleWidth = 9135 StartUpPosition = 3 '窗口缺省 Begin VB.Frame Frame5 Caption = "读写Win.ini文件专用" Height = 3375 Left = 5280 TabIndex = 22 Top = 3600 Width = 3855 Begin VB.CommandButton cmdGetProfileString Caption = "GetProfileString" Height = 495 Left = 240 TabIndex = 27 Top = 360 Width = 3375 End Begin VB.CommandButton cmdWriteProfileString Caption = "WriteProfileString" Height = 495 Left = 240 TabIndex = 26 Top = 960 Width = 3375 End Begin VB.CommandButton cmdGetProfileSection Caption = "GetProfileSection" Height = 495 Left = 240 TabIndex = 25 Top = 1560 Width = 3375 End Begin VB.CommandButton cmdWriteProfileSection Caption = "WriteProfileSection" Height = 495 Left = 240 TabIndex = 24 Top = 2160 Width = 3375 End Begin VB.CommandButton cmdGetProfileInt Caption = "GetProfileInt" Height = 495 Left = 240 TabIndex = 23 Top = 2760 Width = 3375 End End Begin VB.Frame Frame4 Caption = "读写自己的ini文件" Height = 3375 Left = 5280 TabIndex = 17 Top = 120 Width = 3855 Begin VB.CommandButton cmdGetPrivateProfileInt Caption = "GetPrivateProfileInt" Height = 495 Left = 240 TabIndex = 28 Top = 2760 Width = 3375 End Begin VB.CommandButton cmdGetPrivateProfileString Caption = "GetPrivateProfileString" Height = 495 Left = 240 TabIndex = 21 Top = 360 Width = 3375 End Begin VB.CommandButton cmdWritePrivateProfileString Caption = "WritePrivateProfileString" Height = 495 Left = 240 TabIndex = 20 Top = 960 Width = 3375 End Begin VB.CommandButton cmdGetPrivateProfileSection Caption = "GetPrivateProfileSection" Height = 495 Left = 240 TabIndex = 19 Top = 1560 Width = 3375 End Begin VB.CommandButton cmdWritePrivateProfileSection Caption = "WritePrivateProfileSection" Height = 495 Left = 240 TabIndex = 18 Top = 2160 Width = 3375 End End Begin VB.Frame Frame3 Caption = "WriteINIParameters" Height = 975 Left = 240 TabIndex = 2 Top = 5400 Width = 4815 Begin VB.TextBox txtWstr Height = 375 Left = 1320 TabIndex = 8 Text = "Hello" Top = 360 Width = 3135 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "str" Height = 180 Index = 6 Left = 120 TabIndex = 7 Top = 480 Width = 270 End End Begin VB.Frame Frame2 Caption = "GetINIParameters" Height = 3015 Left = 240 TabIndex = 1 Top = 2160 Width = 4815 Begin VB.TextBox txtGDefault Height = 375 Left = 1320 TabIndex = 11 Text = "DefaultValue" Top = 240 Width = 3135 End Begin VB.TextBox txtGSize Height = 375 Left = 1320 TabIndex = 10 Text = "128" Top = 720 Width = 3135 End Begin VB.TextBox txtGReturn Height = 1695 Left = 1320 Locked = -1 'True MultiLine = -1 'True ScrollBars = 3 'Both TabIndex = 9 Top = 1200 Width = 3135 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "strDefault" Height = 180 Index = 3 Left = 120 TabIndex = 14 Top = 360 Width = 900 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "lSize" Height = 180 Index = 4 Left = 120 TabIndex = 13 Top = 840 Width = 450 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "strReturn" Height = 180 Index = 5 Left = 120 TabIndex = 12 Top = 1320 Width = 810 End End Begin VB.Frame Frame1 Caption = "PublicParameters" Height = 1815 Left = 240 TabIndex = 0 Top = 120 Width = 4815 Begin VB.TextBox txtGKeyName Height = 375 Left = 1320 TabIndex = 15 Text = "AppTitleID" Top = 1200 Width = 3135 End Begin VB.TextBox txtFileName Height = 375 Left = 1320 TabIndex = 6 Text = "Test.ini" Top = 720 Width = 3135 End Begin VB.TextBox txtAppName Height = 375 Left = 1320 TabIndex = 3 Text = "prjGetWriteINIFile" Top = 240 Width = 3135 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "varKeyName" Height = 180 Index = 2 Left = 120 TabIndex = 16 Top = 1320 Width = 900 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "strFileName" Height = 180 Index = 1 Left = 120 TabIndex = 5 Top = 840 Width = 990 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "strAppName" Height = 180 Index = 0 Left = 120 TabIndex = 4 Top = 360 Width = 900 End End End Attribute VB_Name = "frmTest" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit
Dim utGetPrivateProfileStringParameters As T_GetPrivateProfileStringParameters Dim utWritePrivateProfileStringParameters As T_WritePrivateProfileStringParameters Dim utGetPrivateProfileSectionParameters As T_GetPrivateProfileSectionParameters Dim utWritePrivateProfileSectionParameters As T_WritePrivateProfileSectionParameters
Private Sub cmdGetPrivateProfileSection_Click() Dim lReturn As Long With utGetPrivateProfileSectionParameters .strAppName = Trim(Me.txtAppName.Text) .lSize = CLng(Trim(Me.txtGSize.Text)) .strReturn = "" If Right(App.Path, 1) = "/" Then .strFileName = App.Path & Trim(Me.txtFileName.Text) Else .strFileName = App.Path & "/" & Trim(Me.txtFileName.Text) End If
lReturn = GetPrivateProfileSection(.strAppName, .strReturn, .lSize, .strFileName) Me.txtGReturn.Text = .strReturn End With End Sub
Private Sub cmdGetPrivateProfileString_Click() Dim lReturn As Long With utGetPrivateProfileStringParameters .strAppName = Trim(Me.txtAppName.Text) .strKeyName = Trim(Me.txtGKeyName.Text) .strDefault = Trim(Me.txtGDefault.Text) .lSize = CLng(Trim(Me.txtGSize.Text)) If Right(App.Path, 1) = "/" Then .strFileName = App.Path & Trim(Me.txtFileName.Text) Else .strFileName = App.Path & "/" & Trim(Me.txtFileName.Text) End If Debug.Print .lSize & vbCrLf & .strAppName & vbCrLf & .strDefault & vbCrLf & .strFileName & vbCrLf & .strKeyName & vbCrLf & .strReturn lReturn = GetPrivateProfileString(.strAppName, .strKeyName, .strDefault, .strReturn, .lSize, .strFileName) Me.txtGReturn.Text = .strReturn End With End Sub
Private Sub cmdWritePrivateProfileSection_Click() Dim lReturn As Long With utWritePrivateProfileSectionParameters .strAppName = Trim(Me.txtAppName.Text) .str = Trim(Me.txtWstr.Text) & Time If Right(App.Path, 1) = "/" Then .strFileName = App.Path & Trim(Me.txtFileName.Text) Else .strFileName = App.Path & "/" & Trim(Me.txtFileName.Text) End If WritePrivateProfileSection .strAppName, .str, .strFileName End With End Sub
Private Sub cmdWritePrivateProfileString_Click() Dim bReturn As Boolean With utWritePrivateProfileStringParameters .strAppName = Trim(Me.txtAppName.Text) .str = Trim(Me.txtWstr.Text) & Time .strKeyName = Trim(Me.txtGKeyName.Text) If Right(App.Path, 1) = "/" Then .strFileName = App.Path & Trim(Me.txtFileName.Text) Else .strFileName = App.Path & "/" & Trim(Me.txtFileName.Text) End If Debug.Print .str & vbCrLf & .strAppName & vbCrLf & .strFileName bReturn = WritePrivateProfileString(.strAppName, .strKeyName, .str, .strFileName) If bReturn = False Then MsgBox "WriteINI Faild !!!" End If End With End Sub