PDA

Click to See Complete Forum and Search --> : INI PROBLEMS


PITBULLCJR
Dec 29th, 1999, 03:16 AM
I am sorry but I don't get that code at all. Could you try and explain it a little better for dumb people. Thanks you

PITBULLCJR
Dec 29th, 1999, 11:01 AM
Ok I made a program that has you log in using a username and password. THe username and password are set in stone in the code but what i want is to be able to change your username or password. so i figured I would have the password thing look for the username and password in an ini file and if they want to change it then it would just rewrite the ini information. well I put in the
[username]
username=chris
[pass]
pass=chris
in the ini file. I did it by opening the file and just typing it in and it didn't work and then i deleted it and had my code write it in and it still didn't work. What keeps happening is that when it goes to read it it reads it like this chris| || || || || || || || || || || || | os something close to that the bars are very fat and close together. WHy does it put all those bars in? I figured this out by using the debug thing and when that is yellow it says username = chris| || || || || || || || well if you have any ideas please help thanks
sincerely, CHris

RogerH
Dec 29th, 1999, 11:40 AM
The bars depend on what you initialize your var with. I suppose you use writeprivateprofilestring and getprivateprofilestring. getprivateprofilestring needs a max number of characters to read and gives you the number of read characters as result. Init your var to read in with space(x) first!

Use
readvar = space(100)
readlen = getprivateprofilestring(<section in ini-file in []>, <key, that is key=>, default, readvar, 100, file)
readvar = left$(readvar, readlen)

RogerH

RogerH
Dec 29th, 1999, 06:27 PM
OK, I'll try to make it clear:

Suppose you want to write/read this INI-file

[username]
usernamekey=chris
[pass]
passkey=chris

named C:\temp\test.ini

First you have to declare the API-functions (in a module):


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 "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long


Writing the file:


WritePrivateProfileString "username", "usernamekey", "chris", "c:\temp\test.ini"
WritePrivateProfileString "pass", "passkey", "chris", "c:\temp\test.ini"


Reading is not that simple. As you see in the declaration, the get-function want's the parameters
lpDefault As String => Give this back, if the entry is not found in the INI-file
lpReturnedString As String => This parameter gives back the result
nSize => max. number of characters for the result

GetPrivateProfileString needs reserved variable space to give something back. You create this by adding spaces to the string variable lpReturnedString before calling the function and you have to tell the function in nSize how many space in number of characters is reserved. The function gives you the number of characters used as result and puts the result in lpReturnedString.



DIM readvar as string
DIM readlen as string
readvar = space(100) 'reserve 100 char
readlen = getprivateprofilestring("username", "usernamekey", "defaultuser", readvar, 100, "c:\temp\test.ini")
readvar = left$(readvar, readlen) 'cut off unused space
msgbox "Username = " & readvar

readvar = space(100) 'reserve 100 char
readlen = getprivateprofilestring("pass", "passkey", "defaultpassword", readvar, 100, "c:\temp\test.ini")
readvar = left$(readvar, readlen) 'cut off unused space
msgbox "Password = " & readvar


Hope this makes it clear ;)

RogerH

PITBULLCJR
Jan 1st, 2000, 02:17 AM
Hey it gives me the correct password now but it says the username is false instead of chris. What went wrong?

------------------
Sincerely,
Chris
:-) ;-)
Email pitbullcr7@aol.com

PITBULLCJR
Jan 1st, 2000, 02:19 AM
Wopps my mistake i missed a line. All fixed I get the correct username and password now lets see if i can get it to work in my program. Well thanks a ton!!!

------------------
Sincerely,
Chris
:-) ;-)
Email pitbullcr7@aol.com