So. I'm working on a BHO in IE and I want to add a browser action like this:
In internet explorer it would look something like
The only tutorials and docs I've found were on creating toolbar items. None mentioned this option. I know this is possible because crossrider let you do this exact thing. I just don't know how.
I can't find any documentation on how I would implement this in a BHO. Any pointers are very welcome.
I tagged this with C# as a C# solution would probably be simpler but a C++ solution, or any other solution that works is also very welcome.
EDIT: https://github.com/somanuell/SoBrowserAction
Here is a screen shot of my work in progress.
The things I did:
1. Escaping from the protected mode
The BHO Registration must update the
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Low Rights\ElevationPolicy
key. See Understanding and Working in Protected Mode Internet Explorer.I choose the process way because it's noted as "best practice" and is easier to debug, but the
RunDll32Policy
may do the trick, too.Locate the
rgs
file containing your BHO registry settings. It's the one containing the upadte to the Registry Key'Browser Helper Object'
. Add to that file the following:The GUID must be a new one, don't use mine, use a GUID Generator. The
3
value for policy ensures that the broker process will be launched as a medium integrity process. The%MODULEPATH%
macro is NOT a predefined one.Why use a macro? You may avoid that new code in your RGS file, provided that your MSI contains that update to the registry. As dealing with MSI may be painful, it's often easier to provide a "full self registering" package. But if you don't use a macro, you then can't allow the user to choose the installation directory. Using a macro permits to dynamically update the registry with the correct installation directory.
How to make the macro works? Locate the
DECLARE_REGISTRY_RESOURCEID
macro in the header of your BHO class and comment it out. Add the following function definition in that header:That code is borrowed from the ATL implementation for
DECLARE_REGISTRY_RESOURCEID
(in my case it's the one shipped with VS2010, check your version of ATL and update code if necessary). TheIDR_CSOBABHO
macro is the resource ID of theREGISTRY
resource adding the RGS in your RC file.The
sm_szModulePath
variable must contains the installation path of the broker process EXE. I choose to make it a public static member variable of my BHO class. One simple way to set it up is in theDllMain
function. Whenregsvr32
load your Dll,DllMain
is called, and the registry is updated with the good path.Many thanks to Mladen Janković.
How to lauch the Broker process?
One possible place is in the
SetSite
implementation. It will be lauched many times, but we will deal with that in the process itself. We will see later that the broker process may benefit from receiving as argument the HWND for the hosting IEFrame. This can be done with theIWebBrowser2::get_HWND
method. I suppose here that your already have anIWebBrowser2*
member.2. Injecting the IEFrame threads
It appears that this may be the most complex part, because there are many ways to do it, each one with pros and cons.
The broker process, the "injector", may be a short lived one, with one simple argument (a HWND or a TID), which will have to deal with a unique IEFrame, if not already processed by a previous instance.
Rather, the "injector" may be a long lived, eventually never ending, process which will have to continually watch the Desktop, processing new IEFrames as they appear. Unicity of the process may be guaranteed by a Named Mutex.
For the time being, I will try to go with a KISS principle (Keep It Simple, Stupid). That is: a short lived injector. I know for sure that this will lead to special handling, in the BHO, for the case of a Tab Drag And Drop'ed to the Desktop, but I will see that later.
Going that route involves a Dll injection that survives the end of the injector, but I will delegate this to the Dll itself.
Here is the code for the injector process. It installs a
WH_CALLWNDPROCRET
hook for the thread hosting the IEFrame, useSendMessage
(with a specific registered message) to immediatly trigger the Dll injection, and then removes the hook and terminates. The BHO Dll must export aCallWndRetProc
callback namedHookCallWndProcRet
. Error paths are omitted.3. Surviving Injection: "hook me harder"
The temporary loading of the Dll in the main IE process is sufficient to add a new button to the Toolbar. But being able to monitor the
WM_COMMAND
for that new button requires more: a permanently loaded Dll and a hook still in place despite the end of the hooking process. A simple solution is to hook the thread again, passing the Dll instance handle.As each tab opening will lead to a new BHO instantiation, thus a new injector process, the hook function must have a way to know if the current thread is already hooked (I don't want to just add a hook for each tab opening, that's not clean)
Thread Local Storage is the way to go:
DllMain
, forDLL_PROCESS_ATTACH
.HHOOK
as TLS data, and use that to know if the thread is already hookedDLL_THREAD_DETACH
DLL_PROCESS_DETACH
That leads to the following code:
We now have a nearly complete hook function:
The code for
AddBrowserActionForIE9
will be edited later.For IE9, getting the TB is pretty simple:
4. Processing the Tool Bar
That part may be largely improved:
See my other answer to the question, as I am currently unable to edit the answer with code format working.