让拖管代码对象和非托管对象协同工做的过程称为互用性(Interoperability),一般简称为 Interop。
P/Invoke在托管代码与非托管代码交互式时产生一个事务(Transition),这一般发生在使用平台调用服务(Platfrom Invocation Services)即P/Invoke。容许托管代码调用平台(Platfrom)相关的非托管代码(c++、VB、Delphi....)
Com Interop 一种服务,它使 .NET Framework 对象可以与 COM 对象通讯。
如调用系统的 API 或与 COM 对象打交道,经过 System.Runtime.InteropServices 命名空间
虽然使用 Interop 很是方便,但据估计每次调用事务都要执行 10 到 40 条指令,算起来开销也很多,因此咱们要尽可能少调用事务
若是非用不可,建议本着一次调用执行多个动做,而不是屡次调用每次只执行少许动做的原则c++
The frequency of the sound, in hertz. This parameter must be in the range 37 through 32,767 (0x25 through 0x7FFF).c#
If the function succeeds, the return value is nonzero.windows
If the function fails, the return value is zero. To get extended error information, call api
RequirementsDLL Kernel32.dll网络
[DllImportAttribute("kernel32.dll")] public static extern bool Beep([In]uint dwFreq, [In] uint dwDuration);
CLR会找到kernel32.dll使用loadlibrary函数加载起来,而后经过GetProcAddress函数查找到Beep入口点地址,而后就能够经过入口点地址调用Beep函数。在调用以前CLR会作一些状态切换,须要进行参数转换.NET的32int 转换为 c++的32整型(在这里因为.NET的int 和c++的32整形是同样的因此不用转换能够直接传递)。可是也有复杂的状况,在字符串的状况下CRl须要把字符串内容备份拷贝,转换编码以\0结尾的字符串内存传递给c++。app
函数入口点 函数
缺省为托管函数的名字 工具
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace demo1 9 { 10 11 12 [StructLayoutAttribute(LayoutKind.Sequential)] 13 public struct HWND__ 14 { 15 16 /// int 17 public int unused; 18 } 19 20 public partial class NativeMethods 21 { 22 23 /// Return Type: int 24 ///hWnd: HWND->HWND__* 25 ///lpText: LPCSTR->CHAR* 26 ///lpCaption: LPCSTR->CHAR* 27 ///uType: UINT->unsigned int 28 [DllImportAttribute("user32.dll", EntryPoint = "MessageBoxA")] 29 public static extern int MessageBoxA([InAttribute()] System.IntPtr hWnd, [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string lpText, [InAttribute()] [MarshalAsAttribute(UnmanagedType.LPStr)] string lpCaption, uint uType); 30 31 } 32 class Program 33 { 34 /// <summary> 35 /// 用于生成简单的声音 36 /// </summary> 37 /// <param name="dwFreq"> Long,声音频率(从37Hz到32767Hz)。在windows95中忽略</param> 38 /// <param name="dwDuration"> Long,声音的持续时间,以毫秒为单位。如为-1,表示一直播放声音,直到再次调用该函数为止。在windows95中会被忽略</param> 39 /// <returns>Long,TRUE(非零)表示成功,不然返回零。会设置GetLastError</returns> 40 [DllImport("kernel32.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode, EntryPoint = "Beep", ExactSpelling = true, SetLastError = true)] 41 public static extern bool Beep([In]int dwFreq, [In] int dwDuration); 42 43 [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "MessageBox")] 44 public static extern bool MsgBox(IntPtr hwnd,string text, string caption ,int type); 45 46 47 static void Main(string[] args) 48 { 49 NativeMethods.MessageBoxA(IntPtr.Zero, "123", "321", 0); 50 // Marshal.GetLastWin32Error(); 51 MsgBox(IntPtr.Zero, "123", "321",2); 52 53 int[] ss = new int[7] { 262, 294, 330, 349, 392, 440, 494 }; 54 55 for (int i = 1; i < 8; i++) 56 { 57 Beep(ss[i-1] / 2, 500); 58 Console.WriteLine("低" + i); 59 Beep(0, 1000); 60 61 62 } 63 for (int i = 1; i < 8; i++) 64 { 65 Beep(ss[i - 1] , 500); 66 Console.WriteLine("中" + i); 67 Beep(0, 1000); 68 } 69 for (int i = 1; i < 8; i++) 70 { 71 Beep(ss[i - 1] * 2, 500); 72 Console.WriteLine("高" + i); 73 Beep(0, 1000); 74 } 75 76 77 //C#自带的应该也是经过上面的手段调用winapi 78 int x = 9; 79 // 80 if ( 81 ((x >= 1) && (x <= 9))) 82 { 83 for (int i = 1; i <= x; i++) 84 { 85 Console.WriteLine("Beep number {0}.", i); 86 Console.Beep(1111, 1111); 87 } 88 } 89 else 90 Console.WriteLine("Usage: Enter the number of times (between 1 and 9) to beep."); 91 92 } 93 94 } 95 }
本文为学习笔记 若有侵犯请联系我 ,学习课程名 由张羿主讲的《公共语言运行库(CLR)开发系列课程(1):Pinvoke简介》 百度能够搜索到相关学习资料 我收集资料并不全官方不提供下载了 如今只有视频提供下载 第三方地址 学习