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说明

      

    反思

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

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

  • 相关阅读:
    分页存储过程
    调存储过程
    winform httplicent调用API
    存储过程,触发器,等等。。。
    C# AJAXform上传图片
    Mysql order by与limit联用出现的问题
    将Sublime Text 3 放到右键中
    Vue-cli构建步骤
    Javascript面试知识点
    position详解
  • 原文地址:https://www.cnblogs.com/liustdelphi/p/3837010.html
Copyright © 2011-2022 走看看