|
-
Jun 12th, 2002, 05:56 AM
#1
INI files in practice
Hi there,
I have downloaded John Percival's "Sample INI program" which is a very nice and clear demo. However there's a point which doesn't make sense to me. Probably it's a beginners question and maybe it does not have anything to do with the specific demo project itself, but as I'm not sure I transcribe below the relevant parts of John code (ok, if you want to run it you'll have to download the project as it contains a few textboxes, etc):
'This part was in a module
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal lpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public 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
'and this part was on a form
Option Explicit
Private IniFileName As String
Private Sub Form_Load()
IniFileName = InputBox("Which file do you want to read/edit/create?")
If IniFileName = "" Then End
End Sub
Private Sub cmdget_Click()
Dim ret As Long
Dim Temp As String * 50
Dim lpAppName As String, lpKeyName As String, lpDefault As String, lpFileName As String
lpAppName = appname.Text
lpKeyName = Keyname.Text
lpDefault = IniFileName
lpFileName = IniFileName
ret = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, Temp, Len(Temp), lpFileName)
If ret = 0 Then
Beep
Else
txtvalue.Text = Trim(Temp)
End If
End Sub
Private Sub cmdstore_Click()
Dim lpAppName As String, lpFileName As String, lpKeyName As String, lpString As String
Dim ret As Long
lpAppName = appname.Text
lpKeyName = Keyname.Text
lpString = txtvalue.Text
lpFileName = IniFileName
ret = WritePrivateProfileString(lpAppName, lpKeyName, lpString, lpFileName)
If ret = 0 Then
Beep
End If
End Sub
Now, my question relates to the cmdget_click() subroutine. As you can see the returned value is assigned to a TextBox: txtvalue.text=Trim(Temp)
But if I want to directly assign this returned value to a string variable (in the test ini file I'm using I'm actually reading a string), it turns out that my string variable is not trimmed at all.
I've tried: MyStrVar = Trim(Temp) which returns the string in the file plus a trail of charcters up to the total capacity of the Temp buffer variable.
What am I missing?
Thank you
-
Jun 12th, 2002, 06:03 AM
#2
Code:
Dim Temp As String * 50
It's because of this line stating that the string MUST BE 50 characters. Normally, this is to trim the returned string down to 50 characters, for you this is working the other way and filling the blank ending figures with spaces.
If you're going to specify and use Temp as another value (say "HelloWorld") then count the characters in this value (10), and change the * 50 to that figure...
-
Jun 12th, 2002, 09:41 AM
#3
OK, but...
Well, this is what I've done:
i=Instr(Temp,Chr(0)) 'Since the rest of the string is padded with null characters
MyStrVar=Left(Temp,i-1)
and it works. Nevertheless, is there a more straightforward way to do it?
And, still, I don't understand why MyStrVar is not trimmed, since it is a different variable from Temp and has not been defined as having a fixed length of 50...
-
Jun 12th, 2002, 10:03 AM
#4
Microsoft recommends using SaveSetting and GetSetting to write to and read from the Registry rather than using ini files.
-
Jun 12th, 2002, 10:06 AM
#5
PowerPoster
Well
Originally posted by MartinLiss
Microsoft recommends using SaveSetting and GetSetting to write to and read from the Registry rather than using ini files.
I use ini files, mainly so it's easier for an administrator other than myself to change aspects that the program can see (especially true dealing with file access paths).
But this leads me to a question :
If the admin is not logged into the, say Win 2k system, and the app tries to manipulate the registry, will the program, and possibly the system, crash?
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 12th, 2002, 10:16 AM
#6
To my knowledge there is no case where accessing the Registry can cause a problem. I also think that changing the registry is just as easy as changing an ini file since if you use SaveSetting you always know where the values are (HKEY_CURRENT_USER|Software|VB and VBA Program Settings|MyAppName). Finally the registry is not something that most users would touch or delete which is not the case with ini files ("What's this file, I don't need it").
-
Jun 12th, 2002, 10:24 AM
#7
PowerPoster
Well
Points well taken, but my ini files are not actaully INI files, they are with weird extensions in the system32 folder, so deleting them is not common.
I appreciate your comments, and will research furthur on Registry manipulation...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
-
Jun 14th, 2002, 02:14 AM
#8
I agree with what Martin's put, the registry is safer from users. As far as this bit goes :
If the admin is not logged into the, say Win 2k system, and the app tries to manipulate the registry, will the program, and possibly the system, crash?
The system won't crash, but your program will error if it's runniing on WinNT, Win2000 or WinXP, yes.
I can't remember if you can still read a registry key or not, but you definitly can't change one using the normal registry code.
The way around that one is to use the API to fiddle with user privelages which is very awkward to say the least & much harder than using a simple ini file; http://www.vbforums.com/showthread.p...ighlight=admin
-
Jun 14th, 2002, 08:00 AM
#9
PowerPoster
Originally posted by alex_read
The system won't crash, but your program will error if it's runniing on WinNT, Win2000 or WinXP, yes.
The way around that one is to use the API to fiddle with user privelages which is very awkward to say the least & much harder than using a simple ini file
This is why I use the INI files. Alot of extra headache, to accomplish the same goal...
Remaining quiet down here !!!
BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|