Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / WTL
Article

Open a URL in a new window

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
10 Jul 2003 183.6K   1.6K   43   42
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



Comments and Discussions

 
GeneralRe: STL version Pin
Rob Caldecott17-Jul-03 2:19
Rob Caldecott17-Jul-03 2:19 
GeneralRe: STL version Pin
Owen Lawrence2-Feb-05 9:55
Owen Lawrence2-Feb-05 9:55 
GeneralNot that I want to take credit... Pin
Iain Clarke, Warrior Programmer12-Jul-03 14:12
Iain Clarke, Warrior Programmer12-Jul-03 14:12 
GeneralSlightly faster, IE only Pin
Stephane Rodriguez.11-Jul-03 21:44
Stephane Rodriguez.11-Jul-03 21:44 
GeneralRe: Slightly faster, IE only Pin
Jörgen Sigvardsson13-Jul-03 1:48
Jörgen Sigvardsson13-Jul-03 1:48 
GeneralExtracting file name from Path Pin
yarp11-Jul-03 20:12
yarp11-Jul-03 20:12 
GeneralRe: Extracting file name from Path Pin
Rob Caldecott11-Jul-03 21:08
Rob Caldecott11-Jul-03 21:08 
GeneralRe: Extracting file name from Path Pin
yarp13-Jul-03 5:07
yarp13-Jul-03 5:07 
I don't think so. Universal softwares like Netscape are professionaly done - I mean by skill developers - so we can expect them to respect path formatting rules. That's why I think that to parse a browser name the Path* functions would have been better.
This problem rather arise with secondary softwares but it's true it's a real pain in that case.

Yarp
http://www.senosoft.com/
GeneralRe: Extracting file name from Path Pin
Rob Caldecott13-Jul-03 21:21
Rob Caldecott13-Jul-03 21:21 
GeneralThis looks familiar... Pin
Ryan Binns11-Jul-03 14:46
Ryan Binns11-Jul-03 14:46 
GeneralRe: This looks familiar... Pin
hahahha12-Jul-03 5:30
hahahha12-Jul-03 5:30 

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.