In the end, what ended up working was we set the path to the ini file using the server name instead of the DFS path (ie. \\fileserver01\Software\Config.ini
instead of \\customDFSPath\OtherDir\Config.ini
). Apparently they upgraded to Windows Server 2012 R2 recently and someone mentioned the DFS system there is slow and that was causing this issue.
GetPrivateProfileString sometimes returns 0 for various Section/Key combinations
Question
Having a new issue with an ini file and getPrivateProfileString - the frustrating bit is they have been working nicely together for about 3 years, and just over the last couple days, something caused them to freak out and every now and then it returns nothing for one of the fields I'm looking up. I've checked with the people who maintain the server, and nothing has changed that they're aware of.
I've looked at this answer, and tried removing the BOM from the file, and adding an extra line break as the first line - neither works.
The odd thing is that it doesn't happen with the same section/key combination every time, nor are all of the sections the same - I've noticed this happen with keys from 4 different sections so far, and it usually fails on just 1 of the keys I'm loading, although it has been up to 3 for a single run (out of about 20). I'm pretty well stumped.
Here's the code that calls GetPrivateProfileString:
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As String, ByVal nSize As Int32, _
ByVal lpFileName As String) As Int32
Public Overloads Shared Function INIRead(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String, _
ByVal DefaultValue As String) As String
Dim n As Int32
Dim sData As String
sData = Space$(1024)
n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
sData, sData.Length, INIPath)
If n > 0 Then
INIRead = sData.Substring(0, n)
Else
INIRead = ""
End If
End Function
' elsewhere in the code...
Dim db_conn as String
db_conn = INIRead(iniFile, "DATABASE", "SecurityDB")
Again, most of the calls to this work, but some don't, and it's doing my head in. Thanks!
Solution