求教DllImport和DllImportAttribute用法上的区别

mote 2003-03-25 12:00:52
DllImport和DllImportAttribute都能引入dll文件,他们在用法上有什么区别吗?
...全文
661 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]是这特性的描述。

110,499

社区成员

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

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

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