Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering what I am i doing incorrectly in the following code to retrieve a connection string from an .ini file. I keep getting a blank string.
I have my API declared in my global mod.

'Get DB Loaction from INI file
 
Dim strDBConnect As String
 sSection$ = "DB1"
 sEntry$ = "DATABASECONN209"
 sDefault$ = ""
 sRetBuf$ = String$(256, 0) '256 null characters
 iLenBuf% = Len(sRetBuf$)
 sFileName$ = "c:\program files\conn.ini"
 '*
 X = GetPrivateProfileString(sSection$, sEntry$, _
 sDefault$, sRetBuf$, iLenBuf%, sFileName$)
 '*
 sValue$ = Left$(sRetBuf$, X)
 strDBConnect = sValue$ <<---Blank string, no return of connection string.


==========In my .ini file i have the following
[DB1]
 DATABASECONN209="Provider=SQLOLEDB;Data Source=222.34.34.34;Initial Catalog=xxx;User Id=appxxx;Password=@xxxpassword"


Thanks,
Gene
Posted
Updated 22-May-14 8:23am
v2
Comments
PIEBALDconsult 22-May-14 14:25pm    
Don't you have to pass in the address of sRetBuf$ ?

We can't answer that - we don't have access to your computer, or your INI file.

So use the debugger: place a breakpoint on the first line, and follow each line through working out what it should produce before you execute it. Compare that with what did happen, and if they are different work out why.

We can't do it for you - we can't run your program under the same conditions you can, so you will have to do this part yourself!
 
Share this answer
 
Comments
Maciej Los 22-May-14 14:45pm    
I believe OP has done it. Please see my answer.
Please read comments to this article: GetPrivateProfileString function[^]. You'll find there:
Adam L Wa wrote:
This function will not work if ini file contains only 1 entry
This might be specific to Unicode ini files, but I spent quite some time trying to figure out what I was doing wrong. It turns out I wasn't doing anything wrong, and this function has a bug. My ini file only needed one section and key, e.g.:
[SomeEntry]
Foo=Bar


You can write custom function too, which
1) opens an ini file
2) reads it content line by line
3) proceeds each line with string functions[^], like: Instr[^], Mid[^], etc.
 
Share this answer
 
Comments
[no name] 24-May-14 20:10pm    
same I would suggest, but you are always faster :( therefore my downvote to 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900