本文介绍了在C#中使用钩子-请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在阅读并尝试了很多在这里和其他网站上找到的示例之后,我仍然没有真正了解Windows挂钩的工作原理..我想创建两个单独的控制台应用程序.一个用于记录Windows操作(并将其保存在文件中),另一个用于重播它们..

因此,我当然开始阅读MSDN库,以了解"JournalRecordProc"和"JournalPlaybackProc"功能的工作原理.

在msdn页面上的使用挂钩":
http://msdn.microsoft.com/en-us/library/ms644960%28v = VS.85%29.aspx [ ^ ]

它说我需要将钩子放置在单独的DLL文件中.这是我的问题真正开始的地方.如何将DLL文件添加到控制台应用程序?我试图创建一个新的"c#类库",只是将msdn页中的代码(以HOOKRPOC开头)粘贴到了项目中,但无法对其进行编译.
我知道作为一个新手,我有很多基本问题,但是如果您甚至不知道如何开始(设置它),那么它真的很令人沮丧.有人可以帮助我吗?

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?

推荐答案

<br />
StructLayout

属性,IntPtr以及毫无疑问的互操作所需的其他奥术细节.

若要了解页面为何说要从控制台应用程序使用单独的DLL,您需要了解本机DLL的性质(与编译为DLL的.Net程序集完全不同的生物),以及DLL和EXE的不同之处.对于.Net程序员来说,这有点底层,但是值得理解.我还没有安装Windows钩子,所以我只是在猜测,但是EXE和DLL看起来"与Windows不同(不是猜测),因此SetWindowsHookEx方法可能无法正常工作如果它是从EXE调用的(这是猜测).

当您安装Windows钩子时,您是在告诉Windows这里有一些有效的代码(DLL),这是钩子被触发时跳转到的位置"-从这个角度讲,Windows仅能理解本机的东西(并且不知道所有的东西.净).当Windows要调用您的钩子时,需要确保DLL已正确初始化(在安全地在DLL中运行方法之前,您可能具有全局(静态)变量和其他需要初始化的东西-并初始化a). DLL与初始化EXE完全不同.

希望所有这些都有意义.
克里斯

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


这篇关于在C#中使用钩子-请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 16:21