Open in Explorer - c++

How do you open a path in explorer by code in c++. I googled and couldn't find any thing but systems commands to do this, however, i dont want it to block or show the console window.

You probably are looking for the ShellExecute() function in shellapi.h. It is called with an "action verb", a path, and optional parameters. In your case this will want either "open" or "explore" as follows:
ShellExecuteA(NULL, "open", "C:\\", NULL, NULL, SW_SHOWDEFAULT);
This will open an unattached explorer window at C:. ShellExecute() will give basically the same action as typing in a command at the Run dialog. It will also handle URLs, so the following will open up the user's default browser:
ShellExecuteA(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWDEFAULT);
Although make sure to pay attention to the note in the documentation that ShellExecute relies on COM (Although your code doesn't have to worry about any COM objects itself).
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

This does not show the command window, just opens the directory.
system("explorer C:\\");

I'm now using VS2017, using as follows works:
ShellExecute(NULL, L"open", L"YourFolderPath\\YourFile.xxx", NULL, NULL, SW_RESTORE);
also reference ShellExecute to open an .exe in C++

Related

How to select a file from windows file explorer

Is there a way to use ShellExecute and grab the file (or rather the file path) and save it in a variable upon selecting it?
Should I use a different function all together?
I've looked at some of the other similar functions for opening the windows file explorer and all of them need a path first, before opening the window.
ShellExecute(NULL, L"explore", NULL, NULL, NULL, SW_SHOWNORMAL);
My goal is to open the file explorer (like ShellExecute does) and select one file, plug the path of that file into a variable and do a thing with it later.
Use IFileDialog, IFileOpenDialog or IFileSaveDialog

ShellExecute doesn't work for other computer

I made a program using Win32Api. In the program, it has "ShellExecute" phrase in order to execute chrome for searching.
Following is a abstraction fo what i wrote.
#include <windows.h>
#include <shellapi.h>
int main () {
ShellExecute(NULL, L"open", searching_url, NULL, NULL, SW_SHOWMAXIMIZED);
}
It works in my computer. But, I sent this windows app to other people and they said it doesn't work. I mean only shellexecute doesn't work. I cannot catch where to start figuring out what's wrong here.
Would you please suggest How I figure it out in structured way?
Thanks.
Two things to try:
ShellExecute can require COM to be initialized under certain circumstances. The docs for the ShellExecute function explain why in more detail. You should call CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE) before the call to ShellExecute and CoUninitialize() afterwards.
Instead of using L"open" as the verb, you should generally use NULL. NULL will always give you the "default" action for an object (equivalent to double-clicking it) which is not always "open".

Why does ShellExecute()ing cmd.exe hidden works?

When trying to execute cmd.exe with the SW_HIDE paramter:
ShellExecute(NULL, "open", "cmd.exe", NULL, NULL, SW_HIDE);
cmd.exe is actually executed hidden! I don't understand why this happened, does cmd.exe's console window considered to be a "normal" window that accepts the nCmdShow parameter, or does cmd.exe when executed sees that I passed SW_HIDE for the nCmdShow parameter and decides not to show the console window?
Note: I tried this code on Windows 7.
The implication is that cmd.exe simply uses the nCmdShow argument to WinMain (or that it uses SW_SHOWDEFAULT which means the same thing) when showing its window via ShowWindow, without checking or modifying the value first.
It's not documented that it will work and so you shouldn't rely on it, but it's a nice side effect that can be a good way to run batch scripts without a visible window appearing.
Of course if you actually launch an interactive cmd.exe in a hidden window it will be quite difficult to ever make it visible :)

Open site through ShellExecute without losing focus on main window

In my code I open site with
ShellExecute(NULL, "open", "http://google.pl", NULL, NULL, SW_MINIMIZE);
It is almost perfect but after browser initialize and open link , main game window lose focus and browser windows is now on top.
I would like to open link in browser but game window should stay on top without minimizing etc.
Browser window should be opened but in the background.
I tried SW_SHOWNA , SW_HIDE , SW_MINIMIZE nothing works.

Custom Run Dialog Input

How would I go about starting a Run Dialog window and having my application input a command to start Steam?
I would like my application to open the standard windows run dialog pre-filled with input.
This is what I have tried:
HINSTANCE result;
result = ShellExecute(NULL, "open", "rundll32.exe", "steam://connect/192.69.96.168:27023", NULL, SW_SHOWDEFAULT);
I don't really understand the parameters for ShellExecute and would like some clarification.
UPDATE
I think I have gotten it to work:
HINSTANCE result;
result = ShellExecute( // WinExec is obsolete.
0, // hwnd
"open", // operation/verb.
"steam://connect/192.69.96.168:27023", // executable.
"", // parameters.
NULL, // directory
SW_SHOW); // how to be displayed
Have a look here, taken from this related question. This will open the run dialog.
Then if your process has the admin rights, you might be able to call SetWindowText on the handle of the input box of the dialog, which you'll find with FindWindowEx or similar.
I really think it's easier to create your own run dialog which looks like the windows one and calls ShellExecute when "ok" is clicked.