Re: What is the problem with GetPrivateProfileString?
- From: "Bob Butler" <tiredofit@xxxxxxxxxx>
- Date: Tue, 1 Nov 2005 14:48:34 -0800
"Steve Richfie1d" <Steve@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3sq543Fpa464U1@xxxxxxxxxxxxxx
> Bob,
>
> Here is the declaration and the wrapper I wrote to use
> GetPrivateProfileString. As I mentioned, this DOES work on XP.
>
> Steve
> . . . . .
> Const FileName$ = "C:\DrEliza\DrEliza.ini"
> Const SectionName$ = "General"
> Const MaxStringLength = 255 ' Maximum length of a string in a .INI
> file. . . . . .
type suffix characters! Yuck! (those aren't the problem, they jsut make
the code messy and hard to read IMO)
> Private Declare Function GetPrivateProfileString% Lib "kernel32" _
> Alias "GetPrivateProfileStringA" _
> (ByVal lpAppName$, ByVal lpKeyName$, ByVal lpDefault$, _
> ByVal lpReturnedString$, ByVal nSize%, ByVal lpFileName$)
GetPrivateProfileString returns a Long, not an Integer and the size
parameter is also a Long so you want to use &, not % in both cases
> Public Function vbGetPrivateProfileString$(SectionName$, _
> KeyName$, _
> Default$, _
> FileName$)
>
> ' This reads an arbitrary string.
>
> Dim Valid%, ReturnString$, Size%
Again, use Long (&), not Integer (%)
In C-style documentation the fields are defined as 'int' but that's a 32-bit
integer which maps to Long in VB so that may be where you got that
> ReturnString$ = Space$(MaxStringLength)
> Size% = Len(ReturnString$)
>
> If SectionName$ = "" Then _
> Error 449 ' Argument not optional or invalid property
> assignment
I strongly suggest losing the _ and adding an "End If" line... I read this
several times trying to figure out how these nested If statements were
working before noticing the line continuation and realizing that they aren't
nested at all. I'd also use Err.Raise instead of Error since Error is there
only for backward compatibility (from the time when MS cared about that sort
of thing!)
> If KeyName$ = "" Then _
> Error 449 ' Argument not optional or invalid property
> assignment
>
> If FileName$ = "" Then _
> Error 449 ' Argument not optional or invalid property
> assignment
>
> Valid% = GetPrivateProfileString%(SectionName$, _
> KeyName$, _
> Default$, _
> ReturnString$, _
> Size%, _
> FileName$)
>
> If Valid% >= MaxStringLength - 1 Then _
> Error 14 ' Out of string space
>
> vbGetPrivateProfileString$ = Left$(ReturnString$, Valid%)
> ' I've looked at ReturnString$ with the VB debugger and it is
> empty. ' I also tried InStr for Chr(0) but it also failed as
> expected.
I'll bet that the use of Integer instead of Long is the main issue here.
Win98 may be being pickier about it.
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
.
- Follow-Ups:
- Re: What is the problem with GetPrivateProfileString?
- From: Steve Richfie1d
- Re: What is the problem with GetPrivateProfileString?
- References:
- What is the problem with GetPrivateProfileString?
- From: Steve Richfie1d
- Re: What is the problem with GetPrivateProfileString?
- From: Bob Butler
- Re: What is the problem with GetPrivateProfileString?
- From: Steve Richfie1d
- What is the problem with GetPrivateProfileString?
- Prev by Date: Re: What is the problem with GetPrivateProfileString?
- Next by Date: Re: What is the problem with GetPrivateProfileString?
- Previous by thread: Re: What is the problem with GetPrivateProfileString?
- Next by thread: Re: What is the problem with GetPrivateProfileString?
- Index(es):