一个关于server-u的问题:
已知我的c:\aa.ini文件,已知段(domain1),已知lpstring的一部分(testa),可不可以给出一程序返回lpkeyname(user1)?谢谢大家帮忙
[Domain1]
User1=testa|0|0
User2=testb|1|1
User3=testc|0|1
已知我的c:\aa.ini文件,已知段(domain1),已知lpstring的一部分(testa),可不可以给出一程序返回lpkeyname(user1)?谢谢大家帮忙
[Domain1]
User1=testa|0|0
User2=testb|1|1
User3=testc|0|1
'针对字符串值
'空值表示出错
Public Function GetIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal FileName As String) As String
Dim GetStr As String
On Error GoTo GetIniStrErr
If VBA.Trim(In_Key) = "" Then
GoTo GetIniStrErr
End If
GetStr = VBA.String(128, 0)
GetPrivateProfileString AppName, In_Key, "", GetStr, 256, FileName 'App.Path & "\SourceDB.ini"
GetStr = VBA.Replace(GetStr, VBA.Chr(0), "")
If GetStr = "" Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If
Exit Function
GetIniStrErr:
Err.Clear
GetIniStr = ""
GetStr = ""
End FunctionPublic Function WriteIniStr(ByVal AppName As String, ByVal In_Key As String, ByVal In_Data As String, ByVal FileName As String) As Boolean
On Error GoTo WriteIniStrErr
WriteIniStr = True
If VBA.Trim(In_Key) = "" Or VBA.Trim(AppName) = "" Then
GoTo WriteIniStrErr
Else
WritePrivateProfileString AppName, In_Key, In_Data, FileName
End If
Exit Function
WriteIniStrErr:
Err.Clear
WriteIniStr = False
End Function
我的aa.ini 为
[MyApp]
text1=panjin1
text2=panjin2
[MyApp2]
text3=panjin3我以知panjin1,myapp,怎么返回text1
Dim sTemp As String * 256
Dim nLength As Integer
sTemp = Space$(256)
nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
255, sINIFile)
sGetINI = Left$(sTemp, nLength)
End FunctionPublic Function getUser1() As String
getUser1 = sGetINI("c:\aa.ini", "Domain1", "User1", "")
End Function
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetPrivateProfileStringlong Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As Long, ByVal nSize As Long, ByVal lpFileName As String) As Long
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
Sub Command1_Click()
Dim key() As String
key = getkey("c:\aa.ini", "MyApp", "panjin1")
Dim i As Long
For i = 0 To UBound(key)
Debug.Print key(i)
Next
End Sub'获得指定小节中所有项的列表
Private Function GetKeys(ByVal filename As String, SectionName As String) As Variant
Dim buff() As Byte
Dim i As Long, j As Long
ReDim buff(2047)
Dim buff2() As Byte
Dim s As String
Dim keys() As String
Dim Outs() As String
i = GetPrivateProfileStringlong(SectionName, vbNullString, vbNullString, VarPtr(buff(0)), 2048, filename)
If i > 0 Then
ReDim buff2(i - 1)
CopyMemory buff2(0), buff(0), i
s = StrConv(buff2, vbUnicode)
keys = Split(s, Chr(0))
j = UBound(keys)
ReDim Outs(j - 1)
For i = 0 To j - 1
Outs(i) = keys(i)
Next
GetKeys = Outs
End If
End Function'取得字串值为 keyvalue 的条目名称,至于返回值为数组,是出于不同的条目可能有重复的字串值
Private Function getkey(ByVal filename As String, SectionName As String, ByVal keyvalue As String) As Variant
Dim buff As String
buff = String(256, Chr(0))
Dim Outs() As String
Dim s As String
Dim keys() As String
keys = GetKeys(filename, SectionName)
Dim i As Long
Dim n As Long
n = 0
For i = 0 To UBound(keys)
buff = String(256, Chr(0))
GetPrivateProfileString SectionName, keys(i), vbNullString, buff, 256, filename
buff = Left(buff, InStr(1, buff, Chr(0)) - 1)
If buff = keyvalue Then
ReDim Preserve Outs(n)
Outs(n) = keys(i)
n = n + 1
End If
Next
getkey = Outs
End Function
GetPrivateProfileSection:
【VB声明】
Private 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【别名】
GetPrivateProfileSectionA【说明】
获取指定小节所有项名和值的一个列表
【返回值】
Long,装载到lpReturnedString缓冲区的字符数量。如缓冲区的容量不够大,不能容下所有信息,就返回nSize-2
【备注】
参考对GetPrivateProfileInt函数的注解
【参数表】
lpAppName ------ String,欲获取的小节。注意这个字串不区分大小写
lpReturnedString - String,项和值字串的列表。每个字串都由一个NULL字符分隔,最后一个字串后面用两个NULL字符中止
nSize ---------- Long,lpReturnedString缓冲区的大小。在windows系统中最大值为32767
lpFileName ----- String,初始化文件的名字。如没有指定完整路径名,windows就在Windows目录中查找文件
上面是这个api函数的说明,结合上面我给你的例子,不难写出代码,如果实在不会的话,就给我发送短消息吧:)
Dim sTemp As String * 256
Dim nLength As Integer
sTemp = Space$(256)
nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
255, sINIFile)
sGetINI = Left$(sTemp, nLength)
End Function