简体   繁体   English

如何检查DWScript FileCreate函数的返回值?

[英]How to check return value of DWScript FileCreate function?

Using DWScript, I don't see obvious way of checking the return value of the FileCreate function. 使用DWScript,我没有看到检查FileCreate函数返回值的明显方法。

Example (not working) script: 示例(不工作)脚本:

function TestFileCreate : Boolean;
var
    F : File;
begin
    F := FileCreate('MyTestFile.txt');
    Result := (F = -1);        // Not working!
    Result := (F.Handle = -1); // Not working!
end;

Extract from DWScript source code: 从DWScript源代码中提取:

procedure TFileCreateFunc.DoEvalAsVariant(const args : TExprBaseListExec; var result : Variant);
var
   h : THandle;
   i : IdwsFileHandle;
begin
   h:=FileCreate(args.AsFileName[0]);
   i:=TdwsFileHandle.Create(h);
   Result:=IUnknown(i);
end;

As you can see, internally, Delphi CreateFile is called and the result value is -1 when it fails. 如您所见,在内部调用Delphi CreateFile,失败时结果值为-1。 This numeric value is converted to a IdwsFileHandle. 此数值将转换为IdwsFileHandle。

They were intended to raise an exception in case of failure. 它们的目的是在发生故障时提出异常。 This has now beed fixed! 这已经固定了!

Extended so that now exception is triggered only in case of use of an invalid file, except for two functions: FileIsValid and FileClose. 扩展,以便现在仅在使用无效文件的情况下触发异常,但两个函数除外:FileIsValid和FileClose。 Also added helpers so that file functions can be used like methods (ie. "FileIsValid(f)" can also be written as "f.IsValid") 还添加了帮助程序,以便可以像方法一样使用文件函数(即“FileIsValid(f)”也可以写为“f.IsValid”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM