Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

after reading and trying a lot of the examples i found here and on other sites, i still dont really get how Windows hooks work.. I want to create two seperate console applications. One to record windows actions (and save it in a file) and another one to replay them..

So i started of course reading the MSDN Libraries to find out how the fuctions "JournalRecordProc" and "JournalPlaybackProc" work.

On the msdn page "Using hooks":
http://msdn.microsoft.com/en-us/library/ms644960%28v=VS.85%29.aspx[^]

it says that i need to place the hook in a seperate DLL file. This is where my problem actually begins. How do i add a DLL file to my console application? i tried to create a new "c# class library" and simply pasted the code from the msdn page (beginning with HOOKRPOC) in the project but wasnt able to compile it..

I know as a newbie i have a lot of basic questions, but its really frustrating if you dont even know how to get started (set it up).. Can somebody help me?
Posted
Updated 27-Aug-10 7:13am
v2
Comments
[no name] 27-Aug-10 15:56pm    
And what were the compile errors?

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900