代码之家  ›  专栏  ›  技术社区  ›  user1580348

重复性误差Winapi.ShlObj.SHGetFolderPath

  •  -1
  • user1580348  · 技术社区  · 6 年前

    uses
      Winapi.ShlObj;
    
    function GetUserAppDataPath: string;
    var
      ThisPath: PWideChar;
    begin
      if Winapi.ShlObj.SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, ThisPath) = S_OK then
        Result := string(ThisPath)
      else
        Result := '';
    end;
    

    enter image description here

    在delphi10.2tokyo中,如果我调用这个函数两次,第二次就得到一个AV。

    是什么导致了这个错误?

    我曾经 PWideChar enter image description here

    1 回复  |  直到 6 年前
        1
  •  6
  •   David Heffernan    6 年前

    您没有遵守文档中列出的协议。这个 documentation 因为最后的论点是

    你需要分配缓冲区并传递它的地址。

    function GetUserAppDataPath: string;
    var
      ThisPath: array[0..MAX_PATH-1] of Char;
    begin
      if Winapi.ShlObj.SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, ThisPath) = S_OK then
        Result := ThisPath
      else
        Result := '';
    end;