一、简介
DllImport所在的名字空间 using System.Runtime.InteropServices,
DllImport的用法:
DllImport("MyDllImport.dll")]
private static extern int mySum(int a,int b);
[DllImport("kernel32.dll", SetLastError = true, EntryPoint = "SetLocalTime")]
public static extern int SetSystemTime(ref SystemTime lpSystemTime);
1、[DllImport("kernel32.dll")]只能放到方法上面声明
2、DllImport具有单个定位参数:制定包含被导入方法的名称dllName参数
3、DllImport具有五个命名参数
1、CallingConvention :参数指示入口点的调用约定,如果未指定 CallingConvention,则使用默认值CallingConvention=CallingConvention.Winapi;
2、CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值
CharSet.Auto3、EntryPoint 参数给出 dll 中入口点的名称。如果未指定EntryPoint,则使用方法本身的名称
4、ExactSpelling 参数指示 EntryPoint是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 ExactSpelling=false;
5、PreserveSig参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT返回值和该返回值的一个名为 retval的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true PreserveSig=true;
6、SetLastError 参数指示方法是否保留Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。 SetLastError=true;
4、它是一次性属性类。
5、用 DllImport 属性修饰的方法必须具有 extern 修饰符。
[DllImport("user32.dll")]//--引入API
public static extern ReturnType FunctionName(type arg1,type arg2,...);//--声明方法