C#有没有方法可以直接都用已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法),而不需要重新编写代码?

答案是肯定,就是通过接下来要说的 DllImport 。

DllImport的namespace: using System.Runtime.InteropServices;

MSDN中对DllImportAttribute的解释:可将该属性应用于方法。

DllImportAttribute 属性提供对从非托管 DLL 导出的函数进行调用所必需的信息。必须提供包含入口点的 DLL 的名称。

DllImport 属性定义如下:

    

namespace System.Runtime.InteropServices
{
  [AttributeUsage(AttributeTargets.Method)]
  public class DllImportAttribute: System.Attribute
  {
   public DllImportAttribute(string dllName) {...}
   public CallingConvention CallingConvention;
   public CharSet CharSet;
   public string EntryPoint;
   public bool ExactSpelling;
   public bool PreserveSig;
   public bool SetLastError;
   public string Value { get {...} }
  }
}

    说明:    
    1、DllImport只能放置在方法声明上。   
    2、DllImport具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。   
    3、DllImport具有五个命名参数:    
     a、CallingConvention 参数:指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。    
     b、CharSet 参数: 指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。   
     c、EntryPoint 参数: 给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。    
     d、ExactSpelling 参数: 指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。    
     e、PreserveSig 参数: 指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。    
     f、SetLastError 参数: 指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。    
    4、它是一次性属性类。    
    5、用 DllImport 属性修饰的方法必须具有 extern 修饰符。

   
DllImport的用法:

  DllImport("MyDllImport.dll")]
private static extern int mySum(int a,int b);


一 在C#程序设计中使用Win32类库
 常用对应类型:
1、DWORD 是 4 字节的整数,因此我们可以使用 int 或 uint 作为 C# 对应类型。
2、bool 类型与 BOOL 对应。

示例一:调用 Beep() API 来发出声音
    Beep() 是在 kernel32.lib 中定义的,在MSDN 中的定义,Beep具有以下原型:

BOOL Beep(DWORD dwFreq, // 声音频率
DWORD dwDuration // 声音持续时间);

用 C# 编写以下原型:

[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);


示例二:枚举类型和常量
  
  MessageBeep() 是在 user32.lib 中定义的,在MSDN 中的定义,MessageBeep具有以下原型:

 BOOL MessageBeep(UINT uType // 声音类型
);

用C#编写一下原型:

public enum BeepType
{
  SimpleBeep = -,
  IconAsterisk = 0x00000040,
  IconExclamation = 0x00000030,
  IconHand = 0x00000010,
  IconQuestion = 0x00000020,
  Ok = 0x00000000,
}

uType 参数实际上接受一组预先定义的常量,对于 uType 参数,使用 enum 类型是合乎情理的。

[DllImport("user32.dll")]
public static extern bool MessageBeep(BeepType beepType);


示例三:处理结构
    如果需要确定笔记本的电池状况。Win32 为此提供了电源管理函数,搜索 MSDN 可以找到GetSystemPowerStatus() 函数。

  BOOL GetSystemPowerStatus(
  LPSYSTEM_POWER_STATUS lpSystemPowerStatus
);

此函数包含指向某个结构的指针,我们尚未对此进行过处理。要处理结构,我们需要用 C# 定义结构。我们从非托管的定义开始:

typedef struct _SYSTEM_POWER_STATUS {
BYTE  ACLineStatus;
BYTE  BatteryFlag;
BYTE  BatteryLifePercent;
BYTE  Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime;
} SYSTEM_POWER_STATUS, *LPSYSTEM_POWER_STATUS;

   然后,通过用 C# 类型代替 C 类型来得到 C# 版本。

struct SystemPowerStatus
{
  byte ACLineStatus;
  byte batteryFlag;
  byte batteryLifePercent;
  byte reserved1;
  int batteryLifeTime;
  int batteryFullLifeTime;
}

这样,就可以方便地编写出 C# 原型:

[DllImport("kernel32.dll")]
public static extern bool GetSystemPowerStatus( ref SystemPowerStatus systemPowerStatus);

   在此原型中,我们用 “ref” 指明将传递结构指针而不是结构值。这是处理通过指针传递的结构的一般方法。 
   此函数运行良好,但是最好将 ACLineStatus 和 batteryFlag 字段定义为 enum:

    enum ACLineStatus: byte
   {
    Offline = ,
    Online = ,
    Unknown = ,
   }
   enum BatteryFlag: byte
   {
    High = ,
    Low = ,
    Critical = ,
    Charging = ,
    NoSystemBattery = ,
    Unknown = ,
   }

请注意,由于结构的字段是一些字节,因此我们使用 byte 作为该 enum 的基本类型

示例四:处理字符串

二 C# 中调用C++代码
    int 类型

[DllImport(“MyDLL.dll")]
//返回个int 类型
public static extern int mySum (int a1,int b1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(int a2,int b2)
{
//a2 b2不能改变a1 b1
//a2=..
//b2=...
return a+b;
}
//参数传递int 类型
public static extern int mySum (ref int a1,ref int b1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(int *a2,int *b2)
{
//可以改变 a1, b1
*a2=...
*b2=...
return a+b;
}

DLL 需传入char *类型

[DllImport(“MyDLL.dll")]
//传入值
public static extern int mySum (string astr1,string bstr1);
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(char * astr2,char * bstr2)
{
//改变astr2 bstr 2 ,astr1 bstr1不会被改变
return a+b;
}

DLL 需传出char *类型

[DllImport(“MyDLL.dll")]
// 传出值
public static extern int mySum (StringBuilder abuf, StringBuilder bbuf );
//DLL中申明
extern “C” __declspec(dllexport) int WINAPI mySum(char * astr,char * bstr)
{
//传出char * 改变astr bstr -->abuf, bbuf可以被改变
return a+b;
}

DLL 回调函数

BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam) 
using System;
using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam); //定义委托函数类型
public class EnumReportApp
{
[DllImport("user32")]
public static extern int EnumWindows(CallBack x, int y);
public static void Main()
{
CallBack myCallBack = new CallBack(EnumReportApp.Report);
EnumWindows(myCallBack, );
}
public static bool Report(int hwnd, int lParam)
{
Console.Write("Window handle is ");
Console.WriteLine(hwnd); return true;
}
}

DLL  传递结构

BOOL PtInRect(const RECT *lprc, POINT pt); 
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Point {
public int x;
public int y;
}
[StructLayout(LayoutKind.Explicit)]
public struct Rect
{
[FieldOffset()] public int left;
[FieldOffset()] public int top;
[FieldOffset()] public int right;
[FieldOffset()] public int bottom;
}
Class XXXX {
[DllImport("User32.dll")]
public static extern bool PtInRect(ref Rect r, Point p);
}

DllImport attribute的总结的更多相关文章

  1. 有关C#标签Attribute的熟悉

    Attribute 简单用法: 最近用到了,所以静下心来找些资料看了一下,终于把这东西搞清楚了. 一.什么是Attribute 先看下面的三段代码: 1.自定义Attribute类:VersionAt ...

  2. C# 特性(Attribute)详细介绍

    1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注 ...

  3. (转)Attribute在.net编程中的应用

    Attribute在.net编程中的应用(一)Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一 ...

  4. Attribute在.net编程中的应用(一)

    Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是一个非常重要的组成部分,为了帮助大家理解和掌握A ...

  5. [转]Attribute在.net编程中的应用

    Attribute在.net编程中的应用(一) Attribute的基本概念 经常有朋友问,Attribute是什么?它有什么用?好像没有这个东东程序也能运行.实际上在.Net中,Attribute是 ...

  6. 【C#特性】 Attribute 特性

    msdn:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/creating-c ...

  7. 5.24 Declaring Attributes of Functions【转】

    转自:https://gcc.gnu.org/onlinedocs/gcc-4.0.0/gcc/Function-Attributes.html 5.24 Declaring Attributes o ...

  8. Platform Invoke

    PInvoke 允许managed code 来调用在DLL中实施的unmanged function. Platform invoke relies on metadata to locate ex ...

  9. C# 笔试题,看你会几道题

    1.       Which interface you need to implement to support for each? IEnumerator IEnumerable ICompare ...

随机推荐

  1. manifest中读取<meta-data>

    meta-data在清单文件中主要有以下用法: <application>       <meta-data android:value="a1" android ...

  2. es6 ajax

    简单的co用例: var co=require("co") class view{ constructor(){ co(function*(){ var p1=this.ajax1 ...

  3. 【爬虫】BeautifulSoup之爬取百度贴吧的帖子

    在网上看到爬百度贴吧的例子,仿照写了一个用BeautifulSoup实现的,直接上代码吧 #coding:gbk import urllib2 from bs4 import BeautifulSou ...

  4. JMeter学习-023-JMeter 命令行(非GUI)模式详解(一)-执行、输出结果及日志、简单分布执行脚本

    前文 讲述了JMeter分布式运行脚本,以更好的达到预设的性能测试(并发)场景.同时,在前文的第一章节中也提到了 JMeter 命令行(非GUI)模式,那么此文就继续前文,针对 JMeter 的命令行 ...

  5. 首次用U盘安装CentOS

    安之前看了这篇文章,http://www.osyunwei.com/archives/2307.html,然后就开始了. 首先下载ultraiso,官网下载的,将centos的iso镜像写到u盘上. ...

  6. Linq语言性能比较

    我不只一次听到不少做技术的朋友随口一句,“linq性能是最差的”,由于缺少具体的数字比照也就没在意,但心里隐隐觉得事实应该不是这样的,我记得我第一次听到有人贬低C# 3.0是在我工作后不久的一个夏季, ...

  7. Python开发【十一章】:数据库操作Memcache、Redis

    一.Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  8. 真机远程调试 ( IOS Android 以及微信,weex)

    1.以前cordova远程调试,Android的直接连接USB后,用chrome打开chrome://inspect网址 IOS的打开Safari的developer下. 这是因为cordova的we ...

  9. RabbitMQ模拟器使用方法

    同事推荐了一个很好玩的RabbitMQ模拟器,记录一下简要使用方法. 模拟器地址 http://tryrabbitmq.com/ 使用方法: 直接将画框左面的图标拖进画图区,构建想要的拓扑图: 按住A ...

  10. web前端基础知识 - Django进阶

    1. 路由系统 1.1 单一路由对应 url(r'^index$', views.index), 1.2 基于正则的路由 url(r'^index/(\d*)', views.index), url( ...