C# 中调用C++ DLL (P/Invoke)

为了能用上原来的C++代码,只好研究下从C# 中调用DLL
首先必须要有一个声明,使用的是DllImport关键字:
包含DllImport所在的名字空间

  1. using System.Runtime.InteropServices;   
  2. public class XXXX{  
  3. [DllImport(“MyDLL.dll")]   
  4. public static extern int mySum (int a,int b);   
  5. }  


[DllImport(“MyDLL.dll")]
public static extern int mySum (int a,int b);
代码中DllImport关键字作用是告诉编译器入口点在哪里,并将打包函数捆绑在这个类中
在调用的时候
在类中的时候 直接   mySum(a,b);就可以了
在其他类中调用: XXXX. mySum(a,b);
[DllImport(“MyDLL.dll”)]在申明的时候还可以添加几个属性 [DllImport(“MyDLL.dll", EntryPoint=" mySum ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)] EntryPoint: 指定要调用的 DLL 入口点。默认入口点名称是托管方法的名称 。
CharSet: 控制名称重整和封送 String 参数的方式 (默认是UNICODE)
CallingConvention指示入口点的函数调用约定(默认WINAPI)(上次报告讲过的)
SetLastError 指示被调用方在从属性化方法返回之前是否调用 SetLastError Win32 API 函数 (C#中默认false )
int 类型

  1. [DllImport(“MyDLL.dll")]   
  2. //返回个int 类型   
  3. public static extern int mySum (int a1,int b1);   

//DLL中申明

  1. extern “C” __declspec(dllexport)  int WINAPI mySum(int a2,int b2)   
  2. {   
  3. //a2 b2不能改变a1 b1  
  4. //a2=..  
  5. //b2=...  
  6. return a+b;   
  7. }  
  1. //参数传递int 类型   
  2. public static extern int mySum (ref int a1,ref int b1);   


//DLL中申明

  1. extern “C” __declspec(dllexport)  int WINAPI mySum(int *a2,int *b2)   
  2. {   
  3. //可以改变 a1, b1  
  4. *a2=...  
  5. *b2=...  
  6. return a+b;   
  7. }   

DLL 需传入char *类型

  1. [DllImport(“MyDLL.dll")]   
  2. //传入值   
  3. public static extern int mySum (string  astr1,string bstr1);  


//DLL中申明

  1. extern “C” __declspec(dllexport)  int WINAPI mySum(char * astr2,char * bstr2)   
  2. {   
  3. //改变astr2 bstr 2  ,astr1 bstr1不会被改变  
  4. return a+b;   
  5. }  


DLL 需传出char *类型

  1. [DllImport(“MyDLL.dll")]   
  2. // 传出值  
  3. public static extern int mySum (StringBuilder abuf, StringBuilder bbuf );  


//DLL中申明

  1. extern “C” __declspec(dllexport)  int WINAPI mySum(char * astr,char * bstr)   
  2. {   
  3. //传出char * 改变astr bstr -->abuf, bbuf可以被改变  
  4. return a+b;   
  5. }  


DLL 回调函数
BOOL EnumWindows(WNDENUMPROC lpEnumFunc, LPARAM lParam)

  1. using System;   
  2. using System.Runtime.InteropServices;   
  3. public delegate bool CallBack(int hwnd, int lParam); //定义委托函数类型   
  4. public class EnumReportApp   
  5. {   
  6. [DllImport("user32")]   
  7. public static extern int EnumWindows(CallBack x, int y);   
  8. public static void Main() {   
  9.     CallBack myCallBack = new CallBack(EnumReportApp.Report); EnumWindows(myCallBack, 0);   
  10. }   
  11. public static bool Report(int hwnd, int lParam)   
  12. {   
  13.     Console.Write("Window handle is ");   
  14.     Console.WriteLine(hwnd); return true;   
  15. }   
  16. }  


DLL  传递结构   (见代码)
BOOL PtInRect(const RECT *lprc, POINT pt);

  1. using System.Runtime.InteropServices;   
  2. [StructLayout(LayoutKind.Sequential)]   
  3. public struct Point {  
  4. public int x;   
  5. public int y;  
  6. }   
  7. [StructLayout(LayoutKind.Explicit)]   
  8. public struct Rect   
  9. {   
  10. [FieldOffset(0)] public int left;   
  11. [FieldOffset(4)] public int top;  
  12. [FieldOffset(8)] public int right;   
  13. [FieldOffset(12)] public int bottom;  
  14. }   
  15. Class XXXX {   
  16. [DllImport("User32.dll")]   
  17. public static extern bool PtInRect(ref  Rect r, Point p);   
  18. }   
http://www.cnblogs.com/howard-queen/archive/2008/07/31/1257127.html
posted @ 2015-01-16 10:36  Net-Spider  阅读(246)  评论(0)    收藏  举报