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

      

    反思

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

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

  • 相关阅读:
    服务器状态码
    QuerySet中添加Extra进行SQL查询
    django配置一个网站建设
    MySQL数据库查询中的特殊命令
    125. Valid Palindrome
    121. Best Time to Buy and Sell Stock
    117. Populating Next Right Pointers in Each Node II
    98. Validate Binary Search Tree
    91. Decode Ways
    90. Subsets II
  • 原文地址:https://www.cnblogs.com/liustdelphi/p/3837010.html
Copyright © 2011-2022 走看看