Show contents of C:\Windows\System32\config using C++ - c++

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++?

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.

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:
build 64 bit app
disable redirect

Related

Terminate the previous iteration when building a new one

TL;DR: How can I make CLion kill off any running versions of the code before building the new one?
I've started playing with CLion for C++. I noticed that I often forget to stop the last iteration before trying to compile again, which leads to "Permission denied" errors -- when I first encountered this, it took me almost half an hour of fiddling with permission settings before realizing that it was because the old version was still running, and therefore couldn't be replaced with the new executable.
As far as I can tell, there's no way to do this in CMake without embedding a Batch (since I'm on Windows) script. I'd like to avoid that, because it'd be a lot of unnecessary complexity for... not that much reward.
In short, is there an option in CLion or something in CMake that will stop the previous iteration when running the new one?
Yes there is. Simply press ctrl + F2 or goto Run > Stop to terminate the previous iteration in case it keeps running.
Alternatively, you can set to run only single instance. This way previous instance will always be terminated before running the new one. To enable this, goto Run | Edit Configurations and select Single Instance Only.
As far as I know, this is not possible by default.
One solution I have found was to create a batch file with the following content:
#echo on
tasklist /FI "IMAGENAME eq %1" 2>NUL |find /I "%1">NUL
if "%ERRORLEVEL%"=="0" taskkill /F /im %1
(Second line checks whether the process is running - found here: How to check if a process is running via a batch script)
And edit the build configuration to make CLion call the batch file and pass the processname to it before every build.
This is how it works:
Run > Edit Configurations > select the configuration to change (in my case "Build All")
In the "Before launch: External..."-section click the green plus
Choose "Run external tool" and click the green plus in the pop-up window
Choose a name for the tool
Add the path of the batchfile in the "Program:" field
Write $ProjectName$.exe into the "Parameters:" field
Click ok until you are back in the config window
Give the script a sufficient priority by selecting it and clicking the arrow up
Now it should try to kill the running process before every build.

Launch a program at login without permissions (fedora 20)

I'm currently developing an application with QT 4.8 with a "Launch on login" option. My main problem currently is that I can't seem to find a proper way to make the program launch itself after login on Linux (Fedora 20 in my case).
My program should be able to run in the background without stopping fedora to launch.
I would also like to avoid having to ask for any sort of admin privilege since my application doesn't require any (except maybe for this option).
Finally found out about "home/.config/autostart/.desktop".
I'll have to create autostart if it's not created but from there I can chose to make or delete the file depending on what my user decided to do.
The .desktop file have to follow a particular syntax (which is not a problem for me)
[Desktop Entry]
Type=Application
Exec=</path/to/binary or command to execute>
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=<Name_to_be_displayed>
Comment=<optional comment>
In my case, I can just remove the "comment" line since just the name of the application should be enough for the user to recognize it.
You can Either keep your executable in side /etc/rc.d/rs or inside /etc/init.d folder, so that it can automatically start once device is up.
Hope this Helps.
Each user has a .bashrc file located in their home directory, you could append a command to run your application to the end of that.
That will run when only that user logs in (I think either with graphical or terminal session)
To run for all users, look at creating a systemd service if you dare, or append a line to the end of /etc/rc.local. You'll need to be root for that though.
EDIT: Don't forget to put an ampersand at the end of the command to run in background.

"windows cannot access the specified device....." error in c++

I am MFC guy working on visual studio 2010 create some executables using visual studio!! but on linux and mac my executables are not working as usual windows!!.
So i decided to use "MinGW" compiler to create executables.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
I successfully install WinGW compiler on my C drive and start working with following program..
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello ";
return 0;
}
I compile it using following command,
g++ -static-libgcc -static-libstdc++ Main.cpp
I found one executable in same folder with name a.exe :).Working fine:)
But after some time i decide to modified same program in following manner like,
int main ()
{
return 0;
}
I compile it with same command but when i execute it using command line it show me error "Access is denied so i goto that folder and run same executable as "Run as Administrator" it show me one messagebox with the message windows cannot access the specified device path or file. you may not have appropriate permissions
---EDIT--
follwing code is NOT WORKING:-
int main ()
{
int k;
return 0;
}
but this program WORKING :-
int main()
{
int k;
k = 0;
return 0;
}
If you are getting this access denied error, then the most likely cause is that the executable file is open in another process, probably the linker or debugger. Try installing Process Explorer and hit Ctrl+F and type in the name of your .exe. This should show the processes that the .exe file open. Kill those processes (or if you are still debugging, then end debugging first). You then should be able to build again.
Note that this has nothing to do with Microsoft APIs, as in any case you're using gcc.
EDIT: If there are no processes holding the .exe then it may be that there is some other kind of permission problem. Does the .exe file exist? Can you delete the file and rebuild? Another thing to try is run Process Monitor and filter for the name of the .exe -- that may show a regular permission denied error, or perhaps another error such as a sharing conflict.
Note:-Please give me one suggestion is that," is minGW is best compiler for cross plateform working ??or any thing else is there??"
No. And there's nothing else out there.
Use whatever compiler is available on target platform, ensure your code compiles on all of them.
Avoid platform-specific and compiler-specific code at all costs (use cross-platform frameworks).
I successfully install WinGW
There are many different versions of mingw provided by different sites. If you install compiler from mingw.org using mingw-get, it'll probably work. If you install mingw from some other site, it may or may not work.
I compile it using following command,
Use a build systems. cmake, qmake or something similar.
it show me error "Access is denied
Launch process monitor and see after which system call it terminates. It is also possible that your antivirus software interferes with your program, or maybe there's some stray dll in your path or something like that.
Check the permissions for the entire folder in which the executable resides. Trying to 'Run as Administrator' doesn't have any effect if the folder doesn't allow the permissions.
It doesn't have anything to do with your code. This is an environmental problem, something is pretty messed up about the permissions your user account has for one or more of the directories on your hard disk. The generic diagnostic is that the default working directory for the program does not permit read or list access.
A possible starting point would be to use Explorer and right-click the directory where MinGW is installed. Use the Security tab and ensure that your user account has all permissions enabled. Further narrow it down to trying to run the program from the command prompt, using different directories as the default directory.

How to launch the associated application for a file / directory / URL?

Linux seems to be easy: xdg-open <file/directory/URL>.
Apparently, Mac is similar: open should be used instead of xdg-open. I don't have access to a Mac so I couldn't test it.
For Windows, I found 4 different suggestions and those that I have tried failed.
Is there a non-java, cross platform way to launch the associated application for a certain file type?
suggests start
How to give focus to default program of shell-opened file, from Java? suggests
cmd /c start ...
How to open user system preferred editor for given file?
How to Find Out Default File Opener with Java?
suggest RUNDLL32.exe
What is the correct way to use ShellExecute() in C to open a .txt
Open file with Windows' native program within C++ code
How to use ShellExecute to open html files in Windows using C++? suggest
ShellExecute
I have tried the first 3 with system() and QProcess::startDetached() and "http://www.stackoverflow.com" as argument but they all failed; start works just fine from the command line though. I haven't tried ShellExecute yet.
What is the Windows equivalent of xdg-open? It seem to me, it is start but why did my attempts with start fail?
Is ShellExecute my only option?
EDIT I thought QDesktopServices::openUrl() was for web pages only because it did not work for files or directories.
After some debugging I figured out that if I replace \\ with / in the path on Windows, it works for files but the directories are still not opened. Any ideas what I am doing wrong?
QDir dir("C:/Documents and Settings/ali");
qDebug() << "Exists? " << dir.exists();
qDebug() << dir.absolutePath();
QDesktopServices::openUrl(QUrl(dir.absolutePath()));
qDebug() << "External app called";
Application Output:
Exists? true
"C:/Documents and Settings/ali"
External app called
But nothing happens, the directory is not opened. On Linux, directories are opened with the default file manager as expected.
SOLUTION: Due to the Qt bug and Windows quirks (malformed application window), I ended up using ShellExecute. That gives me enough flexibility to achieve exactly what I want at some expense...
Why don't you just use Qt's support for this? For example:
QDesktopServices::openUrl(QUrl("/home/realnc/test.pdf"));
This opens the document in Acrobat Reader. In general, it obeys the preferred application settings in my OS for all file types that have one or more applications associated with them. Best of all, it's platform-independent.
Edit:
The fact that it opens directories on Linux but not on Windows smells like a bug. It might be best to report this on Qt's bug tracker. In the meantime, you could have a workaround for Windows for when the file is a directory:
#ifdef Q_WS_WIN
if (QFileInfo(path).isDir())
QProcess::startDetached("explorer", QStringList(path));
else
#endif
QDesktopServices::openUrl(QUrl(path));
You can also do it with cmd.exe's start command, but you'll get an ugly terminal pop up for a few fractions of a second:
QProcess::startDetached("cmd", QStringList() << "/C" << "start"
<< QDir::toNativeSeparators(path));

visual studio 2010 issue

When I write a program using C++ and I want to run it, I can't catch the console window. I press CTRLF5 and it does not work.
I want the window to stay open and wait, even it finishes executing. Can anyone help me?
Thanks in advance.
http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5
In the older versions it would default to the console subsystem even if you selected "empty project", but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "*project_name* properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.
try using system("Pause"); as the last line on your code (before the return of your main function)
Ctrl+F5 should work. Just in case, if you have the source of your program, add the following just before the closing brace of main.
int x;
cin >> x;
the program will wait for you to enter some value.
If you want a breakpoint to be triggerred in debugger, do simple F5 instead of Ctrl+F5, after putting a breakpoint on the relevant source line (assuming the source/debug symbols are available)
Sorry to say, Ruba, but it looks like Microsoft removed this nifty little feature when moving from VS2008 to VS2010.
I can't find anything on MSDN, the web in general, or VS options to turn it back on.
My advice is to bypass the environment altogether for testing your application. Simply open a cmd.exe window in your runtime directory (debug or release or whatever), build the executable within the IDE then switch to the command window and enter testprog.exe to run your program.
Make sure you include any required command line parameters and, after you've entered it the first time, you can just use the up-arrow to retrieve the last command.
Yes, it's a bit of a pain but, until someone comes up with a better solution, it's probably the best way to ensure you see all the output while ensuring the program has shut down completely.
Just set a breakpoint at main()'s closing curly brace if you want to see the console after the program is finished.
You should create VS 2010 C++ Projects as below:
New project -> Visual C++ -> Win32 -> Win32ConsoleApplication
In this way you will be getting "Press any key to continue..." when you run program with ctrl+F5, as it was in VS 2008.
EDIT :
New project -> Visual C++ -> Win32 -> Win32ConsoleApplication -> Next -> Check 'Empty project' -> Finish = what you actually need.