当前位置: 首页 > news >正文

使用 PInvoke.net Visual Studio Extension 辅助编写 Win32 函数签名

在 .NET 程序中使用 Win32 函数并不如 C++ 中方便。因为 C# 中不能引入 C++ 中常用的头文件,于是各种方法签名、结构体定义等等都需要各种寻找。然而 PInvoke.net 帮助我们解决了这个问题。本文推荐一款 Visual Studio 插件来帮助我们更快速地插入 Win32 函数签名。


本文内容

    • PInvoke.net
    • 使用 PInvoke.net 扩展

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 upper-left corner of the screen. For a child window, they are relative
///     to the upper-left corner of the parent window's client area.
///     <para>
///         Go to https://msdn.microsoft.com/en-us/library/windows/desktop/ms633534%28v=vs.85%29.aspx for more
///         information
///     </para>
/// </summary>
/// <param name="hWnd">C++ ( hWnd [in]. Type: HWND )<br /> Handle to the window.</param>
/// <param name="X">C++ ( X [in]. Type: int )<br />Specifies the new position of the left side of the window.</param>
/// <param name="Y">C++ ( Y [in]. Type: int )<br /> Specifies the new position of the top of the window.</param>
/// <param name="nWidth">C++ ( nWidth [in]. Type: int )<br />Specifies the new width of the window.</param>
/// <param name="nHeight">C++ ( nHeight [in]. Type: int )<br />Specifies the new height of the window.</param>
/// <param name="bRepaint">
///     C++ ( bRepaint [in]. Type: bool )<br />Specifies whether the window is to be repainted. If this
///     parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This
///     applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the
///     parent window uncovered as a result of moving a child window.
/// </param>
/// <returns>
///     If the function succeeds, the return value is nonzero.<br /> If the function fails, the return value is zero.
///     <br />To get extended error information, call GetLastError.
/// </returns>
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

不过,插件内所带的 P/Invoke 函数似乎并不够多,因为对于 DwmSetWindowAttribute 这样的函数并没有在插件中出现。不过 https://www.pinvoke.net/ 中是包含的。

在这里插入图片描述

除了包含 C# 调用所需的函数签名之外,还包含函数签名中所用的结构体或枚举类型定义。

[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWINDOWATTRIBUTE attr, ref int attrValue, int attrSize);

enum DWMWINDOWATTRIBUTE : uint
{ 
    NCRenderingEnabled = 1,
    NCRenderingPolicy,
    TransitionsForceDisabled,
    AllowNCPaint,
    CaptionButtonBounds,
    NonClientRtlLayout,
    ForceIconicRepresentation,
    Flip3DPolicy,
    ExtendedFrameBounds,
    HasIconicBitmap,
    DisallowPeek,
    ExcludedFromPeek,
    Cloak,
    Cloaked,
    FreezeRepresentation
}

感谢广大 .NET 的社区开发者帮助收集各种 PInvoke 函数签名;如果你发现了一些没有收录的,也欢迎加入。


我的博客会首发于 https://blog.walterlv.com/,而 CSDN 会从其中精选发布,但是一旦发布了就很少更新。

如果在博客看到有任何不懂的内容,欢迎交流。我搭建了 dotnet 职业技术学院 欢迎大家加入。

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名吕毅(包含链接:https://walterlv.blog.csdn.net/),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。

相关文章:

  • 程序员与英语:即时聊天中的英语缩写 lol / lmao / idk
  • 使用 IFTTT 做 RSS 的邮件订阅服务
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • 语法高亮不够漂亮?这里有你想要的 Rouge 主题
  • 理解 UWP 视图的概念,让 UWP 应用显示多个窗口(多视图)
  • UWP 扩展/自定义标题栏的方法,一些概念和一些注意事项
  • 图片点击放大,你的网页也能做到!
  • UWP 应用中 CoreApplication / Application, CoreWindow / Window 之间的区别
  • 使用 C# 代码创建快捷方式文件
  • 发布了一款库(或工具包),如何持续地编写更新日志(ChangeLog)?
  • Windows 无法删除文件夹 —— 访问被拒绝 / 因为目录不是空的
  • 如何精准地用打印机在贺卡或邀请函上打字
  • .NET/C# 推荐一个我设计的缓存类型(适合缓存反射等耗性能的操作,附用法)
  • 使用 Postman 调试 ASP.NET Core 开发的 API
  • 只有你能 new 出来!.NET 隐藏构造函数的 n 种方法(Builder Pattern / 构造器模式)
  • 10个确保微服务与容器安全的最佳实践
  • ECS应用管理最佳实践
  • python学习笔记 - ThreadLocal
  • ReactNative开发常用的三方模块
  • Service Worker
  • 成为一名优秀的Developer的书单
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 工程优化暨babel升级小记
  • 基于组件的设计工作流与界面抽象
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 我建了一个叫Hello World的项目
  • 学习HTTP相关知识笔记
  • 优化 Vue 项目编译文件大小
  • 原生js练习题---第五课
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​卜东波研究员:高观点下的少儿计算思维
  • ​低代码平台的核心价值与优势
  • $.ajax中的eval及dataType
  • $con= MySQL有关填空题_2015年计算机二级考试《MySQL》提高练习题(10)
  • %check_box% in rails :coditions={:has_many , :through}
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (Redis使用系列) SpirngBoot中关于Redis的值的各种方式的存储与取出 三
  • (SpringBoot)第二章:Spring创建和使用
  • (二)WCF的Binding模型
  • (附源码)springboot高校宿舍交电费系统 毕业设计031552
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (七)Java对象在Hibernate持久化层的状态
  • (十二)springboot实战——SSE服务推送事件案例实现
  • * 论文笔记 【Wide Deep Learning for Recommender Systems】
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET 简介:跨平台、开源、高性能的开发平台
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • /etc/sudoer文件配置简析
  • ?php echo $logosrc[0];?,如何在一行中显示logo和标题?
  • @converter 只能用mysql吗_python-MySQLConverter对象没有mysql-connector属性’...