🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.web
Article #94

Re: calling default browser on windows

#94
From: Colin B Maharaj
Date: Thu, 17 May 2007 12:26
152 lines
5736 bytes
Daniel F wrote:
> "Daniel F" <nospam@spamIsEvil.com> wrote:
>> Hi all,
>> i need to call the default browser on windows and open a 
>> web page.
>
Because we live in a democracy, another thing you can do with two 
browsers is find a way to launch either.  Place two Speed buttons on a 
form and prototype these two functions (source way below).

bool HKCRRegKeyExists(char *reg, AnsiString & DefaultValue);
bool HKLM_CurrentVersion(char *reg, char *key, AnsiString & Value);


//---------------------------------------------------------------------
// Setup the buttons if firefox or IE exists and show them.
//---------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    AnsiString VER;
    SpeedButton1->Visible =
    HKLM_CurrentVersion("SOFTWARE\\Mozilla\\Mozilla Firefox", 
"CurrentVersion", VER);
    SpeedButton1->Hint = (AnsiString) "Launch firefox : v" + VER;

    SpeedButton2->Visible =
    HKLM_CurrentVersion("SOFTWARE\\Microsoft\\Internet Explorer", 
"Version", VER);
    SpeedButton2->Hint = (AnsiString) "Launch IE : v" + VER;

}

//---------------------------------------------------------------------------
// Firefox launch.
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
   AnsiString A, C;
   A = "SOFTWARE\\Mozilla\\Mozilla Firefox";
   if (HKLM_CurrentVersion(A.c_str(), "CurrentVersion", C)==true)
   {
     A = A + "\\" + C + "\\Main";
     if (HKLM_CurrentVersion(A.c_str(), "PathToExe", C)==true)
     {
       AnsiString B = "http://www.google.com";
       ShellExecute(this->Handle, "open", C.c_str(), B.c_str(),
       NULL, SW_SHOWNORMAL);
     }
   }
}

//---------------------------------------------------------------------------
// IE Launch.
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
  AnsiString A;
  if 
(HKCRRegKeyExists("Applications\\iexplore.exe\\shell\\open\\command", 
A)==true)
  {
    int p = A.Pos("%1");
    if (p)
    {
      A.SetLength(p-1);
      A = Trim(A);
    }
    AnsiString B = "http://www.google.com";
    B =  (AnsiString) "\"" + B + "\"";
    ShellExecute(this->Handle, "open", A.c_str(), B.c_str(),
    NULL, SW_SHOWNORMAL);
  }
}



//---------------------------------------------------------------------
// This code was meant to query the versions of the browsers.
// This is placed in Value. Returns false if not found.
//---------------------------------------------------------------------

bool HKLM_CurrentVersion(char *reg, char *key, AnsiString & Value)
{
    HKEY hKey;
    DWORD keyType;
    DWORD retValue;
    DWORD keyLength;
    retValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg, NULL, KEY_READ, &hKey);
    if (retValue != ERROR_SUCCESS)
    {
      return false;
    }

    char *t = new char [MAX_PATH];
               RegQueryValueEx(hKey, key, NULL, &keyType, NULL, 
&keyLength);
    retValue = RegQueryValueEx(hKey, key, NULL, &keyType, (UCHAR *)t, 
&keyLength);
    RegCloseKey(hKey);

    if (retValue != ERROR_SUCCESS)
    {
       delete t;
       return false;
    }
    if (keyLength < MAX_PATH) t[keyLength]    = '\0';
    else                      t[MAX_PATH - 1] = '\0';
    Value = t;
    delete t;
    return true;
}

//---------------------------------------------------------------------
// This code was used to get the path of the EXEs.
// Who knows it might break in later versions of the
// respective browsers. Returns false if not found.
//---------------------------------------------------------------------

bool HKCRRegKeyExists(char *reg, AnsiString & DefaultValue)
{
    HKEY hKey;
    DWORD keyType;
    DWORD retValue;
    DWORD keyLength;
    retValue = RegOpenKeyEx(HKEY_CLASSES_ROOT, reg, NULL, 
KEY_QUERY_VALUE, &hKey);
    if (retValue != ERROR_SUCCESS)
    {
      return false;
    }

    char *t = new char [MAX_PATH];

    RegQueryValueEx(hKey, NULL, NULL, &keyType, NULL, &keyLength);
    retValue = RegQueryValueEx(hKey, NULL, NULL, &keyType, (UCHAR *)t, 
&keyLength);

    RegCloseKey(hKey);

    if (retValue != ERROR_SUCCESS)
    {
       delete t;
       return false;
    }
    if (keyLength < MAX_PATH) t[keyLength]    = '\0';
    else                      t[MAX_PATH - 1] = '\0';
    DefaultValue = t;
    delete t;
    return true;
}

//---------------------------------------------------------------------
Technical Details
Message-ID: <464c4a12$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4631c336$1@newsgroups.borland.com> <4631c649$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: feeec,a6e9d3cf6054e910
X-Google-Attributes: gidfeeec,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Thu, 17 May 2007 08:26:59 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.web
Subject: Re: calling default browser on windows
References: <4631c336$1@newsgroups.borland.com> <4631c649$1@newsgroups.borland.com>
In-Reply-To: <4631c649$1@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Antivirus: avast! (VPS 000740-2, 05/16/2007), Outbound message
X-Antivirus-Status: Clean
NNTP-Posting-Host: 200.108.25.156
Message-ID: <464c4a12$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1179404818 200.108.25.156 (17 May 2007 05:26:58 -0700)
Lines: 165
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail