在模块中声明了
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
Dim DbName As String * 255 '数据库名Dim n As Integern = GetPrivateProfileString("database", "isql", "", DbName, Len(DbName), IniFileName)DbName = Left(DbName, n)结果dbname为空而n为0

解决方案 »

  1.   

    Private Declare Function ProfileGetKeyValue Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
            ByVal lpApplicationName As String, _
            ByVal lpszKey As String, _
            ByVal lpszDefault As String, _
            ByVal lpszReturnBuffer As String, _
            ByVal cchReturnBuffer As Long, _
            ByVal lpszFile As String _
            ) As LongPublic Function GetKey _
                (Optional ByVal isKey As String = "", _
                Optional ByVal isSection As String = "") _
                As String
                
    On Error GoTo GetKey_Error    Dim KeyValue                As String
        Dim Characters              As Long
        
        isKey = Trim$(isKey)
        If isKey = "" Then
            isKey = DefaultKey
        End If    isSection = Trim$(isSection)
        If isSection = "" Then
            isSection = DefaultSection
        End If    KeyValue = String$(128, 0)
            
        Characters = ProfileGetKeyValue(isSection, isKey, DEFAULT_KEYVALUE, KeyValue, 127, FileFullName)
        
        If Characters > 0 Then        DefaultSection = isSection
            DefaultKey = isKey
            Characters = InStr(1, KeyValue, Chr(0)) - 1
            KeyValue = Trim$(Left$(KeyValue, Characters))
            
        End If
            
        GetKey = KeyValue
        
        
        GoTo WayOut
    GetKey_Error:
        
    WayOut:
    End Function
      

  2.   


    '****以下是一个读ini文件的函数****'  
     
    Public  Function  GetIniStr(ByVal  FileName  As  String,  _  
                                                       ByVal  Section  As  String,  _  
                                                       ByVal  Key  As  String)  As  String  
           On  Error  Resume  Next  
             
           Dim  strRetVal  As  String  
           Dim  strVer  As  String  
           Dim  strDefValue  As  String  
             
           strDefValue  =  ""  
           GetIniStr  =  strDefValue  
     
           strRetVal  =  String$(255,  0)  
           GetPrivateProfileString  Section,  Key,  strDefValue,  strRetVal,  _  
                                                           Len(strRetVal),  FileName  
           strVer  =  left(strRetVal,  InStr(strRetVal,  Chr(0))  -  1)  
     
           If  strVer  =  ""  Then  
                 GetIniStr  =  strDefValue  
           Else  
                 GetIniStr  =  strVer  
           End  If  
                   
    End  Function  
      

  3.   

    参考: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
    Private Sub Form_Load()    Dim Ret As String, NC As Long    '   Project1 -> Keyname
        WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"    'Create a buffer
        Ret = String(255, 0)
        'Retrieve the string    NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
        'NC is the number of characters copied to the buffer
        If NC <> 0 Then Ret = Left$(Ret, NC)
        'Show our string
        MsgBox RetEnd Sub
      

  4.   


     
    ‘************函数中用到的API函数声明************  
    Private  Declare  Function  GetPrivateProfileString  Lib  "kernel32"  Alias  _  
                                                     "GetPrivateProfileStringA"  _  
                                                     (ByVal  lpApplicationName  As  String,  _  
                                                       ByVal  lpKeyName  As  String,  _  
                                                       ByVal  lpDefault  As  String,  _  
                                                       ByVal  lpReturnedString  As  String,  _  
                                                       ByVal  nSize  As  Long,  _  
                                                       ByVal  lpFileName  As  String)  _  
                                                       As  Long  
    ---------------------------------------------------------------
      

  5.   

    http://expert.csdn.net/Expert/topic/1673/1673799.xml?temp=.3032801
    搜索一下有好多的
      

  6.   

    '模块说明:用于对INI文件的读写操作
    Option Explicit
    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 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文件进行读操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
        GetProfileString = String(255, 0)
        GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
        GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
    End Function'功能简介:对INI文件进行写操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    '参数四:写操作字符串
    Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
        On Error GoTo WriteErr
        WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
        WriteProfilestring = True
        Exit Function
    WriteErr:
    End Function