System.Runtime.InteropServices提供了相应的类或者方法来支持托管/非托管模块间的互相调用。
System.Runtime.InteropServices中几个比较重要的类:
DllImportAttribute : 该类提供对非托管动态链接库进行引用的方法,并告诉我们的编译器该程序的静态入口点是非托管的动态连接库,它的静态属性提供了对非托管动态链接库进行调用所必需的信息,作为最基本的要求,该类应该定义提供调用的非托管动态链接库的名称。成员详细信息
StructLayoutAttribute: 该类使得用户可以控制类或结构的数据字段的物理布局。

System.Runtime.InteropServices浅见[StructLayout(LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)]
System.Runtime.InteropServices浅见
public class MySystemTime 

MarshalAsAttribute : 指示如何在托管代码和非托管代码之间封送数据。下面是MSDN给出的示例代码:

System.Runtime.InteropServices浅见[C#] 
System.Runtime.InteropServices浅见
//Applied to a parameter.
System.Runtime.InteropServices浅见
  public void M1 ([MarshalAs(UnmanagedType.LPWStr)]String msg);
System.Runtime.InteropServices浅见
//Applied to a field within a class.

一个将三个类综合运用的实例:调用kernel32.dll中的非托管方法"GetSystemTime"将系统时间返回给定制的类MySystemTime并执行输出.

System.Runtime.InteropServices浅见using System;
System.Runtime.InteropServices浅见
using System.Collections.Generic;
System.Runtime.InteropServices浅见
using System.Text;
System.Runtime.InteropServices浅见
using System.Runtime.InteropServices;
System.Runtime.InteropServices浅见
System.Runtime.InteropServices浅见
namespace DllImportTest


 

相关文章:

  • 2021-06-21
  • 2021-11-29
  • 2022-12-23
  • 2021-12-26
  • 2021-06-23
  • 2022-01-09
  • 2022-12-23
猜你喜欢
  • 2022-01-03
  • 2021-12-30
  • 2021-05-16
  • 2022-01-23
  • 2021-05-09
  • 2022-12-23
  • 2021-10-07
相关资源
相似解决方案