Click here to Skip to main content
12,754,175 members (27,655 online)
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 10 Jul 2003

Open a URL in a new window

, 10 Jul 2003
Rate this:
Please Sign up or sign in to vote.
An article showing how to launch a URL in a new browser window
<!-- Add the rest of your HTML here -->


Ever wanted to open a URL without blatting the contents of an existing browser window?  Here's how - and it will only take you seconds.

First, include the header file:

 #include "url.h"

Then simply declare a CURL object and call the Open method:

CURL url;
url.Open(_T("<A href=""></A>"));

If you want to re-use an existing browser window, then pass false as the second parameter:

CURL url;
url.Open(_T("<A href=""></A>"), false);

That's it!  Easy - and you can use this code with any CString friendly framework (MFC, WTL, ATL7).


#pragma once

class CURL
 // The default browser
 CString m_strBrowser;
 // Open a URL
 void Open(LPCTSTR lpszURL, bool bNewWindow = true)
  if (bNewWindow)
   ::ShellExecute(NULL, NULL, GetBrowser(), lpszURL, NULL, SW_SHOWNORMAL);
   ::ShellExecute(NULL, NULL, lpszURL, NULL, NULL, SW_SHOWNORMAL);

 // Get the default browser from the registry
 LPCTSTR GetBrowser(void)
  // Have we already got the browser?
  if (m_strBrowser.IsEmpty())
   // Get the default browser from HKCR\http\shell\open\command
   HKEY hKey = NULL;
   // Open the registry
   if (::RegOpenKeyEx(HKEY_CLASSES_ROOT, _T("http\\shell\\open\\command"),
     0, KEY_READ, &hKey) == ERROR_SUCCESS)
    // Data size
    DWORD cbData = 0;
    // Get the default value
    if (::RegQueryValueEx(hKey, NULL, NULL, NULL, NULL, &cbData) 
        == ERROR_SUCCESS && cbData > 0)
     // Allocate a suitable buffer
     TCHAR* psz = new TCHAR [cbData];
     // Success?
     if (psz != NULL)
      if (::RegQueryValueEx(hKey, NULL, NULL,
       NULL, (LPBYTE)psz, &cbData) ==
       // Success!
       m_strBrowser = psz;
      delete [] psz;
   // Do we have the browser?
   if (m_strBrowser.GetLength() > 0)
    // Strip the full path from the string
    int nStart = m_strBrowser.Find('"');
    int nEnd = m_strBrowser.ReverseFind('"');
    // Do we have either quote?
    // If so, then the path contains spaces
    if (nStart >= 0 && nEnd >= 0)
     // Are they the same?
     if (nStart != nEnd)
      // Get the full path
      m_strBrowser = m_strBrowser.Mid(nStart + 1, nEnd - nStart - 1);
     // We may have a pathname with spaces but
     // no quotes (Netscape), e.g.:
     // Look for the last backslash
     int nIndex = m_strBrowser.ReverseFind('\\');
     // Success?
     if (nIndex > 0)
      // Look for the next space after the final
      // backslash
      int nSpace = m_strBrowser.Find(' ', nIndex);
      // Do we have a space?
      if (nSpace > 0)
       m_strBrowser = m_strBrowser.Left(nSpace);    
  // Done
  return m_strBrowser;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

You may also be interested in...


Comments and Discussions

QuestionRegsitry key to get default browser is wrong Pin
Ramveer Singh from Gurgaon, Haryana10-Nov-11 1:13
memberRamveer Singh from Gurgaon, Haryana10-Nov-11 1:13 
QuestionIt's not work for Safari Pin
Daniel Xu13-Jul-11 17:58
memberDaniel Xu13-Jul-11 17:58 
GeneralIts not working in vs2008 for windows6.5 mobile device Pin
Ashutosh kumar Tripathi23-Mar-11 5:09
memberAshutosh kumar Tripathi23-Mar-11 5:09 
GeneralMy vote of 2 Pin
KarstenK15-Dec-08 3:53
memberKarstenK15-Dec-08 3:53 
QuestionProblem with Running the Setup Pin
laleh_sg1-Oct-06 22:51
memberlaleh_sg1-Oct-06 22:51 
QuestioncURL in c# Pin
plz.ze28-Sep-06 3:53
memberplz.ze28-Sep-06 3:53 
AnswerRe: cURL in c# Pin
Rob Caldecott28-Sep-06 5:24
memberRob Caldecott28-Sep-06 5:24 
GeneralFirefox takes &quot;-new&quot; command line argument Pin
semmel7113-Nov-05 3:07
membersemmel7113-Nov-05 3:07 
GeneralOpen a URL with no Toolbar Pin
vtnam063495-May-05 17:31
membervtnam063495-May-05 17:31 
GeneralRe: Open a URL with no Toolbar Pin
Anonymous8-May-05 20:04
sussAnonymous8-May-05 20:04 
GeneralImproved browser path detection Pin
mnjackson19-Jan-05 3:34
membermnjackson19-Jan-05 3:34 
GeneralRe: Improved browser path detection Pin
Anonymous19-Jan-05 9:22
sussAnonymous19-Jan-05 9:22 
GeneralRe: Improved browser path detection Pin
mnjackson19-Jan-05 11:05
membermnjackson19-Jan-05 11:05 
AnswerRe: Improved browser path detection Pin
dima polyakov17-May-07 20:20
memberdima polyakov17-May-07 20:20 
GeneralRe: Improved browser path detection Pin
oferhe15-Jan-06 0:55
memberoferhe15-Jan-06 0:55 
GeneralSimpler fix Re: Improved browser path detection Pin
peas26-Aug-08 12:22
memberpeas26-Aug-08 12:22 
GeneralMakes sense only for Internet Explorer, after test Pin
Alexandru7-Dec-04 1:22
memberAlexandru7-Dec-04 1:22 
QuestionWhat will be the sub key for the AVI file Pin
Anonymous3-Oct-04 20:04
sussAnonymous3-Oct-04 20:04 
GeneralModifications for FireFox Pin
Jeremy Davis29-Jul-04 2:21
memberJeremy Davis29-Jul-04 2:21 
GeneralRe: Modifications for FireFox Pin
Robert Edward Caldecott29-Jul-04 2:44
memberRobert Edward Caldecott29-Jul-04 2:44 
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IE Pin
toshiya3-Nov-04 16:41
membertoshiya3-Nov-04 16:41 
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IE Pin
Anonymous3-May-05 9:28
sussAnonymous3-May-05 9:28 
Questionbuilder 6 version? Pin
vertygo30-Sep-03 1:11
membervertygo30-Sep-03 1:11 
AnswerRe: builder 6 version? Pin
Robert Edward Caldecott30-Sep-03 1:14
memberRobert Edward Caldecott30-Sep-03 1:14 
GeneralMuch simpler for browser path... Pin
Jai b29-Sep-03 0:39
sussJai b29-Sep-03 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 11 Jul 2003
Article Copyright 2003 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid