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 = sEnd Function这是模块中的语句,要看的疯了。TEMP和S分别是什么作用?
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 = sEnd Function这是模块中的语句,要看的疯了。TEMP和S分别是什么作用?
如果不成功,返回0 (一般都是指要取值的Key等参数在文件中不存在会返回0) s 临时保存 从 ini 文件取得的数据. 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 返回的值,先放在ResultString 中 ,但是 ResultString 的值的长度是144 实际长度后面如果没有数据 Asc 值为0因此 用 Asc(Mid$(ResultString, i, 1)) = 0 来判断那一位开始是0s = s & Mid$(ResultString, i, 1) '表示,如果不是0就表示是正确的数据,一直进行累加.至发现0为止.
ResultString 是预先申请的含有144个字符的缓冲区,然而GetPrivateProfileString后,不是这144个字符都会装满,因为一般的键值没有这样长的,没用到的为0,程序根据这一点把有用的字符组合到s,它就是最终要的键值字符串。
Dim s As String, i As Integer
Temp% = GetPrivateProfileString(SectionName, KeyWord, "", ResultString, 144, AppProfileName()) '不管ResultString获得空值还是非空值ResultString的空间都是144个空字符
If Temp% > 0 Then '如果ResultString获得了一个不为空的值
s = ""
For i = 1 To 144 '循环前假如 ResultString ="abcd ef " 循环后 ResultString="abcd ef"
If Asc(Mid$(ResultString, i, 1)) = 0 Then
Exit For
Else
s = s & Mid$(ResultString, i, 1)
End If
Next
Else '如果ResultString=" " 144个空字符
Temp% = WritePrivateProfileString(SectionName, KeyWord, DefString, AppProfileName())
s = DefString
End If
GetIniS = sEnd Function