midnightexpress
asked on
Ini Read and Ini Write
I am trying to get the following two functions to work within VB V5.0.
A) GetPrivateProfileString
B) WritePrivateProfileString
Right now I am working with the "GetPrivateProfileString", and have no success at it. Yes, I have searched EE under the text 'GetPrivateProfileString' and picked off the second best VB example, and tried to implement that idea into my VB program. And still no success. Currently I am running under Win95-Vers B. I am not into the Registry bit, and that is why I using the INI file. I believe, before I upgraded to Win95-Version B, I was running under Win95-Version A and I was using these two function within a VC++ DLL file that I put together - and it is still working well. I haven't rebuilt the DLL File since my system upgrade. Am I going to run into some trouble with my VC++ DLL functions with Version B?
But any rate, can get a copy of examples of these functions to work. I'll enclose my function code below...
Function GetSysIni(ByVal section As String, ByVal skey As String) As String
Dim retVal As String
Dim defstrg As String
Dim AppName As String
Dim KeyName As String
Dim worked As Long
Dim lnsize As Long
Dim filedir As String
Dim WinPath As String
'' 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
AppName = section
KeyName = skey
WinPath = WindowsDirectory()
filedir = UCase(WinPath & "\" & "System.ini")
retVal = ""
lnsize = 255
defstrg = Chr(0)
' lpApplicationName, lpKeyName, lpDefault, lpRetunedString, nSize, lpFileName)
worked = GetPrivateProfileString(Ap pName, KeyName, defstrg, retVal, lnsize, filedir)
If worked = 0 Then
GetSysIni = "unknown"
Else
GetSysIni = Left(retVal, InStr(retVal, Chr(0)) - 1)
End If
End Function
Are they any drivers that I need to install to make it work??
See what you can do...
Thanks in Advance...
Midnightexpress
A) GetPrivateProfileString
B) WritePrivateProfileString
Right now I am working with the "GetPrivateProfileString",
But any rate, can get a copy of examples of these functions to work. I'll enclose my function code below...
Function GetSysIni(ByVal section As String, ByVal skey As String) As String
Dim retVal As String
Dim defstrg As String
Dim AppName As String
Dim KeyName As String
Dim worked As Long
Dim lnsize As Long
Dim filedir As String
Dim WinPath As String
'' 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
AppName = section
KeyName = skey
WinPath = WindowsDirectory()
filedir = UCase(WinPath & "\" & "System.ini")
retVal = ""
lnsize = 255
defstrg = Chr(0)
' lpApplicationName, lpKeyName, lpDefault, lpRetunedString, nSize, lpFileName)
worked = GetPrivateProfileString(Ap
If worked = 0 Then
GetSysIni = "unknown"
Else
GetSysIni = Left(retVal, InStr(retVal, Chr(0)) - 1)
End If
End Function
Are they any drivers that I need to install to make it work??
See what you can do...
Thanks in Advance...
Midnightexpress
Here's a bit of code you can use:
WriteProfile is straight forward. GetProfile is a bit more coded. Hope this helps.
-start-
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
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString A" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Const ENUM_BUF_SIZE As Integer = 80
Private Function GetProfile(sSection As String, sKey As String, sIniPath) As String
Dim retLen As Long
Dim buf As String * ENUM_BUF_SIZE
If sKey <> "" Then
retLen = GetPrivateProfileString(sS ection, sKey, "", buf, ENUM_BUF_SIZE, sIniPath)
Else
retLen = GetPrivateProfileString(sS ection, 0&, "", buf, ENUM_BUF_SIZE, sIniPath)
End If
If retLen = 0 Then
GetProfile = ""
Else
GetProfile = Left$(buf, retLen)
End If
End Function
Sub SomeSub()
Dim nResult As Integer
nResult = WritePrivateProfileString( sSection, sEntry, sValue, sIniPath)
If nResult = 0 Then
'Some error - Use GetLastError
End If
End Sub
WriteProfile is straight forward. GetProfile is a bit more coded. Hope this helps.
-start-
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString
Private Const ENUM_BUF_SIZE As Integer = 80
Private Function GetProfile(sSection As String, sKey As String, sIniPath) As String
Dim retLen As Long
Dim buf As String * ENUM_BUF_SIZE
If sKey <> "" Then
retLen = GetPrivateProfileString(sS
Else
retLen = GetPrivateProfileString(sS
End If
If retLen = 0 Then
GetProfile = ""
Else
GetProfile = Left$(buf, retLen)
End If
End Function
Sub SomeSub()
Dim nResult As Integer
nResult = WritePrivateProfileString(
If nResult = 0 Then
'Some error - Use GetLastError
End If
End Sub
The answer to your problem is to NOT write calls to the functions all the time - rather cxreate a wrapper so you can make a call to something that you know works; all the time..
Here it is - these two functions,
ReadIni and WriteIni are wrappers that I have used for the last four years - upgrading as I went - they work...
Copy all this code to a code module, and then just make calls to the wrappers...
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 Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString A" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Function ReadIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, Optional bInteger As Variant, Optional vDefault As Variant) As Variant
'========================= ========== ========== ====
' Copyright © 1998 Australian Custom Software Pty Ltd
'------------------------- ---------- ---------- ----
'Author : Tony Harris
'Created :
'Precis : Wrapper around the ini file API to read
' in from a a private ini file.
' Boolean declares whether it is an Integer or not
' vDefault as the default value
'------------------------- ---------- ---------- ----
'Modified :
'========================= ========== ========== ====
Dim bValid As Boolean
Dim bFileFound As Boolean
Dim BString As Variant
Dim sReturn As String
Dim sDefault As String
Dim nReturn As Long
Dim nDefault As Long
Dim nSize As Long
On Error GoTo ReadErr
bValid = True
bFileFound = True
BString = True
If Len(Trim(fileName)) = 0 Then bValid = False
If Len(Dir(fileName)) = 0 Then
bValid = False
bFileFound = False
End If
If Len(Trim(Section)) = 0 Then bValid = False
If Len(Trim(KeyName)) = 0 Then bValid = False
If IsMissing(bInteger) Then
BString = True
Else
BString = Not (CBool(bInteger))
End If
StartRead:
If bValid Then
If Not BString Then
If IsMissing(vDefault) Then
nDefault = 0
Else
nDefault = CInt(vDefault)
End If
nReturn = GetPrivateProfileInt(Secti on, KeyName, nDefault, fileName)
ReadIni = nReturn
Else
If IsMissing(vDefault) Then
sDefault = ""
Else
sDefault = CStr(vDefault)
End If
sReturn = Space(255)
nSize = Len(sReturn)
nReturn = GetPrivateProfileString(Se ction, KeyName, sDefault, sReturn, nSize, fileName)
If nReturn > 0 Then
sReturn = Left(sReturn, nReturn)
Else
sReturn = sDefault
End If
ReadIni = sReturn
End If
Else
If bFileFound Then
MsgBox " some appropriate message"
ReadIni = "Error"
Else
MsgBox "some other appropriate message"
ReadIni = "Error"
End If
End If
On Error GoTo 0
Exit Function
ReadErr:
Select Case Err.Number
Case 71 'Disk Not ready
bFileFound = False
bValid = False
Resume StartRead
Case Else
MsgBox Err, vbOKOnly, "Error in Reading INI File"
End Select
End Function
Public Sub WriteIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, ByVal sEntry As String)
'========================= ========== ========== ====
' Copyright © 1998 Australian Custom Software Pty Ltd
'------------------------- ---------- ---------- ----
'Author : Tony Harris
'Created :
'Precis : Wrapper around the ini file API to write
' out to a private ini file.
'------------------------- ---------- ---------- ----
'Modified :
'========================= ========== ========== ====
Dim bValid As Boolean
Dim nReturnLen As Long
bValid = True
If Len(Trim(fileName)) = 0 Then bValid = False
If Len(Trim(Section)) = 0 Then bValid = False
If Len(Trim(KeyName)) = 0 Then bValid = False
If Len(Trim(sEntry)) = 0 Then sEntry = ""
If bValid Then
nReturnLen = WritePrivateProfileString( Section, KeyName, sEntry, fileName)
Else
MsgBox "some appropriate error message"
End If
End Sub
Here it is - these two functions,
ReadIni and WriteIni are wrappers that I have used for the last four years - upgrading as I went - they work...
Copy all this code to a code module, and then just make calls to the wrappers...
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Function ReadIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, Optional bInteger As Variant, Optional vDefault As Variant) As Variant
'=========================
' Copyright © 1998 Australian Custom Software Pty Ltd
'-------------------------
'Author : Tony Harris
'Created :
'Precis : Wrapper around the ini file API to read
' in from a a private ini file.
' Boolean declares whether it is an Integer or not
' vDefault as the default value
'-------------------------
'Modified :
'=========================
Dim bValid As Boolean
Dim bFileFound As Boolean
Dim BString As Variant
Dim sReturn As String
Dim sDefault As String
Dim nReturn As Long
Dim nDefault As Long
Dim nSize As Long
On Error GoTo ReadErr
bValid = True
bFileFound = True
BString = True
If Len(Trim(fileName)) = 0 Then bValid = False
If Len(Dir(fileName)) = 0 Then
bValid = False
bFileFound = False
End If
If Len(Trim(Section)) = 0 Then bValid = False
If Len(Trim(KeyName)) = 0 Then bValid = False
If IsMissing(bInteger) Then
BString = True
Else
BString = Not (CBool(bInteger))
End If
StartRead:
If bValid Then
If Not BString Then
If IsMissing(vDefault) Then
nDefault = 0
Else
nDefault = CInt(vDefault)
End If
nReturn = GetPrivateProfileInt(Secti
ReadIni = nReturn
Else
If IsMissing(vDefault) Then
sDefault = ""
Else
sDefault = CStr(vDefault)
End If
sReturn = Space(255)
nSize = Len(sReturn)
nReturn = GetPrivateProfileString(Se
If nReturn > 0 Then
sReturn = Left(sReturn, nReturn)
Else
sReturn = sDefault
End If
ReadIni = sReturn
End If
Else
If bFileFound Then
MsgBox " some appropriate message"
ReadIni = "Error"
Else
MsgBox "some other appropriate message"
ReadIni = "Error"
End If
End If
On Error GoTo 0
Exit Function
ReadErr:
Select Case Err.Number
Case 71 'Disk Not ready
bFileFound = False
bValid = False
Resume StartRead
Case Else
MsgBox Err, vbOKOnly, "Error in Reading INI File"
End Select
End Function
Public Sub WriteIni(ByVal fileName As String, ByVal Section As String, ByVal KeyName As String, ByVal sEntry As String)
'=========================
' Copyright © 1998 Australian Custom Software Pty Ltd
'-------------------------
'Author : Tony Harris
'Created :
'Precis : Wrapper around the ini file API to write
' out to a private ini file.
'-------------------------
'Modified :
'=========================
Dim bValid As Boolean
Dim nReturnLen As Long
bValid = True
If Len(Trim(fileName)) = 0 Then bValid = False
If Len(Trim(Section)) = 0 Then bValid = False
If Len(Trim(KeyName)) = 0 Then bValid = False
If Len(Trim(sEntry)) = 0 Then sEntry = ""
If bValid Then
nReturnLen = WritePrivateProfileString(
Else
MsgBox "some appropriate error message"
End If
End Sub
ASKER
One more question regarding your code implementation. I copied over your examples, and ran the 'getprivateprofilestring'. And each time I altered the section and key names at different runs. All of my runs, the return value is allways zero(0). The only thing I altered was to make 'sinipath' to say: sinipath as string.
What haven't I set right within my VB V5.0 environment? Within VC++, have used this function and it has worked.
Can you shed any light on my problem(s)??
Thanks,
Midnightexpress
What haven't I set right within my VB V5.0 environment? Within VC++, have used this function and it has worked.
Can you shed any light on my problem(s)??
Thanks,
Midnightexpress
make sure you have "option explicit" at the beginning of the module...
also make the call using the defined structure - don't change anything...
also make the call using the defined structure - don't change anything...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your expertise.
Does anyone know how to read more that 255 characters of an entry in an INI file?
Bob
Bob
http://www.thescarms.com/vbasic/registry.htm
Be sure to save the web page.. as the documentation for the module is on it.