DllImport 属性用于指定包含外部方法的实现的 dll 位置。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 {...} }
   }
}
准确地说,DllImport 属性具有下列行为: 它只能放置在方法声明上。 
它具有单个定位参数:指定包含被导入方法的 dll 名称的 dllName 参数。 
它具有五个命名参数: 
CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。 
CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。 
EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。 
ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。 
PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。 
SetLastError 参数指示方法是否保留 Win32“上一错误”。如果未指定 SetLastError,则使用默认值 false。 
它是一次性属性类。 
此外,用 DllImport 属性修饰的方法必须具有 extern 修饰符。例如:
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",  SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);

解决方案 »

  1.   

    http://www.aspcn.com/Show.aspx?id=445
    http://www.aspcn.com/Show.aspx?id=446
    http://www.aspcn.com/Show.aspx?id=447
      

  2.   

    举个离子: //声明一个API函数
    [ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
    private static extern bool BitBlt (
    IntPtr hdcDest , // 目标 DC的句柄
    int nXDest , 
    int nYDest , 
    int nWidth , 
    int nHeight , 
    IntPtr hdcSrc ,  // 源DC的句柄
    int nXSrc , 
    int nYSrc , 
    System.Int32 dwRop  // 光栅的处理数值
    ) ;