在VB中如何调用GetPrivateProfileString涵数?

fancy_zhh 2003-10-17 01:24:06
在VB中如何调用GetPrivateProfileString涵数?
...全文
106 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongdong7 2004-02-10
  • 打赏
  • 举报
回复
太好了,,对本人大有帮助..谢谢
easydone 2003-10-19
  • 打赏
  • 举报
回复
放到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
goodname008 2003-10-19
  • 打赏
  • 举报
回复
http://www.tech521.com/techData/data/1851.asp

http://www.yesky.com/20011030/202753_5.shtml
goodname008 2003-10-19
  • 打赏
  • 举报
回复
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
fancy_zhh 2003-10-19
  • 打赏
  • 举报
回复
我用BYREF定义lpReturnString为什么会报错?
li_ping 2003-10-17
  • 打赏
  • 举报
回复
利用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文件存储密码还有一个好处,就是设计者可以建立几个段名来存储不同的密码,从而可实现多用户登录。
UnixXp 2003-10-17
  • 打赏
  • 举报
回复
'首先申明:
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))

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧