C# IE toolbar: how to develop a toolbar object

Hi,

As I'm programming in C#, I'm using theDeskBand objecthttp://www.codeproject.com/csharp/dotnetbandobjects.asp, rather than the MSDN.net kbbar sample which is written in managed C++.

I would like to make the toolbar visible after its installation. Do you know a way to do this?

I tried to use code like this:
Object oShow = true;
Object dummy = false;
Object vGuid = t.GUID.ToString("B"); // or this.GetType().GUID...
Explorer.ShowBrowserBar( ref vGuid, ref oShow, ref dummy);

but the problem is: where do I put this code?! If I put it in the SetSite method, it is called when the toolbar is displayed, so too late! Ideally, I would have liked to make it visible in the Register method, but unfortunately, the Explorer is not initalized at this step :-(
I found a C++ function that could be a way of doing it, but I don't manage to translate it into C#! This is this function:
STDAPI ShowToolbar(bool enable)
{
BOOL bShow = TRUE;
IWebBrowser2 *pIE;
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer, NULL,
CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2,(void**)&pIE);

VARIANT vtBandGUID;
VARIANT vtShow;
VARIANT vtNotUsed;
vtBandGUID.vt = VT_BSTR;
vtBandGUID.bstrVal = SysAllocString( L"{YOUR GUID HEAR}" );
vtShow.vt = VT_BOOL;
vtShow.boolVal = bShow;
vtNotUsed.vt = VT_INT;
vtNotUsed.intVal = 1;
hr = pIE->ShowBrowserBar(&vtBandGUID, &vtShow,
&vtNotUsed);
SysFreeString(vtBandGUID.bstrVal);
pIE->Release ();
return S_OK;
}

Do you have any idea?
Thanks a lot!

Thibaud

[3887 byte] By [Thibaud] at [2008-1-28]
# 1

Hi,

best practice is to write a BHO which will load your BandObject.

A BHO has to implement IObjectWithSite

IObjectWithSite.cs

using System;
using
System.Runtime.InteropServices;
namespace
<YOUR_NAMESPACE_HERE>
{
[C
omVisible(true)]
[
InterfaceType(ComInterfaceType
.InterfaceIsIUnknown)]
[
Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352"
)]
public interface IObjectWithSite
{
[
PreserveSig]int SetSite([MarshalAs(UnmanagedType.IUnknown)] object site);
[
PreserveSig]int GetSite(ref Guid guid, out IntPtr ppvSite);
}
}

BHO.cs

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using SHDocVw;

namespace <YOUR_NAMESPACE_HERE>
{

[ComVisible(true)]
[
Guid("<YOUR_GUID_HERE>")]
[ClassInterface(ClassInterfaceType.None)]

public class BHO : IObjectWithSite
{
private InternetExplorer explorer;

#region ComRegisterFunction
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

[ComRegisterFunction]
public static void RegisterBHO(Type t)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
if (key == null)
{
key = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
}

string guidString = t.GUID.ToString("B");
RegistryKey bhoKey = key.OpenSubKey(guidString, true);

if (bhoKey == null)
{
bhoKey = key.CreateSubKey(guidString);
}

// NoExplorer:dword = 1 prevents the BHO to be loaded by Explorer
string _name = "NoExplorer";
object _value = (object)1;
bhoKey.SetValue(_name, _value);
key.Close();
bhoKey.Close();
}

[ComUnregisterFunction]
public static void UnregisterBHO(Type t)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
string guidString = t.GUID.ToString("B");

if (key != null)
{
key.DeleteSubKey(guidString,
false);
}
}

#endregion

#region IObjectWithSite Members
public int SetSite(object site)
{
if (site != null)
{
explorer = (
InternetExplorer)site;
ShowBrowserBar(
true);
}
return 0;
}

public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(explorer);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}

#endregion

#region Private Functions

private void ShowBrowserBar(bool bShow)
{
object pvaClsid = (object)(new Guid("GUID_OF_YOUR_BANDOBJECT_HERE").ToString("B"));
object pvarShow = (object)bShow;
object pvarSize = null;

if (bShow) /* hide Browser bar before showing to prevent erroneous behavior of IE*/
{
object pvarShowFalse = (object)false;
explorer.ShowBrowserBar(
ref pvaClsid, ref pvarShowFalse, ref pvarSize);
}

explorer.ShowBrowserBar(ref pvaClsid, ref pvarShow, ref pvarSize);
}

#endregion

}
}

BeatScholl at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 2
Hi Beat,

thank you for your answer. I had heard of BHOs but didn't think of using one to make the toolbar visible. In fact I was about to make a small

C++ (but now I can do it in C# thanks to your code!) executable that would be called by the installer, just after the

installation of the toolbar, and would call ShowBrowserBar.
I'm afraid if I use a BHO, it could be recognized as a spyware stuff and removed by antispyware and antivirus programs! What's your opinion about that?

Thanks again
Thibaud

Thibaud at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 3

Hi Thiebaud,

i'm not that concerned about detected as spyware because we use to sign our code. but i get your point. i dont think you gonna sign your code with an expensive code signing certificate ;)

Keep in mind that your toolbar also can be looked at as spyware, because it's nothing else then a bho with a gui. so, if you ask me about my opinion i have to say: if you have no concerns about installing a toolbar to the browser you should be concerned about the BHO. just fill in all the necessary info in the AssemblyInfo.cs so people who are concerned about spyware & stuff at least know who wrote that stuff and what is it's intention.

BeatScholl at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 4
Hi Beat,
yes I guess the toolbar itself could be detected by an antispyware program... in fact it's not impossible we will buy a code signing certificate! I'm having a look at this, but it's somthing new to me... So far, found these links:
http://www.instantssl.com/code-signing/
http://www.verisign.com/products-services/security-services/code-signing/index.html
http://www.thawte.com/ssl-digital-certificates/code-signing/index.html

do you have any signing company/product to recommend to me?

Thanks!

Thibaud

Thibaud at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 5

Hi, beat,

please help a newbie here.

I tried the code you post here, I am having trouble to compile it?

using SHDocVw;

InternetExplorer explorer

It doesn't recognize the two item?

So how should I set up the project? C# --> windows --> , C#-->console, or control library?

Thanks

Codegirl at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 6
Hi CodeGirl,

you need to add a reference to the Microsoft Internet Controls which is a COM component installed on your machine. the IDE will create the Interop Assembly for you. don't forget to add the interop and your bho to the global assembly cache (gacutil.exe)

BeatScholl at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 7
Thibaud wrote:
Hi,

As I'm programming in C#, I'm using the DeskBand object http://www.codeproject.com/csharp/dotnetbandobjects.asp, rather than the MSDN .net kbbar sample which is written in managed C++.

I would like to make the toolbar visible after its installation. Do you know a way to do this?

I tried to use code like this:
Object oShow = true;
Object dummy = false;
Object vGuid = t.GUID.ToString("B"); // or this.GetType().GUID...
Explorer.ShowBrowserBar( ref vGuid, ref oShow, ref dummy);

but the problem is: where do I put this code?! If I put it in the SetSite method, it is called when the toolbar is displayed, so too late! Ideally, I would have liked to make it visible in the Register method, but unfortunately, the Explorer is not initalized at this step :-(
I found a C++ function that could be a way of doing it, but I don't manage to translate it into C#! This is this function:
STDAPI ShowToolbar(bool enable)
{
BOOL bShow = TRUE;
IWebBrowser2 *pIE;
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer, NULL,
CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2,(void**)&pIE);

VARIANT vtBandGUID;
VARIANT vtShow;
VARIANT vtNotUsed;
vtBandGUID.vt = VT_BSTR;
vtBandGUID.bstrVal = SysAllocString( L"{YOUR GUID HEAR}" );
vtShow.vt = VT_BOOL;
vtShow.boolVal = bShow;
vtNotUsed.vt = VT_INT;
vtNotUsed.intVal = 1;
hr = pIE->ShowBrowserBar(&vtBandGUID, &vtShow,
&vtNotUsed);
SysFreeString(vtBandGUID.bstrVal);
pIE->Release ();
return S_OK;
}

Do you have any idea?
Thanks a lot!

Thibaud


Hello friend,
I am a c# programmer.
i am also developing a toolbar using Band Objects.
i have also visited the link that you had.
Actually i want the steps need to be performed to display toolbar in IE.
I want exact sequece of function calls.

I am Highly thankful to you if you do the same.
If there will be the code than it will be the better than the best.

Thanks In Advance.

IrfanKothari at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 8

Hello,

What is your problem? the steps needed to make the toolbar appear are described in the link I mentionned, http://www.codeproject.com/csharp/dotnetbandobjects.asp. Don't forget to regasm the BandObjectLib and to gacutil all dlls of the project, which need to have a strong name.

Thibaud

Thibaud at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...
# 9

Hello Thibaud,

thanks for giving me reply.

but i have a problem regarding how to make communication beetween My toolbar and Internet Explorer.

what will be the steps.

i have tried for this.

but i have stuck in using setsite method.

where to put this method and after what method?

So,please give me the steps need to be done for making communication between Toolbar and IE.

thanks in Advance.

IrfanKothari at 2007-8-31 > top of Msdn Tech,Internet Explorer Development,Internet Explorer Extension Development...