tags:

views:

1209

answers:

7

I have read many articles about how to use an INI file within my VB6 project. I don't have a problem with the methods, my problem is how to make the EXE file find the INI file. I don't want to hard code the path in the program. I simply want the EXE to expect the INI file to be present in the same folder the EXE is executed from.

When I run the program from inside VB6 IDE, the INI is found and processed. When I compile the program and run the EXE, nothing is found.

My code looks like:

gServer = sGetINI(sINIFile, "TOOLBOM", "ServerName", "?")

where TOOLBOM is the [Section] and "ServerName" is the key for the value.

I obtained the following code for the API:

Rem API DECLARATIONS
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
Public Function sGetINI(sINIFile As String, sSection As String, sKey _
                As String, sDefault As String) As String
    Dim sTemp As String * 256
    Dim nLength As Integer
    sTemp = Space$(256)
    nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
              255, sINIFile)
    sGetINI = Left$(sTemp, nLength)
End Function
Public Sub writeINI(sINIFile As String, sSection As String, sKey _
           As String, sValue As String)
    Dim n As Integer
    Dim sTemp As String
    sTemp = sValue
    Rem Replace any CR/LF characters with spaces
    For n = 1 To Len(sValue)
        If Mid$(sValue, n, 1) = vbCr Or Mid$(sValue, n, 1) = vbLf _
        Then Mid$(sValue, n) = " "
    Next n
    n = WritePrivateProfileString(sSection, sKey, sTemp, sINIFile)
End Sub
+4  A: 

In VB6 you can use the App.Path to specify the path to files that should be in the directory you are executing from. e.g.

sIniFile = App.Path & "\myIniFile.ini"

What is the error that you're getting?

Hamish Smith
Unless you *have* to use INI files, you're better off using the registry via SaveSetting and GetSetting. Admins can disallow non-admins the ability to update files in the Program Files directories, so your program can't update INI settings if saved in App.Path.+1 for the answer, though
JeffK
Much of the registry is off limits to standard users beginning with Vista. Microsoft recommends you move back to INI files because registry pollution has become a problem.
Bob
This will fail as soon as anyone tries to run it on Vista or later though.Writeable data files are not supposed to go into Program Files. Since people did it anyway Windows began enforcing the rules beginning with Vista.
MarkJ
As a VB6 old-timer I should warn you that if your app is installed in the drive root, for instance C:, then App.Path will return a string with a trailing backslash for instance "C:\" and you will have two backslashes in your string. Only matters these days if your app is going to be in root of a CD
MarkJ
A: 

This worked for me this time. I actually had this exact code in my project. I don't know what I was missing previously, but it fine now. Thanks again!

+1  A: 

This will fail as soon as anyone tries to run it on Vista or later though.

Writeable data files are not supposed to go into Program Files. Since people did it anyway Windows began enforcing the rules beginning with Vista.

Global settings belong in an application folder under CommonAppData, per-user settings go below LocalAppData, per-using roaming settings under AppData, and so on. These locations are retrieved at runtime via Shell object or API calls.

Bob
A: 

You will want to use the FileSystemObject from the Scripting runtime to combine paths and filenames properly. Although it may seem a trivial issue in reality there are corner cases that the FileSystemObject handles.

RS Conley
+3  A: 

Bob is right, this will fail as soon as anyone tries to run it on Vista or later. Writeable data files are not supposed to go in Program Files. Windows now enforces these rules. Global settings for all users belong in one folder, per-user settings in another, per-user roaming settings in another, etc.

This Visual Studio Magazine article by Karl Peterson gives some VB6 code you can drop into your project to find the locations of these folders at run-time. And then this previous article by the same author gives you a nice class for using INI files, to hide away those API declarations.

MarkJ
A: 

app.path would return the path of the currently executing exe,use it

appusajeev
A: 

Keep the EXE in the same folder

Dipalee