Click here to Skip to main content
Click here to Skip to main content

Open a URL in a new window

By , 10 Jul 2003
 
<!-- Add the rest of your HTML here -->

Introduction

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="http://www.codeproject.com/">http://www.codeproject.com/</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="http://www.codeproject.com/">http://www.codeproject.com/</A>"), false);

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

CURL

#pragma once

class CURL
{
private:
 // The default browser
 CString m_strBrowser;
public:
 // Open a URL
 void Open(LPCTSTR lpszURL, bool bNewWindow = true)
 {
  if (bNewWindow)
   ::ShellExecute(NULL, NULL, GetBrowser(), lpszURL, NULL, SW_SHOWNORMAL);
  else
   ::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) ==
       ERROR_SUCCESS)
      {
       // Success!
       m_strBrowser = psz;
      }
      delete [] psz;
     }
    }
    ::RegCloseKey(hKey);
   }
   // 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);
     }
    }
    else
    {
     // We may have a pathname with spaces but
     // no quotes (Netscape), e.g.:
     //   C:\PROGRAM FILES\NETSCAPE\COMMUNICATOR\PROGRAM\NETSCAPE.EXE -h "%1"
     // 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;
 }
};

License

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

Rob Caldecott
Architect
United Kingdom United Kingdom
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRegsitry key to get default browser is wrongmemberRamveer Singh from Gurgaon, Haryana10-Nov-11 0:13 
QuestionIt's not work for SafarimemberDaniel Xu13-Jul-11 16:58 
GeneralIts not working in vs2008 for windows6.5 mobile devicememberAshutosh kumar Tripathi23-Mar-11 4:09 
GeneralMy vote of 2memberKarstenK15-Dec-08 2:53 
QuestionProblem with Running the Setupmemberlaleh_sg1-Oct-06 21:51 
QuestioncURL in c#memberplz.ze28-Sep-06 2:53 
AnswerRe: cURL in c#memberRob Caldecott28-Sep-06 4:24 
GeneralFirefox takes &quot;-new&quot; command line argumentmembersemmel7113-Nov-05 2:07 
GeneralOpen a URL with no Toolbarmembervtnam063495-May-05 16:31 
GeneralRe: Open a URL with no ToolbarsussAnonymous8-May-05 19:04 
GeneralImproved browser path detectionmembermnjackson19-Jan-05 2:34 
GeneralRe: Improved browser path detectionsussAnonymous19-Jan-05 8:22 
GeneralRe: Improved browser path detectionmembermnjackson19-Jan-05 10:05 
AnswerRe: Improved browser path detectionmemberdima polyakov17-May-07 19:20 
GeneralRe: Improved browser path detectionmemberoferhe14-Jan-06 23:55 
GeneralSimpler fix Re: Improved browser path detectionmemberpeas26-Aug-08 11:22 
GeneralMakes sense only for Internet Explorer, after testmemberAlexandru7-Dec-04 0:22 
QuestionWhat will be the sub key for the AVI filesussAnonymous3-Oct-04 19:04 
GeneralModifications for FireFoxmemberJeremy Davis29-Jul-04 1:21 
GeneralRe: Modifications for FireFoxmemberRobert Edward Caldecott29-Jul-04 1:44 
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IEmembertoshiya3-Nov-04 15:41 
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IEsussAnonymous3-May-05 8:28 
Questionbuilder 6 version?membervertygo30-Sep-03 0:11 
AnswerRe: builder 6 version?memberRobert Edward Caldecott30-Sep-03 0:14 
GeneralMuch simpler for browser path...sussJai b28-Sep-03 23:39 
GeneralRe: Much simpler for browser path...memberRobert Edward Caldecott28-Sep-03 23:45 
GeneralRe: Much simpler for browser path...memberDavidCrow1-Oct-03 5:06 
GeneralUsing Existing browser session is not workingmemberNETLearner21-Jul-03 9:25 
GeneralRe: Using Existing browser session is not workingmemberRobert Edward Caldecott22-Jul-03 22:01 
QuestionRe: Using Existing browser session is not workingmemberzixin_yu25-Feb-08 20:26 
GeneralSTL versionmemberSungJun Hong17-Jul-03 2:03 
GeneralRe: STL versionmemberRobert Edward Caldecott17-Jul-03 2:19 
GeneralRe: STL versionmemberOwen Lawrence2-Feb-05 9:55 
GeneralNot that I want to take credit...memberIain Clarke12-Jul-03 14:12 
GeneralSlightly faster, IE onlymemberStephane Rodriguez.11-Jul-03 21:44 
GeneralRe: Slightly faster, IE onlymemberJörgen Sigvardsson13-Jul-03 1:48 
GeneralExtracting file name from Pathmemberyarp11-Jul-03 20:12 
GeneralRe: Extracting file name from PathmemberRobert Edward Caldecott11-Jul-03 21:08 
GeneralRe: Extracting file name from Pathmemberyarp13-Jul-03 5:07 
GeneralRe: Extracting file name from PathmemberRobert Edward Caldecott13-Jul-03 21:21 
GeneralThis looks familiar...memberRyan Binns11-Jul-03 14:46 
GeneralRe: This looks familiar...memberhahahha12-Jul-03 5:30 

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

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