1

I'm trying to understand event hooks in C++. I know what an event is, I've used them a lot in Java, C# and Javascript.

What I'm having trouble with is finding the documentation, and tutorials on stuff like global hooks, dll injection, global hooks without a DLL.

Lets say that I wanted to iterate through the browser tabis in FireFox .. would I need to hope that FireFox has an API for C++? Or lets say I wanted to do something when a user opens a new tab would I need to use a hook that FireFox would provide in their API?

The above is just an example so people know what I'm trying to learn/understand. Am I thinking on the right ines?

I seen a post on a forum and for the past 2 hours I've took an interest. I always say that a tricky challange, or a new challange, makes a stronger programmer.

Any resources, or any help, would be very much appreciated.

atzz
  • 17,507
  • 3
  • 35
  • 35
Jack Harvin
  • 6,605
  • 7
  • 23
  • 21
  • 2
    There is no such thing as "events" or "event hooks" in C++ itself. It might be a part of some framework, though. "DLL injection" hints that you are perhaps referring to some WinAPI feature. – Vlad Nov 27 '10 at 13:57
  • Whether Firefox (or any other application) provides API in some particular language, is solely determined by their dev team. They may choose to provide API in any language (or several languages, or not provide at all). – Vlad Nov 27 '10 at 14:00
  • The 2nd paragraph makes me think you are talking about Windows. Please clarify your question if it is so. Windows has a hooks mechanizm, and even something that can be described as "global hook w/o DLL". However, it has nothing to do with C++ (it's a system API, language independent). – atzz Nov 27 '10 at 14:02
  • @Vlad the classic example is a keystroke logger that uses a DLL and hooks provide by WinAPI. All the tutorials I have seen fail to provide information on other WinAPI hooks and fail to explain DLL enjection. – Jack Harvin Nov 27 '10 at 14:02
  • @atzz so C++ has no events or hooks but Windows and other applications (if an api is available) do? And yes I am talking about Windows. Sorry I didn't mention but it's only came to light when you and Vlad mentioned it. I'm slowly covering more ground on the subject. – Jack Harvin Nov 27 '10 at 14:03
  • @Jack: C++ is not WinAPI-specific. WinAPI hooks and DLL injection are Windows-only features, so the cross-platform languages like C++ don't define them. From the other side, WinAPI provides C and C++ headers, so the WinAPI features may be consumed by C/C++ programs (which are targeting Windows as execution environment). – Vlad Nov 27 '10 at 14:07

3 Answers3

3

C++ itself does not have events or hooks, but a lot of C++ libraries and frameworks implement them. For an example of generic events library, see Boost.Signals.

Some of the implementations allow their events to be seen by other applications, but the API is application-specific (e.g. for Firefox, see XPCOM).


Windows has a mechanizm of hooks that allows to monitor various events in its windowing system. However, it is an OS feature, not related to C++. As it's a system mechanizm, all Windows applications are affected even if they don't do anything for it. The documentation for Windows hooks can be found here. Also, since you mentioned "global hooks without a DLL", see SetWinEventHook, which is a higher-level API than Windows hooks linked above and can be used with hook functions both implemented in DLLs or EXEs.
atzz
  • 17,507
  • 3
  • 35
  • 35
3

Look up MSDN for SetWindowsHookEx. It should be your entrance in Windows hooks. If you ar etargetting a parituclar window for mthe system then a less intrusive option is SetWindowLongPtr. For the first API you are going to need some Dll injection - which gets automatically for you by the system. Just follow these steps:

Create a Dll that exports a HOOKPROC function (actual type dependent upon the hook tpe - read in the docs)

Load that Dll in your application and retrieve a pointer to the HOOKPROC function. LoadLibrary / GetProcAddress APIs may be used for this.

From your application, make a call to SetWindowsHookEx feeding in the appropriate parameters - this will inject the dll in the target process. So, the dll is now loaded into both your app's process and in the target process. So you will need a mechanism to IPC between the two processes probably. Lots of ways here - sockets, pipes, shared segment in DLL, filesystem, windows messages, COM servers + events, etc etc.

The former API, while less powerful, does not require DLL injection.

Choose wisely & good luck!

kellogs
  • 2,837
  • 3
  • 38
  • 51
0

I dont think firefox would be having a C++ aPI to find the open tabs.... If you want to find out open tabs or whenever a new tab is open , you can basically hook the firefox window and get all events happening on that window to your hook procedure.

If you open spy++ in VC++ and track firefox window , you can see a new MozillaContentWindowClass gets created every time whenever a new tab is opened. So you can basically iterate through window handles and get information about open tabs.

You can use SetWindowLongPtr to set the subclass procedure for that window.

anand
  • 11,071
  • 28
  • 101
  • 159