20.12.2008, 04:07:08
(This post was last modified: 20.12.2008, 04:08:45 by Professor_Tesla.)
I need help with fixing something really annoying in my visual basic code. I found some code on the internet for working with .ini files, and I wanted to incorporate it into a program that I have written. Here is the relevant part of the code.
The function at the bottom is part of the code that one of you (Marshall, I think) gave to me for opening a file with its default editor. For some stupid, extremely annoying reason, it won't put the function "ShellExecute" in a separate section to the Sub defined above it, so I get an error when I try to compile.
Code:
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
'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
Sub InitProgram()
Dim sINIFile As String
Dim sBetaUpdatePath As String
Dim nCount As Integer
Dim i As Integer
'Store the location of the INI file
sINIFile = App.Path & "\WAAutorun.INI"
'Read the Beta Update Path from the INI file
sBetaUpdatePath = sGetINI(sINIFile, "WAAutorun", "BetaUpdatePath", "?")
If sBetaUpdatePath = "?" Then
'No user name was present – ask for it and save for next time
sBetaUpdatePath = InputBox$("Enter the location of the Beta Update:")
writeINI sINIFile, "WAAutorun", "BetaUpdatePath", sBetaUpdatePath
End If
End Sub
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long