Search code examples
windowswinapiinicontrolled-folder-access

WritePrivateProfileString produces ERROR_FILE_NOT_FOUND


My Software is writing to an ini file in c:\users...\documents and from time to time I get error reports from users where the WritePrivateProfileString call returns false and then GetLastError returns 2 (ERROR_FILE_NOT_FOUND).

I don't understand how this can happen and I wasn't able to reproduce it, since WritePrivateProfileString is supposed to create the file if it's missing. If the directory didn't exist, the error would be ERROR_PATH_NOT_FOUND, if the file couldn't be created for permission reasons, it would be ERROR_ACCESS_DENIED.

So does anyone know why/when this function can produce this error? And how I can deal with it? As in: I rely on the file being created if necessary.


Solution

  • I think I may have found the answer myself. The problem seems to occur when Windows Security -> Ransomware protection -> Controlled folder access is enabled and the application isn't white-listed.

    ERROR_FILE_NOT_FOUND is a very confusing error message for this situation imho...