www.pudn.com > DataCollectionSystem.rar > modIniRW.bas, change:2003-07-12,size:3696b


Attribute VB_Name = "modIniRW" 
Option Explicit 
 
Private 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 
    ' 返回所读取的长整型值 
    ' 要读取的段(Section)名称 
    ' 要读取的的键(Key)名称 
    ' 定默认值,如果读取时出错,则返回该值 
    ' 指定要读的 INI 文件名 
 
Private 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 
    ' 返回所读取的字符串值的真实长度 
    ' 要读取的段 (Section) 名称 
    ' 要读取的的键 (Key) 名称 
    ' 指定默认值,如果读取时出错,则返回该值 
    ' 指定接收返回值的字符串变量 
    ' 指定允许字符串值的最大长度 
    ' 指定要读的 INI 文件名 
 
Private 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 
    ' 如果成功返回非 0 值,失败返回 0 
    ' 要写入的段 (Section) 名称 
    ' 要写入的的键 (Key) 名称 
    ' 要写入的值 (Value),以字符串表示 
    ' 指定要写的 INI 文件名 
 
Public Function GetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal DefString As String) As String 
    Dim ResultString As String * 144, Temp As Integer 
    Dim s As String, i As Integer 
    Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, AppProfileName()) 
    '检索关键词的值 
    If Temp% > 0 Then '关键词的值不为空 
    s = "" 
    For i = 1 To 144 
      If Asc(Mid$(ResultString, i, 1)) = 0 Then 
        Exit For 
      Else 
        s = s & Mid$(ResultString, i, 1) 
      End If 
    Next 
    Else 
    Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, AppProfileName()) 
    '将缺省值写入INI文件 
    s = DefString 
    End If 
    GetIniS = s 
End Function 
 
Public Function GetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal defValue As Integer) As Integer 
    Dim d As Long, s As String 
    d = defValue 
    GetIniN = GetPrivateProfileInt(SectionName, KeyWord, defValue, AppProfileName()) 
    If d <> defValue Then 
      s = "" 
      d = WritePrivateProfileString(SectionName, KeyWord, s, AppProfileName()) 
    End If 
End Function 
 
Public Sub SetIniS(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValStr As String) 
    Dim res% 
    res% = WritePrivateProfileString(SectionName, KeyWord, ValStr, AppProfileName()) 
End Sub 
Public Sub SetIniN(ByVal SectionName As String, ByVal KeyWord As String, ByVal ValInt As Integer) 
    Dim res%, s$ 
    s$ = Str$(ValInt) 
    res% = WritePrivateProfileString(SectionName, KeyWord, s$, AppProfileName()) 
End Sub 
 
Public Function AppProfileName() As String 
    AppProfileName = App.Path & "\ProFiles\ProFiles.ini" 
End Function 
 
Public Function WriteINI(sSection As String, sKeyName As String, sNewString As String) As Boolean 
 
On Local Error Resume Next 
 
Call WritePrivateProfileString(sSection, sKeyName, sNewString, AppProfileName()) 
 
WriteINI = (Err = 0) 
 
End Function 
Public Function ReadINI(sSection As String, sKeyName As String) As String 
 
On Local Error Resume Next 
 
Dim sRet As String 
 
sRet = String(255, Chr(0)) 
 
ReadINI = Left(sRet, GetPrivateProfileString(sSection, ByVal sKeyName, "", sRet, Len(sRet), AppProfileName())) 
 
End Function