简体   繁体   English

这个API究竟是什么以及它在做什么?

[英]What exactly is this API and what is it doing?

I know you are gonna hate me for that kind of question. 我知道你会因为那样的问题而讨厌我。 But could somebody tell me what the following code is doing? 但是有人可以告诉我以下代码在做什么吗?

I mean there are some libraries loaded, i get that. 我的意思是有一些库加载,我明白了。 plus there are some methods, still I don't get it. 加上有一些方法,但我还是得不到它。

Fe: 铁:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]

Here is the code: 这是代码:

private static class API 
    {

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr SetWindowsHookEx(
            int idHook,
            HookDel lpfn,
            IntPtr hMod,
            uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr CallNextHookEx(
            IntPtr hhk,
            int nCode,
            IntPtr wParam,
            IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern IntPtr GetModuleHandle(
            string lpModuleName);
    }

You do not have to explain it to me line for line. 你没有必要向我解释一行。 At least give me some reference where I can read it up, please. 请至少给我一些参考,我可以把它读出来。

Thx in advance! Thx提前!

This code is using P/Invoke to allow C# code to call several Win32 API functions related to Windows Hooks . 此代码使用P / Invoke允许C#代码调用与Windows Hook相关的多个Win32 API函数。

The posted code only defines the methods; 发布的代码仅定义方法; it doesn't call them, so it doesn't do anything by itself. 它不会调用它们,因此它本身不会做任何事情。 It just allows you to use the methods from other parts of your code. 它只允许您使用代码其他部分的方法。

Here's an older MSDN article explaining P/Invoke and what's going on. 这是一篇较旧的MSDN文章,解释P / Invoke以及发生了什么。 Hopefully this helps you. 希望这会对你有所帮助。

What the code is doing is allowing your managed C# code to call unmanaged Win32 API functions. 代码正在做的是允许您的托管C#代码调用非托管的Win32 API函数。

Here's also a tutorial on MSDN that walks you through the P/Invoke process of creating code like your question has. 这里还有一个关于MSDN的教程 ,它将引导您完成创建代码的P / Invoke过程,就像您的问题一样。

DllImport is used to call unmanaged code/API in .Net/Managed code. DllImport用于在.Net /托管代码中调用非托管代码/ API。 All the code you've posted is trying to work with the window object of Win32 API. 您发布的所有代码都在尝试使用Win32 API的window对象。

References: 参考文献:

DLLImport 的DllImport

Win32 API Win32 API

Win32 API to .Net API map Win32 API到.Net API映射

Take a look at this . 看看这个 Your program somewhere is installing a hook into windows hook chain to monitor some events. 你的程序在某个地方安装一个挂钩进入windows钩链来监视一些事件。

The dllimport attribute itself lets the program to invoke win32 api functions like the previous answer mentions. dllimport属性本身允许程序调用win32 api函数,就像之前的答案提到的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM