After doing some testing on the posts in this topic, i have come up wiht this:I hope this will inspire to more ideas in the possibilities of returncodes from UDF's whitout disturbing Ruud.
You don't need a separate function to set @Error, Exit @Error in UDF's works just fine.
As for the setting of @SError, look in the following example, where i picked one of the win32 error codes from
the manual to descripe the error. Of course it would be nice to be able to make your own text for @SError, but
this is my suggestion:
You should be able to find an appropriate error/description from the 500+ decriptions in the manual,
Appendix B: Win32 Error codes.
In this example I use:
Exit 6 ; @SError = ERROR_INVALID_HANDLE
to describe why the UDF ReadSection() didn't work (All available handles (1 - 10) was allready used by Open().
and
Exit 87 ; ERROR_INVALID_PARAMETER
to describe that the UDF ReadSection() was called wiht an invalid argument.
Observe: Even if the the UDF ReadSection() is places inside the build in function Split(), the error is
stil retrieved.
(This is a very nice feature)
Another observation: Don't use @Error in your UDF to check for errors, use $RC as in my example, because calls to
@Error tends to reset @Error.
To test the script on WinNT/2000 you have to copy the file MSPrint.inf from a Win9x workstation, or choose another
ini-like file.
And now the code:
code:
Break On; Show section. This is the valid version of the call
$File = %WINDIR% + "\Inf\MSPrint.inf"
$Section = "Uni"
ShowSection($File, $Section)
?
; Invalid filename
$File = %WINDIR% + "\Inf\WRONG.INF"
$Section = "Uni"
ShowSection($File, $Section)
?
; Invalid path
$File = %WINDIR% + "\WRONG\MSPrint.inf"
$Section = "Uni"
ShowSection($File, $Section)
?
; Invalid parameter
$File = %WINDIR% + "\Inf\MSPrint.inf"
$Section = 0 ; Should be a string
ShowSection($File, $Section)
?
; No file handles available
$File = %WINDIR% + "\Inf\MSPrint.inf"
For $i = 1 To 10 ; Use all handles
$RC = Open($i, "$File")
Next
$Section = "Uni"
ShowSection($File, $Section)
?? "Press any key to continue... " Get $x
Return
Function ShowSection($File, $Section)
$SecArray = Split(ReadSection($File, $Section), Chr(10))
If @Error
? "Error: " + @Error + " " + @SError
Else
"Entries in section($Section): " ??
For Each $Member In $SecArray
$Member ?
Next
EndIf
EndFunction
Function ReadSection($File, $Section)
$ReadSection = ""
If VarType($File) <> 8
Exit 87 ; ERROR_INVALID_PARAMETER
EndIf
If VarType($Section) <> 8
Exit 87 ; ERROR_INVALID_PARAMETER
EndIf
$Handle = 0
Do
$Handle = $Handle + 1
$RC = Open($Handle, $File)
If $RC > 0 ; System error, Open() returns only negative error numbers
Exit $RC
EndIf
Until $Handle > 10 Or $RC = 0 ; Invalid handle or successfull open
If $RC
Exit 6 ; ERROR_INVALID_HANDLE
EndIf
$RetVal = ""
$Section = "[" + $Section + "]"
$Lin = ReadLine($Handle)
While @Error = 0 And RTrim(LTrim($Lin)) <> $Section
$Lin = ReadLine($Handle)
Loop
$Lin = LTrim(RTrim(ReadLine($Handle)))
While @Error = 0 And SubStr($Lin, 1, 1) <> "["
If $Lin
If $RetVal
$RetVal = $RetVal + Chr(10)
EndIf
$RetVal = $RetVal + $Lin
EndIf
$Lin = LTrim(RTrim(ReadLine($Handle)))
Loop
$ReadSection = $RetVal
$RC = Close($Handle)
EndFunction
Erik
PS:
You might wonder what i would need to use the function ReadSection() for!
It is for reading inf files. Inf files nearly follows the the rules for ini files, but this is allowed in an
inf file:
[UNI]
UNIDRV.DLL
UNIDRV.HLP
ICONLIB.DLL
Observe no =
Also if i need to read all entries in the section [386Enh] in System.ini, the section might look like this(Win9x):
[386Enh]
ebios=*ebios
mouse=*vmouse, msmouse.vxd
device=*dynapage
device=*vcd
device=*vpd
Observe, there is several device= statements, Read/WriteProfilestring will always read/write the first entry.
[This message has been edited by kholm (edited 18 May 2001).]