Csharp/C#教程:在哪里放DllImport?分享


在哪里放DllImport?

static class Class { public static void methodRequiringStuffFromKernel32() { // code here... } } 

我在哪里放[DllImport("Kernel32.dll")]

你把它放在你从Kernel32.dll导入的方法上。

例如,

 static class Class { [DllImport("Kernel32.dll")] static extern Boolean Beep(UInt32 frequency, UInt32 duration); public static void methodRequiringStuffFromKernel32() { // code here... Beep(...); } } 

来自@ dtb :请注意,该类应命名为NativeMethodsSafeNativeMethodsUnsafeNativeMethods 。 有关更多详细信息,请参阅命名非托管代码方法约定 。

CA1060:将P / Invokes移动到NativeMethods类 :

这是DllImport一个例子:

 using System; using System.Runtime.InteropServices; class MsgBoxTest { [DllImport("user32.dll")] static extern int MessageBox (IntPtr hWnd, string text, string caption, int type); public static void Main() { MessageBox (IntPtr.Zero, "Please do not press this again.", "Attention", 0); } } 

我建议你学习平台调用教程 。

 static class Class { [DllImport("kerynel32.dll")] public static extern void methodRequiringStuffFromKernel32(); } 

它继续在P /调用外部方法的方法本身。 确保添加对System.Runtime.InteropServices的引用

上述就是C#学习教程:在哪里放DllImport?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/998769.html

(0)
上一篇 2021年12月27日
下一篇 2021年12月27日

精彩推荐