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);
{
[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);
解决方案 »
- PictureBox Vs2010
- 浩方对战平台类似的平台是怎么做的?用C#能完成吗
- 出一个题目,200分,考考你:
- C#在桌面程序中怎么使用Server.URLEncode("汇编")函数?
- 把下载的图片直接放进本地数据库,不在本地保存。请高手指点
- 修改app.config的问题
- c#的初学者,问个问题先关于文件的上传和下载的
- 请问在用C# WinFrom中的TextBox控件能否直接使用掩码定义储如:yyyy-mm-dd,###,###.00等格式。
- C# WinForm 点击一个自定义控件,在groupbox里面绘制样式
- 换了Win7系统后,vs2005遇到两个问题,请指教
- c#如何读写Office 文挡?
- 应用程序不能在其他机子上使用??
http://www.aspcn.com/Show.aspx?id=446
http://www.aspcn.com/Show.aspx?id=447
[ 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 // 光栅的处理数值
) ;