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 LongPrivate Sub Command1_Click()
Dim success As Long
success = WritePrivateProfileString("MyApp", "text1", Text1.Text, "c:\aa.ini")
'叁数一 Section Name
'叁数二 於.ini中的项目
'叁数三 项目的内容
'叁数四 .ini文件的名称
success = WritePrivateProfileString("MyApp", "text2", Text2.Text, "c:\aa.ini")
success = WritePrivateProfileString("MyApp2", "text3", Text3.Text, "c:\aa.ini")
End SubPrivate Sub Form_load()
Dim ret As Long
Dim buff As String
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text1", "text1", buff, 256, "c:\aa.ini")'若.ini MyApp中无text1,则采用叁数三的值
Text1.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
Text2.Text = buff
buff = String(255, 0)
ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
Text3.Text = buff
End Sub我想问buff = String(255, 0)是什么意思呀
还有想取得lpkeyname的值,怎么做呢?

解决方案 »

  1.   

    String 函数
          返回 Variant (String),其中包含指定长度重复字符的字符串。语法String(number, character)String 函数的语法有下面的命名参数:部分 说明 
    number 必要参数;Long。返回的字符串长度。如果 number 包含 Null,将返回 Null。 
    character 必要参数;Variant。为指定字符的字符码或字符串表达式,其第一个字符将用于建立返回的字符串。如果 character 包含 Null,就会返回 Null。 
    说明如果指定 character 的数值大于 255,String 会按下面的公式将其转为有效的字符码:character Mod 256
      

  2.   

    我还有看不太懂呀, 能再说详细点吗?buff = String(255, 0),在这里有什么用呢?
      

  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 Long
    的lpReturnedString的参数要求一个字符串指针 ,它的大小有nSize指出,由于
    vb中的string类型变量的长度是由赋值决定的,所以用
    buff=string(255,0) ,得到一个长度为255的字符串,它的每个字符都是 0
      

  4.   

    谁能给我一个读.ini中 lpapplicationname,lpkeyname,lpstring的例子,嘀水之嗯,涌泉想报!!!!
      

  5.   

    Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (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
    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
    Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic ErrorMsg As String
    Public IniFileName As StringPrivate Sub Class_Initialize()
        IniFileName = vbNullString
        ErrorMsg = vbNullString
    End SubPublic Sub SpecifyIni(FilePathName)
        IniFileName = Trim(FilePathName)
    End SubPrivate Function NoIniFile() As Boolean
        NoIniFile = True
        If IniFileName = vbNullString Then
            ErrorMsg = "没有指定 INI 文件"
            Exit Function
        End If
        ErrorMsg = vbNullString
        NoIniFile = False
    End FunctionPublic Function WriteString(Section As String, key As String, Value As String) As Boolean
        WriteString = False
        If NoIniFile() Then
            Exit Function
        End If
        If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
            ErrorMsg = "写入失败"
            Exit Function
        End If
        WriteString = True
    End FunctionPublic Function ReadString(Section As String, key As String, Size As Long) As String
        Dim ReturnStr As String
        Dim ReturnLng As Long
        ReadString = vbNullString
        If NoIniFile() Then
            Exit Function
        End If
        ReturnStr = Space(Size)
        ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, IniFileName)
        ReadString = Left(ReturnStr, ReturnLng)
    End FunctionPublic Function ReadInt(Section As String, key As String) As Long
        Dim ReturnLng As Long
        ReadInt = 0
        ReturnLng = GetPrivateProfileInt(Section, key, 0, IniFileName)
        If ReturnLng = 0 Then
            ReturnLng = GetPrivateProfileInt(Section, key, 1, IniFileName)
            If ReturnLng = 1 Then
                ErrorMsg = "不能读取"
                Exit Function
            End If
        End If
        ReadInt = ReturnLng
    End Function
      

  6.   

    不对啊,我加了个
    Private Sub Command1_Click()
    Call ReadString("MyApp", "test1", 100)
    End Sub再把
    Public Function ReadString(Section As String, key As String, Size As Long) As String
        Dim ReturnStr As String
        Dim ReturnLng As Long
        ReadString = vbNullString
        If NoIniFile() Then
            Exit Function
        End If
        ReturnStr = Space(Size)
        ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, "c:\aa.ini")
        ReadString = Left(ReturnStr, ReturnLng)
        MsgBox (ReadString)
    End Function改成了c:\aa.ini
    为什么没有msgbog呢?还想问下
    Public Function ReadString(Section As String, key As String, Size As Long) As String    第三个参数是干什么用的呢?
    我c盘根目录下的aa.ini文件内容为
    [MyApp]
    text1=panjin1
    text2=panjin2
    [MyApp2]
    text3=panjin3
      

  7.   

    const MODULENAME as String ="模块名"'*************************************************************
    'Get INI File KeyValue
    '   INPUT:
    '       p_strFile               ---- FileName
    '       p_strSection            ---- Section
    '       p_strKey                ---- Key
    '       p_blnAllowEmpty         ---- Allow Empty  Default:True
    '   ReturnValue:
    '       Success=KeyValue        Fail=""
    '           Create  2004-08-02 Lu BingLin
    '*************************************************************
    Public Function f_ReadIniKeyValue(ByVal p_strFile As String, ByVal p_strSection As String, _
        ByVal p_strKey As String, Optional ByVal p_blnAllowEmpty As Boolean = True) As String
        
        'Variables
        Dim strTxt              As String * 200
        Dim strValue            As String
        Dim strErrMsg           As String       'Error Message
        Dim strModuleName       As String
        
        strModuleName = MODULENAME & ".f_ReadIniKeyValue" 'Module Name
        
    On Error GoTo ErrReadIniKeyValue    ' Section
        GetPrivateProfileString p_strSection, p_strKey, "", strTxt, 200, p_strFile    'Delete NULL
        strValue = Trim(strTxt)
        strValue = Replace(strValue, Chr(0), "")
        
        If strValue = "" Then
            If p_blnAllowEmpty = False Then
                MsgBox "Lost [" & p_strSection & "]-'" & p_strKey & "' in " & p_strFile, vbCritical, MODULENAME & " Error"
            End If
        End If
        
        'Rnturn Value
        f_ReadIniKeyValue = strValue
            
        Exit Function
        
    ErrReadIniKeyValue:
        
        strErrMsg = "Program ID : " & g_strProgramID & Chr(13) & _
                  "Module Name : " & strModuleName & Chr(13) & _
                  "Error Message : " & Trim(Error(Err)) & Chr(13) & _
                  "Comment : "
        MsgBox strErrMsg, vbCritical, strModuleName & " Error"
        
        'Abnormal Exit
        f_ReadIniKeyValue = ""
        
    End Function'*************************************************************
    'Write INI File KeyValue
    '   INPUT:
    '       p_strFile               ---- FileName
    '       p_strSection            ---- Section
    '       p_strKey                ---- Key
    '       p_strValue              ---- New Key Value
    '   ReturnValue:
    '       Success=Rrue            Fail=False
    '           Create  2004-08-02 Lu BingLin
    '*************************************************************
    Public Function f_WriteIniKeyValue(ByVal p_strFile As String, ByVal p_strSection As String, _
        ByVal p_strKey As String, ByVal p_strValue As String) As Boolean
        
        'Variables
        Dim intRnt              As Integer
        Dim strErrMsg           As String
        Dim strModuleName       As String
        
        strModuleName = MODULENAME & ".f_WriteIniKeyValue"
        
    On Error GoTo ErrWriteIniKeyValue
        'Section
        intRnt = WritePrivateProfileString(p_strSection, p_strKey, p_strValue, p_strFile)
        
        'Rnturn Value
        If intRnt = 0 Then
            f_WriteIniKeyValue = False
        Else
            f_WriteIniKeyValue = True
        End If
        
        Exit Function
        
    ErrWriteIniKeyValue:
        
        strErrMsg = "Program ID : " & g_strProgramID & Chr(13) & _
                  "Module Name : " & strModuleName & Chr(13) & _
                  "Error Message : " & Trim(Error(Err)) & Chr(13) & _
                  "Comment : "
        MsgBox strErrMsg, vbCritical, strModuleName & " Error"
        
        'Abnormal Exit
        f_WriteIniKeyValue = False
    End Function
      

  8.   

    哦,忘了还有一段代码,你要把上面的一段程序作为一个类模块,然后定义
    Public ReadIniFile As CIniFileFunction ReadConfig(Section As String, key As String, Value As Long) As String
        Set ReadIniFile = New CIniFile
        ReadIniFile.SpecifyIni CurrentPath & "aa.ini"
        ReadConfig = ReadIniFile.ReadString(Section, key, Value)
        Set ReadIniFile = Nothing
    End Function然后只要调用ReadConfig函数就可以了
      

  9.   

    例如:ReadConfig("MyApp", "Text1", "50"
      

  10.   

    怎么用上面那段呀, 我定义在了一个普通模块里, 但是我不太清楚
    Function ReadConfig(Section As String, key As String, Value As Long) As String
        Set ReadIniFile = New CIniFile
        ReadIniFile.SpecifyIni CurrentPath & "aa.ini"
        ReadConfig = ReadIniFile.ReadString(Section, key, Value)
        Set ReadIniFile = Nothing
    End Function这块在哪定义呀
      

  11.   

    要在类模块中定义,下面的
    Public ReadIniFile As CIniFileFunction ReadConfig(Section As String, key As String, Value As Long) As String
        Set ReadIniFile = New CIniFile
        ReadIniFile.SpecifyIni CurrentPath & "aa.ini"
        ReadConfig = ReadIniFile.ReadString(Section, key, Value)
        Set ReadIniFile = Nothing
    End Function
    放在普通模块中
      

  12.   

    1、我想问buff = String(255, 0)是什么意思呀?
    答:在你的程序中是开辟一段内存空间,用来存储GetPrivateProfileString 和WritePrivateProfileString 返回的内容。而用ASC码为“0”的字符是为了传给Text1.Text实际的内容。也就是说:'若.ini MyApp中无text1,则采用叁数三的值
    Text1.Text = buff
    buff = String(255, 0) '开辟空间
    ret = GetPrivateProfileString("Myapp", "text2", "text2", buff, 256, "c:\aa.ini")
    Text2.Text = buff
    buff = String(255, 0) ’清空上边GET的内容
    ret = GetPrivateProfileString("Myapp2", "text3", "text3", buff, 256, "c:\aa.ini")
    Text3.Text = buff
    End Sub在这里其实用不着第二次再 buff = String(255, 0),因为返回的字符串是以 chr(0) 为结束的(这个chr(0)是C语言字符串的结束符'\0',因为这两个函数都是用C编写的),当Text3.Text = buff时,VB遇到第一个chr(0)后,就知道字符串结束了。
    这里第二次再 buff = String(255, 0)是因为:比如第一次GET的内容为"ABCDEF",则buff的内容为"ABCDEF000..."(0代表chr(0)),第二次GET的内容"ZZ"少于第一次GET的内容时,就会出现buff为"ZZ0DEF000...",这时buff用在其它地方时,不小心的话会出错,所以第二次再 buff = String(255, 0)后,第二次GET的buff为"ZZ0000000..."2、还有想取得lpkeyname的值,怎么做呢?
    答:看楼上几位的了。
      

  13.   

    去找一个ini的类库,有很多的....