سؤال

I'm trying to write a code snippet to open a URL in google chrome from my VC++ application code, while my system's default browser continues to be Internet Explorer only.

// Existing VC++ Code used in application to open the URL in Internet Explorer(Default browser)
ShellExecute (NULL, "open", pURLinfo->szURL, NULL, NULL, SW_SHOWNORMAL);

On referring to the below stackoverflow links, I arrived at the code changes below

How to launch Chrome maximized via shell execution?

// VC++ Code change to open the URL in Google Chrome
PROCESS_INFORMATION processInformation;
STARTUPINFO startupInfo;

memset(&processInformation, 0, sizeof(processInformation));
memset(&startupInfo, 0, sizeof(startupInfo));

startupInfo.cb = sizeof(startupInfo);
startupInfo.wShowWindow = SW_SHOWMAXIMIZED;

CreateProcess("C:\Program Files\Google\Chrome\Application\chrome.exe",pURLinfo->szURL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);
//VC++ Code change to open the URL in Google Chrome

I understand that the requirement to open the URL cannot be achieved via ShellExecute(), as shellExecute will open only the default browser, the CreateProcess() code builds without errors but does not open the URL in Chrome browser. Clicking on the button to the URL, nothing happens. Can anyone point us what is wrong in the CreateProcess part of the code?..

Thanks in advance!

Update:

On re-writing the CreateProcess as below, chrome browser opens, but its blank.

CreateProcess("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",pURLinfo->szURL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInformation);

Could someone throw some light on how to pass URL in CreateProcess(), as now Chrome browser opens on execution of CreateProcess().

هل كانت مفيدة؟

المحلول

Chrome wants "--" (two dash characters) on the command line in front of the URL. You would need something 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);

ShellExecute should work, too:

string program = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
string params = " -- ";
params += pURLinfo->szURL;
ShellExecute(NULL, "open", program.c_str(), params.c_str(), NULL, SW_SHOWNORMAL);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top