delphifileuactempmadexcept

How to fix madExcept creating temporal files in User\LocalSettings\Temp


I sterated using "Standard User Analyzer" from Application Compatibility toolkit and it reported that my app is not UAC compatible because:

"DeleteFileA: File (\Device\HarddiskVolume1\Documents and Settings\Administrator\Local Settings\Temp\mtgstudio.madExcept) is denied 'DELETE' access with error 0x5."

"DeleteFileA: File (\Device\HarddiskVolume1\Documents and Settings\Administrator\Local Settings\Temp) is denied 'DELETE' access with error 0x5."

Checking the madExcept.pas file I found:

function GetTempPath : AnsiString;
var arrCh : array [0..MAX_PATH] of AnsiChar;
begin
  if windows.GetTempPathA(MAX_PATH, arrCh) > 0 then begin
    result := arrCh;
    if result <> '' then begin
      CreateDirectoryA(PAnsiChar(result), nil);
      if result[Length(result)] <> '\' then
        result := result + '\';
      result := result + KillExt(ExtractFileName(ModuleName(0))) + '.madExcept';
      CreateDirectoryA(PAnsiChar(result), nil);
      result := result + '\';
    end;
  end else
    result := '';
end;

Is there a good way to overwrite the madExcept behaviour and store the temp files in a UAC allowed location?


Solution

  • It doesn't look like there's anything to fix. The GetTempPath API function is exactly the function to use to get a location where a program is allowed to create temporary files. That the compatibility tester was unable to delete the directories doesn't mean that the directories should have been someplace else. It only means they couldn't be deleted at the time the program tried. It could be that another program (such as the one being tested) had a file open in one of those directories; Windows doesn't allow folders to be deleted when there are open files in them.

    One possible source of problems is the way MadExcept creates the directories. It creates them such that they inherit the permissions of their parent directories. If deletion is forbidden for the parent directory, then it will also be forbidden for the newly created temp directories. That partly points to a configuration problem on your system: GetTempPath might be returning a path for a directory that doesn't exist. It just returns the first value it finds in any of the TMP, TEMP, and USERPROFILE environment variables. It's the user's responsibility (not your program's) to make sure those are accurate.

    Knowing that MadExcept uses GetTempPath to discover the temp directory gives you an opportunity. You can call SetEnvironmentVariable to change the TMP value for your process, and MadExcept will create its directory there instead. (But if the system-designated location for temporary files already doesn't work, good luck finding some alternative to use.)