DLLImport的用法C#

它来调用WIN32的API或者调用一下C或C++编写的DLL。
使用实例:
将编译好的C++ DLL拷贝到BIN目录(DLLImport会从程序启动目录BIN开始查找相应名称的DLL,未找到则转至system32下查找)

程序写法:
1、引用命名空间:

using System.Runtime.InteropServices;

 2、创建函数名称

[DllImport("demo.dll")]
public static extern bool OpenDemo();

 3、DllImportAttribute属性用法

[AttributeUsage(AttributeTargets.Method)]
public class DllImportAttribute: System.Attribute
{
   public DllImportAttribute(string dllName) {…} 	//定位参数为dllName
   public CallingConvention CallingConvention; 		//入口点调用约定
   public CharSet CharSet;                              //入口点采用的字符接
   public string EntryPoint;  				//入口点名称
   public bool ExactSpelling;   			//是否必须与指示的入口点拼写完全一致,默认false
   public bool PreserveSig;  				//方法的签名是被保留还是被转换
   public bool SetLastError;  				//FindLastError方法的返回值保存在这里
   public string Value { get {…} }
}

猜你喜欢

转载自www.cnblogs.com/sntetwt/p/9896799.html