Insert item to desktop right-click menufrom shell extension - c++

I've written a windows explorer shell extension using VS2010 and ATL, registered under HKLM\Directory\Background\shellex\ContextMenuHandlers to add my commands under context menu.
All is working as expected, but I want to add a context-menu entry which shows up if and only if user has right-clicked on desktop, like windows 7 defaults "gadgets, personalize, etc".
I've tried this, but does not work (does not match):
if (GetForegroundWindow () == GetDesktopWindow ()) {
// code here
}
Any toughts on how this can be done ?
I've also tried using SHGetFolderPath() with CSIDL_DESKTOPDIRECTORY and do a _tcsicmp(), this works, but does show up even if desktop directory is opened in a normal windows explorer window, which I don't want.

Register it under HKCR\DesktopBackground\shellex\ContextMenuHandlers

Related

Run AHK script as admin without hitting "run as administrator"

Is it possible to have an AHK script instantly run as admin without me having to right-click on it? I would like to have the UAC prompt pop up when I double-click the script.
(Sorry if I am bad at explaining I am a beginner)
You this snippet from the documentation to automatically relaunch any script as admin (if it already wasn't ran as admin).
full_command_line := DllCall("GetCommandLine", "str")
if (!(A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)")))
{
try
{
if (A_IsCompiled)
Run *RunAs "%A_ScriptFullPath%" /restart
else
Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
}
ExitApp
}
Just make sure this is in the auto-execute section (the top of the script).
Create a shortcut to the AHK script (or to the compiled EXE that you made from it). In the properties for the shortcut, select the SHORTCUT tab, and click on the ADVANCED button. On the popup, put a check in the RUN AS ADMINISTRATOR box, then click OK, APPLY, OK

No console application on Linux

On Windows I normally create a Windows Desktop Application, this is because console applications display a brief black box on the screen.
I am using CodeBlocks on Linux Mint, how could I do the equivalent of the above but on Linux?
I do not want to hide the terminal window after it has been displayed.
Linux does not have the same "subsystem" concept as windows: there is no difference or separation between console and desktop applications. When you start an application on Linux, it will not open a console window, unless the programmer explicitly programmed it to open one.
If the application writes anything to stdout or stderr, what happens with that depends on how exactly the application got started. By default, the application inherits the stdout and stderr of its parent process. If the application is being started from a terminal, the output will be visible on the terminal. If the application was started by the desktop environment from a menu entry, the output may go to a log file or it may be lost.
If you see a terminal window open when you run your program from the IDE, that's something that the IDE is doing for you, it's not your application. If it bothers your, I would think that the IDE has a way to disable this behavior in settings.
Look into QT. It is a GUI framework that works on Linux.
You can write your code without creating a main window (or maybe you have to have a main window but it can be always hidden... it's been a while since I've used it).
Be aware though, that you may run into usability issues with this type of design... the user has no way of knowing if your app was launched or if it succeeded, when it's completed, etc.
The simple way is to use (e.g.) xterm [or gnome-terminal] to get a terminal window.
Then, invoke your program from the shell [manually]:
/path_to_my_program
You may be able to configure codeblocks to do this for you.
Or, you could add some code that holds the default window open:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid = fork();
if (pid != 0) {
waitpid(pid,NULL,0);
while (1) sleep(1);
}
long double n;
n=5;
printf("n= %Lf\n",n);
return 0;
}
UPDATE:
The invocation command can be controlled from: Settings -> Environment -> General Settings
The default is to invoke in an xterm sub-window [popup]. You may be able to change the settings to (re)use an existing [terminal] window.
Note that [a codeblocks program] cb_console_runner is used. You may be able to replace this with something more to your liking.
I do not want a GUI nor a terminal popup...
You'll need some sort of terminal to run the command in. This could be a script that diverts stdin/stdout/stderr as appropriate [and suppresses the invocation of a sub-window], so you'll have to experiment a bit.
As I mentioned above, you can just open a terminal window outside of codeblocks and then run the command manually inside it. Technically, this is not a popup. But, you lose the [automatic] debugger invocation.

Show contents of C:\Windows\System32\config using 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

Installing MinGW's gdb.exe for Code Blocks

Hey guys I really need help.
I have recently started programming C++ again and I am trying to get Code Blocks up and running but it keeps failing because of this error.
"ERROR: You need to specify a debugger program in the debuggers's settings."
Understandably this means there isn't a debugger program so I have to download one. I tried to find the gdb.exe for MinGW but I can't find it at all. I just end up with the MinGW's Installation Manager and being left confused.
Are there any tutorials about using the MinGW written for beginners or anyone willing to help?
Thanks in advance.
Steps to add gdb.exe
Open MinGW Installation Manager
select package "mingw32-gdb" from the list.
Then select "Apply Changes" in Installation menu.
This will automatically install gdb.exe in the path C:\MinGW\bin.
Please let me suggest you debugger program TDM-GCC. For download and install visit
http://wiki.codeblocks.org/index.php?title=MinGW_installation
Because a debugger program (toolchain) is very complex undertaking, is not standard in Code::Blocks. You must download it and install on Code::Blocks.
Note: It is impossible copy and paste images here. In the case linked page changes, I, or another one, should suggest new link.
However following are instructions according to images and my experience.
Download TDM-GCC from https://jmeubank.github.io/tdm-gcc/download/ click [tdm64-gcc-10.3.0-2.exe] and run it.
In Wizard Action window - Click [Create].
In Select Edition window - If MinGW/TDM (32-bit) is not checked, check it. Click [Next>].
In Licence Changes window - Click [Next>].
In New Instalation: Instalation Directory window - If text box is "C:\TDM-GCC-32", it's okay. If is not, click [Browse ...], and navigate: C:\ > TDM-GCC-32 > click [OK]. Now text box is C:\TDM-GCC-32. Click [Next>].
If a small window appears saying The directory TDM-GCC-32 is not empty! Are you sure you want to install here?. Click [Yes].
In New Installation: Download Mirror window - Click [Next>].
In New Installation: Choose Components window - Check whether Add to PATH box is checked. Actually is checked. Click [Install].
In Installing window - Wait till [Next] become black. Then click it. Even if Instalation Failed window appears click [Next].
In Completing the TDM-GCC Setup Wizard window - Click [Finish].
Go to Code Blocks > Settings > Compiler.
10a. In Global compiler settings window click "Toolchain executables". Just below, find "Compiler's installation directory". If the below text field is C:\TDM-GCC-32, it's okay. If not, click the [...] at the end of the field. In "Select directory" wizard, navigate to C:\TDM-GCC-32. Now "Folder:" should be TDM-GCC-32. Click [Select a folder] in "Select directory" wizard.
10b. In Global compiler settings. Below red "Note:..." find "Program Files" > "Debuger". If text field reads "GDB/CDB debuger : Default", it's okay, click [OK]. If not, click the down arrow at the end of the field > choose "GDB/CDB debuger : Default". Check that text field is "GDB/CDB debuger : Default". Click [OK].
Go to Code Blocks > Settings > Debugger... . In Common window, left list, click "Default". If "Executable path:"'s text field is "C:\TDM-GCC-32\bin\gdb32.exe", it's okay, click [OK]. If not, click [...] button and navigate C: > TDM-GCC-32 > bin > click gdb32 (or gdb32.exe) file > click [open] in "Select executable file" wizard. "Executable path:" should be C:\TDM-GCC-32\bin\gdb32.exe. Click [OK]. Now you can debug your program.
If Debugger does not function, close Code Blocks and restart them.
Regards.
Hey guys I suggest a better way. Download "gdb.exe" from the Internet and copy it to the below path.
C:\MinGW\Bin
It's really works. I did it.

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.