0

I'm trying to list files of C:\Windows\System32\config directory.

I've tried to use QDir::entryList() like this

QDir dir(R"(C:\Windows\System32\config)");
dir.setFilter(QDir::Hidden | QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot);
qDebug().noquote() << dir.entryInfoList();

Also I've tried to use std::filesystem::directory_iterator like this

std::string path = R"(C:\Windows\System32\config)";
for (const auto& entry : std::filesystem::directory_iterator(path))
{
    qDebug().noquote() << entry.path().string().c_str();
}

Both gives me the same output:

C:\Windows\System32\config\ELAM

C:\Windows\System32\config\Journal

C:\Windows\System32\config\RegBack

C:\Windows\System32\config\systemprofile

C:\Windows\System32\config\TxR

File manager shows me this output:

C:\Windows\System32\config\BBI

C:\Windows\System32\config\BCD-Template

C:\Windows\System32\config\COMPONENTS

C:\Windows\System32\config\DEFAULT

C:\Windows\System32\config\DRIVERS

C:\Windows\System32\config\ELAM

C:\Windows\System32\config\Journal

C:\Windows\System32\config\netlogon.ftl

C:\Windows\System32\config\RegBack

C:\Windows\System32\config\SAM

C:\Windows\System32\config\SECURITY

C:\Windows\System32\config\SOFTWARE

C:\Windows\System32\config\SYSTEM

C:\Windows\System32\config\systemprofile

C:\Windows\System32\config\TxR

C:\Windows\System32\config\VSMIDK

OS: Windows 10

The question is how can I get the same output using C++?

Community
  • 1
  • 1
  • Maybe you do not have permission to access those files? – PaulMcKenzie Dec 16 '19 at 16:32
  • @PaulMcKenzie I was thinking that, but if Explorer shows them then his user account has permission. I was wondering if it was an elevated process thing? I couldn't reproduce however. – Fire Lancer Dec 16 '19 at 16:34
  • Run your program from the command line with elevated rights. That directory is special in that (for me) requires admin rights to get to it. – PaulMcKenzie Dec 16 '19 at 16:36
  • hmm strange, two of my Windows VMs (Win10 and 2019) have different permissions on those files... – Fire Lancer Dec 16 '19 at 16:44

2 Answers2

1

This is likely an issue with permissions, if you view the "Security" tab in the properties window in Explorer, you will likely see some files have "Read" permission on the "Users" group, but some files only have permissions for "SYSTEM" and "Administrators".

When you run a program in Windows, even from an administrator account, it generally runs without elevation, so it won't be able to access those files with more restricted permissions.

  • You can explicitly run your program elevated, e.g. right click the exe/shortcut and "Run as administrator". Note that in the case of Visual Studio, you could run VS itself as administrator.

  • If your program will always need to run elevated, you can set it as such, in VS, on "Linker" -> "Manifest File" there is the "UAC Execution Level" option, the "highestAvailable" or "requireAdministrator" options might be useful.

  • If you are launching a child process, you can choose to elevate at that point, e.g. using ShellExecuteEx, which will cause a UAC popup if required.

Fire Lancer
  • 29,364
  • 31
  • 116
  • 182
0

I finally found a solution after 1 year and 9 months.

When I tried list files I was building a 32 bit app and there was Wow64 redirection. There are two ways to resolve this:

  1. build 64 bit app
  2. disable redirect