Click here to Skip to main content
Licence 
First Posted 10 Jul 2003
Views 119,867
Bookmarked 40 times

Open a URL in a new window

By | 10 Jul 2003 | Article
An article showing how to launch a URL in a new browser window

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

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRegsitry key to get default browser is wrong PinmemberRamveer Singh from Gurgaon, Haryana0:13 10 Nov '11  
QuestionIt's not work for Safari PinmemberDaniel Xu16:58 13 Jul '11  
GeneralIts not working in vs2008 for windows6.5 mobile device PinmemberAshutosh kumar Tripathi4:09 23 Mar '11  
GeneralMy vote of 2 PinmemberKarstenK2:53 15 Dec '08  
QuestionProblem with Running the Setup Pinmemberlaleh_sg21:51 1 Oct '06  
QuestioncURL in c# Pinmemberplz.ze2:53 28 Sep '06  
AnswerRe: cURL in c# PinmemberRob Caldecott4:24 28 Sep '06  
GeneralFirefox takes "-new" command line argument Pinmembersemmel712:07 13 Nov '05  
GeneralOpen a URL with no Toolbar Pinmembervtnam0634916:31 5 May '05  
GeneralRe: Open a URL with no Toolbar PinsussAnonymous19:04 8 May '05  
GeneralImproved browser path detection Pinmembermnjackson2:34 19 Jan '05  
GeneralRe: Improved browser path detection PinsussAnonymous8:22 19 Jan '05  
GeneralRe: Improved browser path detection Pinmembermnjackson10:05 19 Jan '05  
AnswerRe: Improved browser path detection Pinmemberdima polyakov19:20 17 May '07  
GeneralRe: Improved browser path detection Pinmemberoferhe23:55 14 Jan '06  
GeneralSimpler fix Re: Improved browser path detection Pinmemberpeas11:22 26 Aug '08  
GeneralMakes sense only for Internet Explorer, after test PinmemberAlexandru0:22 7 Dec '04  
QuestionWhat will be the sub key for the AVI file PinsussAnonymous19:04 3 Oct '04  
GeneralModifications for FireFox PinmemberJeremy Davis1:21 29 Jul '04  
GeneralRe: Modifications for FireFox PinmemberRobert Edward Caldecott1:44 29 Jul '04  
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IE Pinmembertoshiya15:41 3 Nov '04  
GeneralRe: Modifications for FireFox, Mozilla, Netscape, Opera, IE PinsussAnonymous8:28 3 May '05  
Questionbuilder 6 version? Pinmembervertygo0:11 30 Sep '03  
AnswerRe: builder 6 version? PinmemberRobert Edward Caldecott0:14 30 Sep '03  
GeneralMuch simpler for browser path... PinsussJai b23:39 28 Sep '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 11 Jul 2003
Article Copyright 2003 by Rob Caldecott
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid