0

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.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Tannin
  • 488
  • 5
  • 11
  • Post code please. – Michael Chourdakis Jun 18 '19 at 09:22
  • 1
    **Note** *This function is provided only for compatibility with 16-bit versions of Windows.* from the docs. Use a bespoke ini file library for this. – David Heffernan Jun 18 '19 at 09:23
  • 1
    Ask the users where this is failing about their anti-malware software. Maybe there is a recurring pattern. – IInspectable Jun 18 '19 at 09:24
  • @DavidHeffernan The ini we're working with is for a third party application, we don't have control over that. And since ini is not a standardized format, using an ini file library can't guarantee that it produces format that is actually usable by that other application. In short: Those functions aren't going away and MS knows that full well. – Tannin Jun 18 '19 at 09:57
  • @MichaelChourdakis For me to post code I'd have to be able to reproduce the issue which is exactly what I'm trying to get help with... ;) – Tannin Jun 18 '19 at 09:58
  • You should check yourself if the registry key or file exists (you know that most ini files are mapped to the registry, right?), or if the section doesn't exist (WritePrivateProfileSection), and if neither of those apply, then the key doesn't exist. – Strive Sun Jun 18 '19 at 10:03
  • Unless the ini file is explicitly mapped to the registry it shouldn't be, but even then, the interface shouldn't change. My understanding (which I confirmed through testing) is that the api will create sections as necessary. WritePrivateProfileSection would replace the entire section which is not what I want. I should also maybe point out that my application is used by >200k users and this problem occurs for a few dozen of them so there doesn't seem to be anything fundamentally wrong in my code logic. It's more likely to be related to something unusual - AV software isn't a bad call. – Tannin Jun 18 '19 at 11:13
  • May I ask: Why the downvote? How is this a bad question? – Tannin Jun 18 '19 at 11:14
  • These APIs are full of compat shims. All sorts of ways they could fail. – David Heffernan Jun 18 '19 at 11:16
  • But that's what I'm asking about: Why is it failing with this error. As I said before: I don't have a choice in the matter, for my application to do its job properly I have to use this API. Are you essentially saying my question is bad because you don't know the answer? – Tannin Jun 18 '19 at 11:23
  • _Are you essentially saying my question is bad because you don't know the answer?_ No, I'm not saying that. – David Heffernan Jun 18 '19 at 11:29
  • Are you absolutely sure that you are using the correct path when it fails? Use [SysInternals Process Monitor](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) to verify, or simply log the actual path you are passing in – Remy Lebeau Jun 18 '19 at 15:23

1 Answers1

0

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...

Tannin
  • 488
  • 5
  • 11
  • I'll leave the answer un-accepted for the moment in case someone else can come up with another way to reproduce this error. – Tannin Jun 18 '19 at 11:28