C# Win32API
[assembly: AssemblyVersion("1.0.0.3")]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Win32API
{
#region ==== Kernel32.dll ====
/// <summary>
/// 取得当前线程ID
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CreateFile(
string lpFileName,
FileAccess dwDesiredAccess,
FileShare dwShareMode,
int lpSecurityAttributes,
FileMode dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);
/// <summary>
/// 打开进程
/// </summary>
/// <param name="dwDesiredAccess">想得到的访问权限</param>
/// <param name="bInheritHandle">指定返回的句柄是否可以被继承</param>
/// <param name="dwProcessId">指定要打开的进程的ID</param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr OpenProcess(DesiredAccess dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
/// <summary>
/// 结束其它进程
/// </summary>
/// <param name="hProcess"></param>
/// <param name="uExitCode"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
/// <summary>
///
/// </summary>
/// <param name="hProcess"></param>
/// <param name="lpBaseAddress"></param>
/// <param name="lpBuffer"></param>
/// <param name="nSize"></param>
/// <param name="lpNumberOfBytesRead"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, out uint lpNumberOfBytesRead);
/// <summary>
///
/// </summary>
/// <param name="dwFlags"></param>
/// <param name="th32ProcessID"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
//[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//public static extern bool Process32First(IntPtr hSnapshot, LPPROCESSENTRY32W lppe);
#endregion
#region ==== User32.dll ====
/// <summary>
/// 继续下一个钩子
/// </summary>
/// <param name="hhk"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// 获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数不查找子窗口。在查找时不区分大小写。
/// </summary>
/// <param name="lpClassName">指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低 16位,高位必须为 0。</param>
/// <param name="lpWindowName">指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。</param>
/// <returns>如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;如果函数失败,返回值为NULL</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 检取表示键名的字符串
/// </summary>
/// <param name="lParam"></param>
/// <param name="lpBuffer"></param>
/// <param name="nSize"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int GetKeyNameText(IntPtr lParam, string lpBuffer, int nSize);
/// <summary>
///
/// </summary>
/// <param name="hWnd">收消息的窗口的句柄</param>
/// <param name="Msg">指定被发送的消息</param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern long SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// 改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。子窗口,弹出式窗口,及顶层窗口根据它们在屏幕上出现的顺序排序、顶层窗口设置的级别最高,并且被设置为Z序的第一个窗口。
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="hWndInsertAfter">在z序中的位于被置位的窗口前的窗口句柄</param>
/// <param name="X">以客户坐标指定窗口新位置的左边界</param>
/// <param name="Y">以客户坐标指定窗口新位置的顶边界</param>
/// <param name="cx">以像素指定窗口的新的宽度</param>
/// <param name="cy">以像素指定窗口的新的高度</param>
/// <param name="uFlags">窗口尺寸和定位的标志</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP_Flags uFlags);
/// <summary>
/// 安装钩子
/// </summary>
/// <param name="idHook">钩子类型</param>
/// <param name="lpfn">钩子函数的地址</param>
/// <param name="hMod">钩子函数所在DLL的实例名柄. 如果一个局部的钩子, 该参数的值为 NULL</param>
/// <param name="dwThreadId">要安装钩子的线程, 0 为全局钩子</param>
/// <returns>钩子句柄</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(WH_Codes idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
/// <summary>
/// 卸载钩子
/// </summary>
/// <param name="hhk">要卸载的钩子的句柄</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
#endregion
#region ==== 文件系统 ====
#region ==== 磁盘和驱动器管理类 API ===
#endregion
#endregion
}
{
#region ==== Kernel32.dll ====
/// <summary>
/// 取得当前线程ID
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern uint GetCurrentThreadId();
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CreateFile(
string lpFileName,
FileAccess dwDesiredAccess,
FileShare dwShareMode,
int lpSecurityAttributes,
FileMode dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);
/// <summary>
/// 打开进程
/// </summary>
/// <param name="dwDesiredAccess">想得到的访问权限</param>
/// <param name="bInheritHandle">指定返回的句柄是否可以被继承</param>
/// <param name="dwProcessId">指定要打开的进程的ID</param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr OpenProcess(DesiredAccess dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
/// <summary>
/// 结束其它进程
/// </summary>
/// <param name="hProcess"></param>
/// <param name="uExitCode"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool TerminateProcess(IntPtr hProcess, uint uExitCode);
/// <summary>
///
/// </summary>
/// <param name="hProcess"></param>
/// <param name="lpBaseAddress"></param>
/// <param name="lpBuffer"></param>
/// <param name="nSize"></param>
/// <param name="lpNumberOfBytesRead"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, uint nSize, out uint lpNumberOfBytesRead);
/// <summary>
///
/// </summary>
/// <param name="dwFlags"></param>
/// <param name="th32ProcessID"></param>
/// <returns></returns>
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags, uint th32ProcessID);
//[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
//public static extern bool Process32First(IntPtr hSnapshot, LPPROCESSENTRY32W lppe);
#endregion
#region ==== User32.dll ====
/// <summary>
/// 继续下一个钩子
/// </summary>
/// <param name="hhk"></param>
/// <param name="nCode"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
/// <summary>
/// 获得一个顶层窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数不查找子窗口。在查找时不区分大小写。
/// </summary>
/// <param name="lpClassName">指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低 16位,高位必须为 0。</param>
/// <param name="lpWindowName">指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。</param>
/// <returns>如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;如果函数失败,返回值为NULL</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 检取表示键名的字符串
/// </summary>
/// <param name="lParam"></param>
/// <param name="lpBuffer"></param>
/// <param name="nSize"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int GetKeyNameText(IntPtr lParam, string lpBuffer, int nSize);
/// <summary>
///
/// </summary>
/// <param name="hWnd">收消息的窗口的句柄</param>
/// <param name="Msg">指定被发送的消息</param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern long SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
/// <summary>
/// 改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序。子窗口,弹出式窗口,及顶层窗口根据它们在屏幕上出现的顺序排序、顶层窗口设置的级别最高,并且被设置为Z序的第一个窗口。
/// </summary>
/// <param name="hWnd">窗口句柄</param>
/// <param name="hWndInsertAfter">在z序中的位于被置位的窗口前的窗口句柄</param>
/// <param name="X">以客户坐标指定窗口新位置的左边界</param>
/// <param name="Y">以客户坐标指定窗口新位置的顶边界</param>
/// <param name="cx">以像素指定窗口的新的宽度</param>
/// <param name="cy">以像素指定窗口的新的高度</param>
/// <param name="uFlags">窗口尺寸和定位的标志</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SWP_Flags uFlags);
/// <summary>
/// 安装钩子
/// </summary>
/// <param name="idHook">钩子类型</param>
/// <param name="lpfn">钩子函数的地址</param>
/// <param name="hMod">钩子函数所在DLL的实例名柄. 如果一个局部的钩子, 该参数的值为 NULL</param>
/// <param name="dwThreadId">要安装钩子的线程, 0 为全局钩子</param>
/// <returns>钩子句柄</returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr SetWindowsHookEx(WH_Codes idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);
/// <summary>
/// 卸载钩子
/// </summary>
/// <param name="hhk">要卸载的钩子的句柄</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(IntPtr hhk);
#endregion
#region ==== 文件系统 ====
#region ==== 磁盘和驱动器管理类 API ===
#endregion
#endregion
}