No problem with being a newbie - we all started somewhere (and, mostly, we didn't start with anything near as challenging as what you're attempting!)
The first thing to notice is that the sample code in the MSDN page is C/C++, not C#. That's a big difference, and the sample code will never compile in a C# class library. So, you'll either have to create a DLL using C/C++, or translate the C++ code into the equivalent C# code.
To go with the former of these two approaches, you'll need to create an
unmanaged (C++) DLL into which you paste the sample code from the MSDN page. Hopefully the sample is complete and correct, and will produce visible results that you can learn from. To call into such a DLL from a C# console app, you'll need to use P/Invoke to reference the entry point(s) into the DLL that you'll be using from C#.
To jump in the deep end and create a .Net / C# DLL from the sample, you'll also use P/Invoke, but this time referencing
SetWindowsHookEx
and any other Windows methods you'll be calling. You'll need to understand the
<br />
StructLayout
attribute,
IntPtr
and doubtlessly other arcane details that are required for interop.
To understand why the page says to use a separate DLL from your console app, you need to learn about the nature of a native DLL (which is a very different creature from a .Net assembly compiled into a DLL) and how DLLs and EXEs differ. To a .Net programmer this is somewhat low-level stuff, but worth understanding. I've not installed windows hooks myself, so here I'm only guessing, but EXEs and DLLs 'look' different to Windows (not a guess), so the
SetWindowsHookEx
method probably just doesn't work if it's called from an EXE (this is the guess).
When you install a windows hook, you're telling Windows "here's some valid code (the DLL), and here's where to jump to when the hook is fired" - and from this perspective Windows only understands native stuff (and is unaware of all things .Net). When Windows wants to invoke your hook it needs to be sure that the DLL is correctly initialized (you may have global (static) variables and other things that need to be initialized before it is safe to run a method in the DLL - and initializing a DLL is very different from initializing an EXE.
Hopefully, some of all that made sense.
Chris