开发者

What exactly is this API and what is it doing?

开发者 https://www.devze.com 2023-03-22 16:24 出处:网络
I know you are gonna hate me for that kind of question. But could somebody tell me what the following code is 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 开发者_JAVA百科get it.

F.e.:

[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!


This code is using P/Invoke to allow C# code to call several Win32 API functions related to Windows Hooks.

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. Hopefully this helps you.

What the code is doing is allowing your managed C# code to call unmanaged Win32 API functions.

Here's also a tutorial on MSDN that walks you through the P/Invoke process of creating code like your question has.


DllImport is used to call unmanaged code/API in .Net/Managed code. All the code you've posted is trying to work with the window object of Win32 API.

References:

DLLImport

Win32 API

Win32 API to .Net API map


Take a look at this. Your program somewhere is installing a hook into windows hook chain to monitor some events.

The dllimport attribute itself lets the program to invoke win32 api functions like the previous answer mentions.

0

精彩评论

暂无评论...
验证码 换一张
取 消