在 .NET 程序中使用 Win32 函数并不如 C++ 中方便。因为 C# 中不能引入 C++ 中常用的头文件,于是各种方法签名、结构体定义等等都需要各种寻找。然而 PInvoke.net 帮助我们解决了这个问题。本文推荐一款 Visual Studio 插件来帮助我们更快速地插入 Win32 函数签名。
PInvoke.net
PInvoke.net 的官方网站是 https://www.pinvoke.net/,如果你只是希望临时找一找 P/Invoke 函数调用的方法签名,那么直接去网站就能搜索。不过,如果你期望写代码时能够随时方便地插入,那么安装插件还是非常方便的。
前往 Visual Studio Marketplace 即可下载安装 PInvoke.net Visual Studio Extension 扩展。不过,更推荐直接在 Visual Studio 的“工具->扩展和更新”里面在线下载安装插件:
下载完关闭所有的 Visual Studio 后,会弹出扩展安装界面,继续安装即可。
使用 PInvoke.net 扩展
在安装了 PInvoke.net 插件后,可以在顶部菜单栏中寻找到 PInvoke.net 菜单项,里面可以插入 PInvoke 的函数调用签名:
现在,我们搜索 MoveWindow
函数:
随后点击 Insert 便在代码中得到了一份 MoveWindow 的 P/Invoke 函数签名。
/// <summary>
/// The MoveWindow function changes the position and dimensions of the specified window. For a top-level window, the
/// position and dimensions are relative to the