求教DllImport和DllImportAttribute用法上的区别

mote 2003-03-25 12:00:52
DllImport和DllImportAttribute都能引入dll文件,他们在用法上有什么区别吗?
...全文
669 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mote 2003-03-26
  • 打赏
  • 举报
回复
以下是我在书上看见的一段代码,如果两者相同,为何作者要在同一段代码中应用两种不同的编程风格?都用DllImport不是简单多呢?
[DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // hanlde to destination Dc (device context)
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

[DllImport("gdi32.dll")]
private static extern IntPtr CreateDC(
String DriverName, // driver name
String DeviceName, // device name
String Output, // not used; should be NULL
IntPtr lpInitData // optional printer data
);

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool DeleteDC(
IntPtr dc
);

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
private static extern unsafe bool ClientToScreen(
IntPtr hWnd, // handle to window
Point* lpPoint // screen coordinates
);
Knight94 2003-03-25
  • 打赏
  • 举报
回复
我觉得DllImport是关键字,而引入DLL需要写明其它属性,例如如下所示,DllImport后面括号中间都是DllImportAttribute
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);

robosmart 2003-03-25
  • 打赏
  • 举报
回复
DllImport = DllImportAttribute,Attribute在使用时可以省略不写
yarshray 2003-03-25
  • 打赏
  • 举报
回复
一样的东西

DllImportAttribute是这个特性的类



[DllImport]是这特性的描述。
大家在实际工作学习C#的时候,可能会问:为什么我们要为一些已经存在的功能(比如Windows中的一些功能,C++中已经编写好的一些方法)要重新编写代码,C#有没有方法可以直接都用这些原本已经存在的功能呢?答案是肯定的,大家可以通过C#中的DllImport直接调用这些功能。 DllImport所在的名字空间 using System.Runtime.InteropServices; MSDN中对DllImportAttribute的解释是这样的:可将该属性应用于方法。DllImportAttribute 属性提供对从非托管 DLL 导出的函数进行调用所必需的信息。作为最低要求,必须提供包含入口点的 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 修饰符。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧