Create Process default browser - c++

I'm currently using ShellExecute "open" to open a URL in the user's browser, but running into a bit of trouble in Win7 and Vista because the program runs elevated as a service.
I want to get thread id so ShellExecute is not possible to get thread id so i started using "CreateProcess" But i don't see any help on opening default browser whichever is it from CreateProcess.

You can use other features of the Shell API to figure out which browser is the default.
For instance, you can ask the shell for the executable name associated with .html files using AssocQueryString, and launch that via CreateProcess.

Here you can open the URL with your default browser.
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CStringA command_line;
command_line.Format("cmd.exe /c start \"link\" \"%s\"", "http://www.google.com");
if (!CreateProcessA(NULL, // No module name (use command line)
command_line.GetBuffer(),
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
TRACE("CreateProcess failed (%d).\n", GetLastError());
return;
}

Here is another answer as you wanted. As far as you want it open as a new window. You can expand it for IE, opera, and so on...
DWORD size = MAX_PATH;
char buff[MAX_PATH];
int err = AssocQueryStringA(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_EXECUTABLE, ".html", NULL, buff, &size);
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
CStringA command_line;
CStringA target_url( "http://google.com/" );
if( strcmp( "chrome.exe", PathFindFileNameA(buff)) == 0 )
command_line.Format("%s --new-window %s", buff, target_url);
else if( strcmp( "firefox.exe", PathFindFileNameA(buff)) == 0 )
command_line.Format("%s -new-instance %s", buff, target_url);
else
command_line.Format("%s %s", buff, target_url);
if (!CreateProcessA(NULL, // No module name (use command line)
command_line.GetBuffer(),
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
//... error handling
return;
}

Related

Why does the CreateProcess() function not work?

I am trying to call the CreateProcess function to execute some powershell command. The function doesnt return any errors but it doesnt execute the command (there is no file created after running the app). Here is the code.
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcess(NULL, // No module name (use command line)
const_cast<LPTSTR>(TEXT("powershell.exe -command echo Hello > D:\\out.txt")), // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
std::cout << ("CreateProcess failed (%d).\n", GetLastError());
}
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);

C++ CreateprocessA Failed Error

I use this code but i get a failed error: 2 i know that means the file doesn't exist but no idea what am doing wrong.
DWORD size = 1024;
TCHAR buff[1024];
int err = AssocQueryString(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_EXECUTABLE, _T(".html"), NULL, buff, &size);
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
LPCSTR BrowLoc = (LPCSTR)buff;
if (!CreateProcessA(BrowLoc, // No module name (use command line)
NULL,
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
getchar();
return 0;
}
I guess you are using unicode. In that case, force using multi-byte character for AssocQueryString().
Use the code below for the first 3 lines.
DWORD size = 1024;
char buff[1024];
int err = AssocQueryStringA(ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR_EXECUTABLE, ".html", NULL, buff, &size);

Failing to open a chrome browser via CreateProcess API

I am trying to open a chrome browser via CreateProcess API. I was not able to do so.
I tried to do like this:
string commandLine = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"";
commandLine += " -- ";
commandLine += pURLinfo->szURL;
CreateProcess(commandLine.c_str(), NULL, NULL, NULL, FALSE,
CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);
CreateProcess returned error 123.
Perhaps there is another way to open it. (i wasn't talking about ShellExecute).
Update: My code now looks like this and still i cannot run chrome.
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// Start the child process.
if (!CreateProcessA("C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe", // No module name (use command line)
NULL,
NULL, // Process handle not inheritable
NULL, // Thread handle not inhberitable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
getchar();
return 0;
}
Try to remove unneeded quotes from command line
string commandLine = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";

CreateProcess function issue

I'm trying to start an process from my code with createprocess function. The command line is ok, and the other exe, i can start it without problems from visual studio.
When I am trying to start it from the other process with createprocess it gives me - Runtime error this app has requested the runtime to terminate it in an unusual way.
What could be the problem ? How could I remove this problem ?
STARTUPINFO si;
PROCESS_INFORMATION pi;
bool bResult;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Cast System::String* __gc to char*
char* chAppName = (char*)(void*)Marshal::StringToHGlobalAnsi(AppName);
char* chCmdLine = (char*)(void*)Marshal::StringToHGlobalAnsi(CmdLine);
//Start the child process.
bResult = CreateProcess( chAppName, // No module name (use command line)
chCmdLine, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi); // Pointer to PROCESS_INFORMATION structure
Try eliminating the StringToHGlobalAnsi calls and hard coding them instead first. If that stops the problems, we can then sort out the StringToHGlobalAnsi calls.

using CreateProcess with a relative path

Is it possible to pass a relative path to create my child process?
This code will compile, but it gives an error because I'm using a relative path.
void Cminivideo3App::creerChildProcess(void)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
int retvalue = CreateProcess( TEXT("\..\Debug\traitement.exe"), // No module name (use command line)
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure
);
int lastError = GetLastError();
}
Couple things:
As #Oswald says, \ is the root folder of the current drive, not a relative path.
You forgot to escape your backslashes. You really want TEXT("..\\Debug\\traitement.exe").
It does not look like a relative path to me. \ is the root folder of the current drive.