在C#中使用钩子-请帮助 [英] Using Hooks in C# - please help

查看:123
本文介绍了在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?

推荐答案

成为新手没问题-我们都从某个地方开始了(而且,大多数情况下,我们并没有像您尝试的那样具有挑战性!)

首先要注意的是,MSDN页面中的示例代码是C/C ++,而不是C#.那是一个很大的区别,并且示例代码永远不会在C#类库中编译.因此,您将不得不使用C/C ++创建DLL,或将C ++代码转换为等效的C#代码.

要使用这两种方法中的前一种,您将需要创建一个 unmanaged (C ++)DLL,将MSDN页面中的示例代码粘贴到其中.希望该示例是完整且正确的,并会产生可见的结果,您可以从中学习.要从C#控制台应用程序调用此类DLL,您将需要使用P/Invoke引用要从C#使用的DLL的入口点.

为了深入研究并从示例中创建.Net/C#DLL,您还将使用P/Invoke,但这一次引用SetWindowsHookEx和您将要调用的任何其他Windows方法.您将需要了解
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

属性,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#中使用钩子-请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆