改为这样就对了,如果不明白,可以发消息问我,我可以给你解释,不过这是你结贴后的事。
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
Public Function GetString(Name As String) As StringDim User_ID As String
Dim PassWord As String
Dim Source As StringDim strOutput As String
Dim strFileName As String strFileName = "C:\DBConnection.ini"
Dim key As String * 255
User_ID = GetPrivateProfileString(Name, "dbUser", "false", key, 255, strFileName)
If key = "false" Then strOutput = "ID read Error!<p>": key = "true"
User_ID = Left(key, CInt(User_ID))
PassWord = GetPrivateProfileString(Name, "dbPassword", "false", key, 255, strFileName)
If key = "false" Then strOutput = strOutput & "PASS read Error!<p>": key = "true"
PassWord = Left(key, CInt(PassWord))
Source = GetPrivateProfileString(Name, "dbSource", "false", key, 255, strFileName)
If key = "false" Then strOutput = strOutput & "SOURCE read Error!<p>"
Source = Left(key, CInt(Source))
strOutput = User_ID & "," & PassWord & "," & SourceGetString = strOutputEnd Function
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
Public Function GetString(Name As String) As StringDim User_ID As String
Dim PassWord As String
Dim Source As StringDim strOutput As String
Dim strFileName As String strFileName = "C:\DBConnection.ini"
Dim key As String * 255
User_ID = GetPrivateProfileString(Name, "dbUser", "false", key, 255, strFileName)
If key = "false" Then strOutput = "ID read Error!<p>": key = "true"
User_ID = Left(key, CInt(User_ID))
PassWord = GetPrivateProfileString(Name, "dbPassword", "false", key, 255, strFileName)
If key = "false" Then strOutput = strOutput & "PASS read Error!<p>": key = "true"
PassWord = Left(key, CInt(PassWord))
Source = GetPrivateProfileString(Name, "dbSource", "false", key, 255, strFileName)
If key = "false" Then strOutput = strOutput & "SOURCE read Error!<p>"
Source = Left(key, CInt(Source))
strOutput = User_ID & "," & PassWord & "," & SourceGetString = strOutputEnd Function
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
Public 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'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'WriteValue:值
Public Function WriteIni(FileName As String, _
PathName As String, _
KeyName As String, _
WriteValue As String) As Long
Dim Rc As Long
Rc = WritePrivateProfileString(PathName, KeyName, WriteValue, FileName)
WriteIni = Rc
End Function'FileName:Ini文件
'PathName:小节名
'KeyName:值名
'Default:默认字符
Public Function ReadIni(FileName As String, _
PathName As String, _
KeyName As String, _
Optional Default As String = vbNullString, _
Optional DataLen As Long = &H100) As String
Dim Rc As Long
Dim TempStr As String
If DataLen <= 0 Then DataLen = &H100
TempStr = String$(DataLen, Chr$(0))
Rc = GetPrivateProfileString(PathName, KeyName, Default, TempStr, DataLen, FileName)
If Rc > 0 Then
ReadIni = StrConv(LeftB(StrConv(TempStr, vbFromUnicode), Rc), vbUnicode)
Else
ReadIni = Default
End If
End Function
如User_ID = GetPrivateProfileString(Name, "User", "false", key, 255, strFileName)
所以每次取到Key值后,Key左边的User_ID位为用户名。你应该这样写:
//
Dim Leng As Integer, User_ID As String, Temp As String*255
Leng = GetPrivateProfileString(Name, "User", "false", Temp, 255, strFileName)
User_ID = Left(Temp, Leng)
//
就OK了。这个函数与众不同,希望能给其他朋友提供一些帮助。