关于在VB中利用API读取INI文件的问题!!帮主亲自卖大白菜!!80分一个!!

wangqiu 2003-09-12 02:36:44
12.INI文件内容:
[SQL]
Driver=C:\WINNT\System32\sqlsrv32.dll
Setup=aaaa
32Bit=1
代码如下:
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
Private Sub Command1_Click()
Section = "SQL"
key = "Setup"
Dim ReturnStr,readstring As String
Dim ReturnLng As Long
ReadString = vbNullString
ReturnStr = Space(20)
ReturnLng = GetPrivateProfileString(Section,key,vbNullString,ReturnStr, 20, IniFileName)
ReadString = Left(ReturnStr, ReturnLng)
Text1.Text = readstring
End Sub
这只是其中的一部分代码,前提是12.INI是可读的,IniFileName显示是12.INI!!
但returnlng的值是零!!!TEXT1.TEXT的值也是零!!!

请高人帮帮拉!!!
...全文
19 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwzxwz 2003-09-12
  • 打赏
  • 举报
回复
up
learn
lilaclone 2003-09-12
  • 打赏
  • 举报
回复
我也蒙了:(
wangqiu 2003-09-12
  • 打赏
  • 举报
回复
其他都是没有问题的就是到了
ReturnLng = GetPrivateProfileString(Section,key,vbNullString,ReturnStr, 20, IniFileName)的返回值是0,section="sql",key="setup",inifilename="路径 &12.ini"

我蒙了,到底是怎么回事
lilaclone 2003-09-12
  • 打赏
  • 举报
回复
你把我给你的函数写到模块中,然后跟踪调试一下看看,看看文件路径等地方有无出错。

这个函数我在好几个地方都用到了,是没问题的
wangqiu 2003-09-12
  • 打赏
  • 举报
回复
Dim filepath As String
Dim strdriver As String
Dim mth As inifile
Private Sub Command1_Click()
Set mth = New inifile
strdriver = Trim(mth.GetInIKeyValue("SQL", "setup", filepath))
Text1.Text = strdriver
End Sub

Private Sub Form_Load()
filepath = App.Path & "12.ini"
End Sub
我写下了如下代码!!!但返回还是什么也没有呀!!和我那个一样呀
lilaclone 2003-09-12
  • 打赏
  • 举报
回复
不会的,我一直都在用啊,你用你的INI文件试试应该可以的
wangqiu 2003-09-12
  • 打赏
  • 举报
回复
lilaclone(~~阿九~~) ( )你那个类好象有点太简单了吧!!容易出现很多问题的(我认为)
wangqiu 2003-09-12
  • 打赏
  • 举报
回复
up 大家看我那个怎么不行????????

最关键的一句ReturnLng = GetPrivateProfileString(Section,key,vbNullString,ReturnStr, 20, IniFileName)
里面的值一切都正常,section="sql",key="setup",inifilename="12.ini"
他的返回值是0
lilaclone 2003-09-12
  • 打赏
  • 举报
回复
在窗口中使用方法如下,比如说取ini文件中Driver的值

strDriver = Trim(GetInIKeyValue("SQL", "Driver", FilePath))

这样就可以了
lilaclone 2003-09-12
  • 打赏
  • 举报
回复
你的写得也太复杂了吧,我给你个函数调用试试(用于模块中)
Option Explicit
'lpBuffer : buffer for system directory
'uSize : size of directory buffer
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" _
(ByVal lpBuffer As String, ByVal nSize As Long) As Long

'lpAppName : section name
'lpKeyName : key name
'lpDefault : default string
'lpReturnedString : destination buffer
'nSize : size of destination buffer
'lpFileName : initialization file name
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

'lpAppName : section name
'lpKeyName : key name
'lpString : string to add
'lpFileName : initialization file
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
'********************************************************************************
'** 函数功能:从ini配置文件中读取指定段名、关键字名的值
'** 调用语法: GetInIKeyValue(SectionName as string,KeyName As String,FileName As String)
'** 参数说明:
'** SectionName :段名
'** KeyName :关键字名
'** FileName :ini文件名包括路径
'** 返 回 值:
'** String :返回关键字值
'** 处理说明:
'** 调用API函数GetPrivateProfileString
'******************************************************************************
Public Function GetInIKeyValue(ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal FileName As String) As String
Dim KeyValue$
Dim strTmp As String

KeyValue$ = String$(512, " ")
GetPrivateProfileString SectionName, KeyName, "", KeyValue$, 512, FileName
strTmp = Trim(KeyValue$)
GetInIKeyValue = Left(strTmp, Len(strTmp) - 1)
End Function

'********************************************************************************
'** 函数功能:从ini配置文件中写入指定段名、关键字名及值
'** 调用语法: SetInIKeyValue(SectionName as string,KeyName As String,KeyValue as string ,FileName As String)
'** 参数说明:
'** SectionName :段名
'** KeyName :关键字名
'** KeyValue :关键字值
'** FileName :ini文件名包括路径
'** 返 回 值:
'** 处理说明:
'** 调用API函数WritePrivateProfileString
'******************************************************************************
Public Sub SetInIKeyValue(ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal KeyValue As String, _
ByVal FileName As String)
Dim lng As Long

lng = WritePrivateProfileString(SectionName, KeyName, KeyValue, FileName)
End Sub

1,485

社区成员

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

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