在VB中如何调用GetPrivateProfileString涵数?

解决方案 »

  1.   

    '首先申明:
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, ByVal lpRetunedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long    Dim RetStr As String
        Dim l As Long
        
        RetStr = String(255, Chr(0))
        l = GetPrivateProfileString(SectionName, ByVal KeyName, "", RetStr, Len(RetStr), IniFileName)
        retStr= left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", RetStr, Len(RetStr), FileName))
      

  2.   

    利用GetPrivateProfileString函数读出password的值:
      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 key as String*255
      c=GetPrivateProfileString("user","password","false",key,255,"c:\windows\user.ini")
      If key="false" then
      MsgBox"文件不存在或没有该字段"
      Else: Form1.Print"The password is ";key
      该函数将文件user.ini中password的值(即你设定的密码)赋予key,若发生错误(文件不存在或没有该段名)则key的值为“false”,注意一定要声明变量key的长度并与函数中的值一致。这样你就可以将key与登录密码进行对照或直接处理key的值来决定是否继续运行程序。
      使用ini文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。 
      

  3.   

    我用BYREF定义lpReturnString为什么会报错?
      

  4.   

    API浏览器中就是这样定义的啊.Private Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
      

  5.   

    http://www.tech521.com/techData/data/1851.asphttp://www.yesky.com/20011030/202753_5.shtml
      

  6.   

    放到module里
    Option Explicit
    '调用说明定义----------------------------------------------
    Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFileName As String) As Long
    Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    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
    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'调用说明定义结束------------------------------------------'设置或读取INI文件数据
    '---------------------(1)-------------------------------
    Function ReadWriteINI(FileName As String, Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String
    Dim tmpString As String
    Dim secname As String
    Dim keyname As String
    Dim keyvalue As String
    Dim anInt
    Dim defaultkey As String
        On Error GoTo ReadWriteINIError
          '
          ' *** set the return value to OK
          'ReadWriteINI = "OK"
          ' *** test for good data to work with
          If IsNull(Mode) Or Len(Mode) = 0 Then
            ReadWriteINI = "ERROR MODE"    ' Set the return value
            Exit Function
          End If
          If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then
            ReadWriteINI = "ERROR Secname" ' Set the return value
            Exit Function
          End If
          If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then
            ReadWriteINI = "ERROR Keyname" ' Set the return value
            Exit Function
          End If
          ' *** set the ini file name
          'FileName = "C:\sss.ini" ' <<<<< put your file name here
          '
          '
          ' ******* WRITE MODE *************************************
          If UCase(Mode) = "WRITE" Then
                If IsNull(tmpKeyValue) Or Len(tmpKeyValue) = 0 Then
                    ReadWriteINI = "ERROR KeyValue"
                    Exit Function
              Else
              
              secname = tmpSecname
              keyname = tmpKeyname
              keyvalue = tmpKeyValue
              anInt = WritePrivateProfileString(secname, keyname, keyvalue, FileName)
              End If
          End If
          ' *******************************************************
          '
          ' *******  READ MODE *************************************
          If UCase(Mode) = "GET" Then
          
              secname = tmpSecname
              keyname = tmpKeyname
              defaultkey = "Failed"
              keyvalue = String$(50, 32)
              anInt = GetPrivateProfileString(secname, keyname, defaultkey, keyvalue, Len(keyvalue), FileName)
              If Left(keyvalue, 6) <> "Failed" Then        ' *** got it
                 tmpString = keyvalue
                 tmpString = RTrim(tmpString)
                 tmpString = Left(tmpString, Len(tmpString) - 1)
              End If
              ReadWriteINI = tmpString
          End If
        Exit Function
       
      ' *******
    ReadWriteINIError:
       MsgBox Error
       Stop
    End Function