请教读取ini文件的api函数的使用问题
以下是一个读取ini文件数据的函数,有几个问题请教一下
1.我的变量 strtmp 应该预留多少的字符呀,是应该全部填充空格呀,还是用 ascii码 0 来填充呢?,
关键是预留多少个字符,有没有规定,还是随便设?
2.如果用事先填充的是 null,即ascii码的0,是不是就不需要截尾了.因为这个函数也是网上找的,所以一些地方不明白.当然了,这个函数需要事先声明API函数,我这里省略了.
Public Function GetIniKey(strSection As String, strKey As String) As String On Error GoTo errhandle
Dim strtmp As String
Dim lngRet As String
Dim i As Integer
Dim strTmp2 As String
'先将strtmp定义成1024个字符的长度,保证一定能装下返回的字串
'strtmp = String$(1024, Chr(32))
strtmp = String$(1024, 0) lngRet = GetPrivateProfileString(strSection, strKey, "", strtmp, Len(strtmp), strINI)
'strtmp现在已经是返回的字串了,所以要进行截尾处理
strtmp = Trim(strtmp)
'strTmp2 = ""
'ascii码为0对应的为空字符
' For i = 1 To Len(strtmp)
' If Asc(Mid(strtmp, i, 1)) <> 0 Then
' strTmp2 = strTmp2 + Mid(strtmp, i, 1)
' End If
' Next i
GetIniKey = strtmp
GetIniKey = strtmp2
Exit Function
errhandle:
GetIniKey = ""End Function
以下是一个读取ini文件数据的函数,有几个问题请教一下
1.我的变量 strtmp 应该预留多少的字符呀,是应该全部填充空格呀,还是用 ascii码 0 来填充呢?,
关键是预留多少个字符,有没有规定,还是随便设?
2.如果用事先填充的是 null,即ascii码的0,是不是就不需要截尾了.因为这个函数也是网上找的,所以一些地方不明白.当然了,这个函数需要事先声明API函数,我这里省略了.
Public Function GetIniKey(strSection As String, strKey As String) As String On Error GoTo errhandle
Dim strtmp As String
Dim lngRet As String
Dim i As Integer
Dim strTmp2 As String
'先将strtmp定义成1024个字符的长度,保证一定能装下返回的字串
'strtmp = String$(1024, Chr(32))
strtmp = String$(1024, 0) lngRet = GetPrivateProfileString(strSection, strKey, "", strtmp, Len(strtmp), strINI)
'strtmp现在已经是返回的字串了,所以要进行截尾处理
strtmp = Trim(strtmp)
'strTmp2 = ""
'ascii码为0对应的为空字符
' For i = 1 To Len(strtmp)
' If Asc(Mid(strtmp, i, 1)) <> 0 Then
' strTmp2 = strTmp2 + Mid(strtmp, i, 1)
' End If
' Next i
GetIniKey = strtmp
GetIniKey = strtmp2
Exit Function
errhandle:
GetIniKey = ""End Function
解决方案 »
- 跪求!!! vb用inet控件,以http协议,当字符中出现 & 号或数据过大会被截断,请问大侠如何处理?
- 全局变量-模块bas、类数据cls之间能否转递
- 请教一个activex中的数组问题
- 如何根据路径加载图片
- win98下编的屏保,为何XP下不能正常显示,谢谢!!!!
- 如果将文件内容(文件类型不定)读入一变量中
- 如何通过VB编程来访问局域网里其他机子的注册表??是高手的快来!!
- 这个式子IIf(lC = 0, 0,b /c * 100)的结果是多少??
- DataGrid控件问题:我想在一个绑定了的DataGrid上实现类似pb中的dropdowndatawindow,就是某个字段的值可以取自另外一个表(代码表),详情请看内容:
- 请问VB程序如何在客户端调用服务器上的Active Exe?
- 读取图片
- print问题
2)返回字符串以 \0 字符结尾,必须截尾。如果你不关心 ini 值末尾是否有空格,如下:
strtmp = Trim(Replace(strtmp, Chr(0), " "))
'ascii码为0对应的为空字符
' For i = 1 To Len(strtmp)
' If Asc(Mid(strtmp, i, 1)) <> 0 Then
' strTmp2 = strTmp2 + Mid(strtmp, i, 1)
' End If
' Next i
以前听说过,对于字符串,VB的处理与C语言不一样,所以涉及到字符串结尾标记的问题。
C是以\0做为结束标记,可是VB不是这样
1 无论预先赋值空格还是 Null,实际目的是分配存储空间。只要长度大于等于预期取回的字符串长度即可。如果预设短了,则会取回不完整的字符串。2 函数的作者没有正确理解 API 函数。实际上,不存在他所理解的截尾问题。返回值就是取回的字符数。lngRet = GetPrivateProfileString(strSection, strKey, "", strtmp, Len(strtmp), strINI)strtmp = Left(strtmp, lngRet)
下面第一个函数也是网上找的,第二个是您的函数,
可是我看到第一种写法简洁,没有用循环,可是您的代码用了循环,
我想问,第一种写法对吗?您为何要用循环来处理截尾呢?请再指教。Public Function GetIniKey(strSection As String, strKey As String) As String
On Error GoTo errhandle
Dim strResult As String * 255 '固定255长度
Dim lngRet As Long
lngRet = GetPrivateProfileString(strSection, strKey, "", strResult, Len(strResult), strINI)
GetIniKey = Left(strResult, InStr(strResult, Chr(0)) - 1) Exit Function
errhandle:
GetIniKey = ""
End FunctionFunction 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, IniFileName) 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, IniFileName)
S = DefString
End If
GetIniS = S
End Function
Dim l As Long, i As Long, j As Long
Dim sReturn As String
On Error Resume Next sReturn = Space(200)
If InStr(strFile, "\") = 0 Then
strFile = App.Path & "\" & strFile
End If
l = GetPrivateProfileString(strSection, strKeyName, strDefault, sReturn, 200, strFile)
If l > 0 Then
sReturn = Trim(StrConv(LeftB(StrConv(sReturn, vbFromUnicode), l), vbUnicode))
j = InStr(sReturn, vbTab)
If i > 0 Then
If j > 0 And j < i Then
l = j - 1
Else
l = i - 1
End If
ElseIf j > 0 Then
l = j - 1
End If
If l > 0 Then
sReturn = Trim(Left(sReturn, l))
End If
GetStrVal = sReturn
End If
End Function