[英]I wish to wrap a DLL in C#
System.Windows.Forms.dll
我希望将一些函数包装在C#类的此文件中。
特别是这些: https : //msdn.microsoft.com/zh-cn/library/system.windows.forms.cursor(v=vs.110).aspx
但是我不确定如何获取功能列表。 我尝试了没有返回结果的程序。 我想知道是否有人可以给我1个例子。
例如,这将返回EntryPointNotFoundException
[DllImport("System.Windows.Forms.dll")]
public static extern void SetCursor(String s);
首先,您需要启用互操作服务才能调用Windows函数:
using System.Runtime.InteropServices;
然后,您只需声明要导入的方法,如下所示:
[DllImport("winmm.dll")]
public static extern bool PlaySound(string filename,long hmodule, int dword );
这将在不受管理的winmm.dll中为PlaySound方法创建“映射”。该方法被创建为静态方法,并且使用extern关键字会告诉其他方法该方法在您的类外部(不在内部运行)
要获取列表/搜索非托管功能,您可以使用
例如,对于SetCursor
您可以调用
http://www.pinvoke.net/search.aspx?search=SetCursor&namespace= [全部]
例如将光标放置在您可以执行的SetCursorPos
提供的位置
using System.Runtime.InteropServices;
...
// Wrapper
class CursorNativeMethods {
[DllImport("User32.dll",
EntryPoint = "SetCursorPos",
CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean SetCursorPos(Point point);
...
[DllImport("User32.dll",
EntryPoint = "GetCursorPos",
CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean GetCursorPos([Out] out Point point);
...
}
// Your Routine
public static class MyCursor {
public Point Location {
get {
Point pt = new Point(-1, -1);
if (CursorNativeMethods.GetCursorPos(out pt))
return pt;
else
return new Point(-1, -1);
}
set {
CursorNativeMethods.SetCursorPos(value);
}
}
...
}
请注意, System.Windows.Forms.dll
是托管程序集 (您不应与之互操作),而User32.dll
是非托管库 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.