zoukankan      html  css  js  c++  java
  • 【温故Delphi】Win32API之GetTempFileName

    所遇问题

      新建的算量工程文件暂时保存到临时文件中,代码中调用了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说明

      

    反思

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

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

  • 相关阅读:
    Common ThreadView
    经典代码IOCP的C#实现(转)
    Common.UdpLib
    Common.TcpLibTcpServerWIOCP
    Common.TcpLibTcpServerY
    sql中将分隔字符串转为临时表的方法
    病毒及流氓软件自我复制的简单实现
    一句sql搞定个人所得税计算
    财务月度的创建及生成
    box2d 教程
  • 原文地址:https://www.cnblogs.com/liustdelphi/p/3837010.html
Copyright © 2011-2022 走看看