3

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.

1
  • CreateProcess() works differently. You cannot do it using this function. Commented Jan 30, 2016 at 10:39

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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;
}

12 Comments

You really fantastic. Well but i got a issue that it open as a tab rather than opening as new window, any solution?
In that case you need a bit of work. Cause you need to add a different command line arg for each browser. Go back to the solution with using AssocQueryString(). Check the default browser. If that's chrome, you should add --new-window for the sencond arg of CreateProcess(). If that's firefox, add -new-instance. Maybe you can add IE and others...
if (!CreateProcessA("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", // No module name (use command line) "--new - window", ........ Give me ERROR CreateProcess failed: (2)
It might be an off topic, but I added it. Pleas vote if it helps you, thx.
Thanks very much. Well can you also tell me how would i change headers?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.