• DllImport 调用DLL类库


    .net 框架程序可以通过静态 DLL 入口点的方式来访问本机代码库。DllImport 属性用于指定包含外部方法的实现的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 修饰符。
       
    下面是 C# 调用 Win32 MessageBox 函数的示例:
      
    using System;
    using System.Runtime.InteropServices;
    class MainApp
    {

         //通过DllImport引用user32.dll类。MessageBox来自于user32.dll类
     [DllImport("user32.dll", EntryPoint="MessageBox")]
     public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);
     public static void Main()
     {
      MessageBox( 0, "您好,这是 PInvoke!", ".net", 0 );
     }
    }
      
      
    C#调用标准动态库的问题

           在我以前的文件中讲到过, C#调用Win32API, 原理是一样的. 这里我详细讲解用C写一个标准的动态库, 然后让C#调用. (本篇适合初学者, 中间没有任何冗余代码, 简洁明了)
    1.制作标准动态库
    __declspec(dllexport) int __cdecl add(int, int);//这一句是声明动态库输出一个可供外不调用的函数原型.
    int add(int a,int b) {//实现这个函数
    return a+b;
    }
    以上简单3行代码,声明一个add的方法, 输入参数是两个int参数,返回这两个数之和. 保存为MyLib.c
    然后执行编译命令.
    H:/XSchool/C#-School/HowTo>cl /LD MyLib.c
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

    MyLib.c
    Microsoft (R) Incremental Linker Version 6.00.8447
    Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

    /out:MyLib.dll
    /dll
    /implib:MyLib.lib
    MyLib.obj
       Creating library MyLib.lib and object MyLib.exp

    确信有以上输出, 说明编译成功生成了动态库.

    2.编写C-Sharp程序调用该动态库
    using System;
    using System.Runtime.InteropServices;//这是用到DllImport时候要引入的包

    public class InvokeDll {
    [DllImport("MyLib.dll", CharSet=CharSet.Auto)]
    static extern int add(int a,int b);//声明外部的标准动态库, 跟Win32API是一样的.

    public static void Main() {
    Console.WriteLine(add(10,30));
    }
    }
    保存为InvokeDll.cs文件, 与MyLib.dll置于同一目录, 编译该文件.
    H:/XSchool/C#-School/HowTo>csc invokedll.cs
    将生成Invokedll.exe, 可以执行该文件.

  • 相关阅读:
    国外高手又一力作,CAML Builder
    用word2007发布blog!!
    SharePoint Server 2007 trial 年末大餐细细品尝 (第八口)
    SharePoint Server 2007 trial 年末大餐细细品尝 (再来一口)
    SharePoint Server 2007 trial 年末大餐细细品尝 (第三口)
    SharePoint Server 2007 trial 年末大餐细细品尝 (第十口)
    卡壳了~~~
    SharePoint Server 2007 trial 年末大餐细细品尝 (第九口)
    moss 与 ad 用户信息不同步的问题。
    SharePoint Server 2007 trial 年末大餐细细品尝 (第六口)
  • 原文地址:https://www.cnblogs.com/mapstar/p/16598512.html
Copyright © 2020-2023  润新知