views:

1043

answers:

3

I've noticed that if the path parameter to the CreateFile function targets \Windows\System32\ the call is failing with the following error code ERROR_PATH_NOT_FOUND.

The file path is correct, I'm the owner of the folder, so the question is why is the call failing? Did MS add special policy forbidding the folder from being accessed?

Sample code:

TCHAR szFile[MAX_PATH];
PathCombine(szFile, g_szSystemDirectory, "settings.ini");

HANDLE hFile = CreateFile(szFile,
        GENERIC_READ,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);

if (hFile == INVALID_HANDLE_VALUE)
{
 printf("INVALID FILE: %i", GetLastError());
 return FALSE;
}
A: 

You're program probably needs to run as Administrator. You'll have to escalate your privileges, even if you are an administrator. Right click when you run the program and click "Run as Administrator", or edit the properties and select always run as administrator.

Ted Elliott
Tried that, still the same error.
arul
+1  A: 
  1. Can we see some example code?
  2. Have you specified the drive, I.e. "C:\Windows\System32\"
  3. Are you trying to open a file inside system32?
  4. Does this occur on Windows 7 only? and
  5. Why do you need to modify anything inside system32 in the first place?

Billy3

Billy ONeal
1) I'll add in a sec. 2) Yes. 3) Yes. 4) Yes, works fine under XP. 5) Need to load a config file.
arul
Why is the config file in system32? Shouldn't you be putting that in %appdata%?
Billy ONeal
Oh, and is this a 64 Bit OS?
Billy ONeal
Yep, it's 64bit. It's a legacy app, so I have no idea :)
arul
WOW is redirecting calls not into system32, but into syswow64. See http://en.wikipedia.org/wiki/WOW64#Registry_and_file_systemSo you have rights on that folder? Is the file you're looking for in that folder? Because that's where the call is actually going on x64.
Billy ONeal
If at all possible you do want to save to %appdata% because write access in System32 or SysWow64 requires admin rights.
Billy ONeal
Sample code added. Yes, the file is there and I made sure that I have full control over the entire folder. Besides I'm running as admin.
arul
I actually solved that problem by moving the folder out of System32, but I'm still kinda curious as to where was the problem.
arul
The problem is that your 32 bit app cannot see the C:\Windows\System32 folder. For compatability, all 32 bit apps on 64 bit windows, when asking for a file in system32, are automatically redirected to C:\Windows\Syswow64.If you place your config file in syswow64, but ask for it in system32, it will open correctly.
Billy ONeal
Yup, should have read you first comment more carefully, thanks!
arul
+1  A: 

If it's a 32-bit app running on a 64-bit OS, then calling Wow64DisableWow64FsRedirection() before your call to CreateFile will read from "C:\Windows\System32" instead of "C:\Windows\Syswow64", which is probably what's happening to you.

jeffm
Good to know, thanks.
arul