一个关于server-u的问题:
已知我的c:\aa.ini文件,已知段(domain1),已知lpstring的一部分(testa),可不可以给出一程序返回lpkeyname(user1)?谢谢大家帮忙
[Domain1]
User1=testa|0|0
User2=testb|1|1
User3=testc|0|1

解决方案 »

  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 LongPrivate 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文件,不固定节点,in_key为写入/读取的主键
    '针对字符串值
    '空值表示出错
    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
      

  2.   

    怎么运行呀,
    我的aa.ini 为
    [MyApp]
    text1=panjin1
    text2=panjin2
    [MyApp2]
    text3=panjin3我以知panjin1,myapp,怎么返回text1
      

  3.   

    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 LongPublic Function sGetINI(sINIFile As String, sSection As String, sKey As String, sDefault As String) As String
     
        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
      

  4.   

    思路很简单,就是调用GetPrivateProfileString这个api函数解决,在第一次调用时,将lpKeyName参数设为vbnullstring,就可以在lpReturnedString缓冲区内装载指定小节所有项的列表。注意GetPrivateProfileStringlong这个函数的声明,是我将GetPrivateProfileString这个函数的lpReturnedString参数声明改为了ByVal lpReturnedString As Long,里面有一些小的技巧,你自己体会一下吧,另外,没有加错误处理,你自己加上吧:Option Explicit
    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
      

  5.   

    其实,解决这个问题,比较省事的方案是调用GetPrivateProfileSection这个api函数解决:
    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函数的说明,结合上面我给你的例子,不难写出代码,如果实在不会的话,就给我发送短消息吧:)
      

  6.   

    Public Function sGetINI(sINIFile As String, sSection As String, sKey As String, sDefault As String) As String
     
        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