纠结两天了求助:关于用数组返回GetPrivateProfileSection的问题

nightstar0 2011-03-25 07:07:47
我想写一个类,把操作INI的几个方法都封装进去。其他的都好了。就差读取 GetPrivateProfileSection 这个了。
我的思路是: 传一个自定义类型数组进去,然后也是返回一个 自定义的数组。
public type ini
keyName as string
keyValue as string
end type
ini 文件里存放了 4组数据:
[主号]
aaa=123
bbb=321
ccc=568
ddd=888

前后改了两种思路,都有出错。 很纠结啊。
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

第一种:
' 5. 根据节点名,读出该节点下所有数据
Public Function getSectionIni(ByVal lpAppName As String, ByRef returnKey() As ini, ByVal nSize As Long, ByVal lpFileName As String) As ini()
'Dim iniList() As ini
'Const maxStrLen = 255 '声明个缓冲区长度变量
Dim tempStr As String
Dim strs() As String
'Dim filePath As String
Dim i, j As Long
ReDim returnKey(0)
' 用类型 vbnullchar 填充变量 tempStr 长度为: maxStrLen
tempStr = String(nSize, vbNullChar)
' 获取INI中的数据流

i = GetPrivateProfileSection(lpAppName, tempStr, nSize, lpFileName)
'如果读到数据, 那么 i > 0
If i > 0 Then
'InStrRev 返回一个字符串在另一个字符串中出现的位置,从字符串的末尾算起
'以下是为了找出有多少个 键值对(键名+键值 为一对),从字符串最末位找起
j = InStrRev(tempStr, vbNullChar, i)
' 如果找到到,J有值。则把 J 赋予 I
If j Then i = j

'Left() 函数 返回 Variant (String),其中包含字符串中从左边算起指定数量的字符。
tempStr = Left(tempStr, i - 1)
'MsgBox "LEFT处理后的返回值" & tempStr
'返回一个字符串数组, split()函数 返回一个下标从零开始的一维数组,它包含指定数目的子字符串
strs = Split(tempStr, vbNullChar)
j = 0
' UBound 函数 返回一个 Long 型数据,其值为指定的数组维可用的最大下标 ;比如数组"a","b","c" 则其最大下标为:2
Dim aa As Integer
aa = 0
For i = 0 To UBound(strs)
If InStr(strs(i), "=") Then
'aa = aa + 1

'这个判断的作用是为了 忽略 没有键名的行
ReDim Preserve returnKey(j) '重定义 iniList() 数组的大小
'赋值
'LTrim、RTrim与 Trim 函数
'返回 Variant (String),其中包含指定字符串的拷贝,没有前导空白 (LTrim)、尾随空白 (RTrim) 或前导和尾随空白 (Trim)。

' 键值对如: aa = bb 则: left(aa=bb,instr("=")寻找=号所在的位置 -1) keyName 就是在 =号位置的 左边(-1位置)
returnKey(j).keyName = Trim(Left(strs(i), InStr(strs(i), "=") - 1))
returnKey(j).keyValue = Trim(Mid(strs(i), InStr(strs(i), "=") + 1, Len(strs(i))))
j = j + 1
End If
Next
getSectionIni = returnKey
End If

End Function
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
界面上的操作:
Private Sub Command1_Click()
Dim filePath As String
Dim keyList() As ini
Dim x As Integer

filePath = App.Path & "\userMsg.ini"
'keyList = getSectionIni("主号", 255, filePath)
getSectionIni "主号", keyList, 255, filePath
x = UBound(keyList)
For i = 0 To x
Combo1.AddItem keyList(x).keyName
Combo2.AddItem keyList(x).keyValue
Next
End Sub
这么做的值是读出来了。。可是 重复显示出了4个 最后一个数据。。。。。。 4个列表都是:ddd=888
...全文
316 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nightstar0 2011-03-29
  • 打赏
  • 举报
回复
我这里用代码实现是能读取了
可问题是我现在想把这个功能封装到类里去
用的时候 CALL就好
嗷嗷叫的老马 2011-03-26
  • 打赏
  • 举报
回复
http://www.m5home.com/blog/article.asp?id=541

正好我写了一个读值的过程,哈哈,你组合一下吧.

另外,个人感觉自己用代码实现也是可以的.
nightstar0 2011-03-26
  • 打赏
  • 举报
回复
郁闷。。。。。
参考别人的代码仿写的
是有点乱。。。那些解释是我自己添加的,方便自己解读代码用的
  • 打赏
  • 举报
回复
看到了,你的代码有点乱,家里,没vb。帮顶
nightstar0 2011-03-25
  • 打赏
  • 举报
回复
郁闷
没人看到么???

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧