tags:

views:

607

answers:

4

How can I launch a URL in a NEW window using C++ (Windows only)?

The straight-forward approach seems to open a new tab in an existing browser window. (Or, if tabbed browsing is disabled, the new URL hijacks the existing browser window).

This is for a (large) desktop app, using MFC and Qt.

A: 

This is controlled by windows. The only way to explicitly tell it to open in a new browser window is to spawn the browser explicitly and give it the url.

John Weldon
That's not guaranteed to work. The spawned process is free to tell an existing process (via DDE) to open the URL in an existing window, or as a new tab in an existing window.
Andrew Medico
+2  A: 

I've used this for showing locally generated html in the default browser, in my case filename is something like "c:\temp\page.html", perhaps replacing filename with the URL might work??

ShellExecute(NULL,"open",filename,NULL,NULL,SW_SHOWNORMAL);

Updated: http://support.microsoft.com/kb/224816

How ShellExecute Determines Whether to Start a New Instance When ShellExecute looks through the registry, it looks for the shell\open subkey. If the shell\open\ddeexec key is defined, then a Dynamic Data Exchange (DDE) message with the specified application IExplore and the topic WWW_OpenURL is broadcast to all top-level windows on the desktop. The first application to respond to this message is the application that goes to the requested URL. If no application responds to this DDE message, then ShellExecute uses the information that is contained in the shell\open\command subkey to start the application. It then re-broadcasts the DDE message to go to the requested URL.

So it looks like you have no control over opening a new window. Whatever browser currently running can handle opening it in whatever way they want.

KPexEA
I almost posted the same answer, but that wont necessarily open a new window. For example, on my machine it opens the url in a new tab in Firefox. But still, +1 for probably being good enough
Joe
A: 

Here's a link to some code that will open a URL in a new browser. The code looks up the default application for handling an HTML document and then explicitly opens that application with a ShellExecute call.

Naaff
A: 

You can't in general. The user's browser is free to do whatever the user wants it to do.

One way to achieve your desired effect might be to embed a particular browser in a window of your own (say, the IE ActiveX control) and have that render your URL.

Andrew Medico