liuem  

所遇问题

  新建的算量工程文件暂时保存到临时文件中,代码中调用了Win32 API——GetTempFileName

  但在一台笔记本上,函数返回了一个空字符串!

  为了查明原因想到了好用的GetLastError——返回错误信息。

  结果错误信息为“拒绝访问”,这让我很快想到了传入文件夹用户权限问题。

  经过检验,果然当前用户没有传入文件夹的写权限。

 1   function GetTempFileA(const APrefix: string; const APath: string): string;
 2   var
 3     sPath: string;
 4     nErrCode: UINT;
 5   begin
 6     SetLength(Result, MAX_PATH);
 7     sPath := Trim(APath);
 8     if sPath = '' then
 9       sPath := GetCurrentDir;
10 
11     SetLastError(ERROR_SUCCESS);
12     if GetTempFileName(PChar(sPath), PChar(APrefix), 0, PChar(Result)) = 0 then
13     begin
14       Result := '';
15       raise Exception.Create(SysErrorMessage(GetLastError));
16     end
17     else
18       SetLength(Result, StrLen(PChar(Result)));
19   end;

API说明

  

反思

  茁壮的程序代码一定要有好的容错方法,一定要将错误信息发布出去。

  不怕出错,就怕不知道错在哪!

posted on 2014-07-11 09:26  弈名  阅读(1863)  评论(0编辑  收藏  举报