I'm sure there's someway to make CreateProcess
and ShellExecute
work with this, but I suspect the simplest way to do this will be good old system
, e.g. system("explorer /n, /select,c:\\123.doc")
.
Just because it was bugging me, I went ahead and wrote a simple program that does this with CreateProcess:
#define UNICODE
#include <windows.h>
#include <string>
void SimpleWriteConsole(std::wstring msg) {
DWORD written = 0;
WriteConsole( GetStdHandle(STD_OUTPUT_HANDLE),
msg.c_str(), msg.length(), &written, NULL);
}
int wmain(int argc, wchar_t **argv, wchar_t **envp) {
SimpleWriteConsole(L"Opening explorer...\n");
std::wstring commandLine = L"explorer /n, /select,";
if( argc < 2 ) {
SimpleWriteConsole(L"Please include a file to select.\n");
return EXIT_FAILURE;
}
commandLine += argv[1];
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
PROCESS_INFORMATION procInfo;
if( ! CreateProcess(NULL, const_cast<wchar_t*>(commandLine.c_str()),
NULL, NULL, 0, 0, NULL, NULL, &startupInfo, &procInfo) ) {
SimpleWriteConsole(L"Couldn't create process :(\n");
return EXIT_FAILURE;
}
CloseHandle( procInfo.hThread );
CloseHandle( procInfo.hProcess );
SimpleWriteConsole(L"Hooray launched explorer.\n");
return EXIT_SUCCESS;
}
It takes the C:\abc.txt part as a parameter on the command line. There's no extra dos box, and doesn't eat your existing process (exec is supposed to do that, btw) and it doesn't use a deprecated API.