温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在c#项目中通过调用Win32Api关闭当前应用

发布时间:2021-03-03 15:40:47 来源:亿速云 阅读:161 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在c#项目中通过调用Win32Api关闭当前应用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Win32 API

Win32 API即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface)。所有在Win32平台上运行的应用程序都可以调用这些函数

  • 使用Win32 API,应用程序可以充分挖掘Windows的32位操作系统的潜力。 Microsoft的所有32位平台都支持统一的API,包括函数、结构、消息、宏及接口。使用 Win32 API不但可以开发出在各种平台上都能成功运行的应用程序,而且也可以充分利用每个平台特有的功能和属性。

  • 在具体编程时,程序实现方式的差异依赖于相应平台的底层功能的不同。最显著的差异是某些函数只能在更强大的平台上实现其功能。例如,安全函数只能在Windows NT操作系统下使用。另外一些主要差别就是系统限制,比如值的范围约束,或函数可管理的项目个数等等。

思路

  1. 使用EnumWindows接口枚举当前窗口;

  2. 过滤掉不可用、隐藏、最小化的窗口;

  3. 过滤掉子窗口;

  4. 通过标题、类名过滤掉系统窗口;

  5. 使用PostMessage发送关闭窗口信息。

具体实现

// 过滤掉系统的一些窗口
private static string[] filterTitles = new string[1] { "program manager"};
private static string[] filterClasses = new string[5] { "shell_traywnd", "workerw", "button", "progman", "windows.ui.core.corewindow"};

private void CloseCurrentApp()
{
 CallBack sort = new CallBack(EnumCallback);
 EnumWindows(sort, 0);
 return;
}

private bool EnumCallback(IntPtr hwnd, int lParam)
{
 string title = GetWindowText(hwnd);
 StringBuilder className = new StringBuilder(256);
 int nRet = GetClassName(hwnd, className, className.Capacity);
 if (nRet == 0)
  className.Append("");

 if (!IsWindowVisible(hwnd))
  return true;

 if (!IsWindowEnabled(hwnd))
  return true;

 if (IsIconic(hwnd))
  return true;

 // 过滤掉子窗口
 IntPtr parent = GetParent(hwnd);
 string parentTitle = GetWindowText(parent);
 if (parent != IntPtr.Zero)
 {
  if (IsWindowVisible(parent) && IsWindowEnabled(parent))
   return true;
 }

 IntPtr owner = GetWindow(hwnd, GW_OWNER);
 if (owner != IntPtr.Zero)
 {
  if (IsWindowVisible(owner) && IsWindowEnabled(owner))
   return true;
 }

 if (!filterTitles.Contains(title.ToLower()) && !filterClasses.Contains(className.ToString().ToLower()))
 {
  PostMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0);
  Console.WriteLine("关闭窗口(句柄:{0}, 标题:{1})!", hwnd, title);

  #region 获取窗口信息
  int processID = -1;
  long threadID = -1;
  processID = GetWindowThreadProcessId(hwnd, out threadID);
  bool isiconic = IsIconic(hwnd);
  uint gwlStyle = (uint)GetWindowLong(hwnd, GWL_STYLE);

  IntPtr hProcess = OpenProcess(ProcessAccessFlags.QueryInformation, false, processID);
  string fullPath = "";
  if (hProcess != IntPtr.Zero)
  {
   int capacity = 1024;
   StringBuilder processName = new StringBuilder(capacity);
   QueryFullProcessImageName(hProcess, 0, processName, ref capacity);
   fullPath = processName.ToString(0, capacity);
   CloseHandle(hProcess);
  }

  Console.WriteLine("-------------------窗口info:---------------");
  Console.WriteLine("====标题:{0} 句柄:{1}====", title, hwnd);
  Console.WriteLine("====父窗口标题:{0} 父窗口句柄:{1}====", parentTitle, parent);
  Console.WriteLine("====进程ID:{0} 类名:{1}====", processID, className.ToString());
  Console.WriteLine("====进程名:{0}====", fullPath);
  Console.WriteLine("====isiconic:{0} 样式:{1}====", isiconic, gwlStyle);
  WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
  placement.length = System.Runtime.InteropServices.Marshal.SizeOf(placement);
  GetWindowPlacement(hwnd, ref placement);
  Console.WriteLine("====placement:{0}====", placement.showCmd);
  EnumPropsDelegate prop = new EnumPropsDelegate(EnumPropsProc);
  EnumProps(hwnd, prop);
  #endregion 获取窗口信息

  return false;
 }

 return true;
}

private bool EnumPropsProc(IntPtr hwnd, IntPtr lpszString, IntPtr hData)
{
 string propName = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(lpszString);
 Console.WriteLine("====属性:{0} 数据:{1}====", propName, hData);
 return true;
}

#region Win32Api
public const int GWL_STYLE = (-16);
public const int GWL_EXSTYLE = (-20);
public const int GW_OWNER = 4;
public const int WS_EX_TOOLWINDOW = 0x00000080;
public const int WM_SYSCOMMAND = 0x0112;
public const int WM_CLOSE = 0x10;
public const int SC_CLOSE = 0xF060;

public delegate bool CallBack(IntPtr hwnd, int lparam);
public delegate bool EnumPropsDelegate(IntPtr hwnd, IntPtr lpszString, IntPtr hData);

[DllImport("user32.dll")]
public static extern int EnumWindows(CallBack x, int y);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);

[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern bool IsWindowEnabled(IntPtr hwnd);

[DllImport("user32.dll", EntryPoint = "IsIconic")]
public static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hwnd);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindow(IntPtr hwndParent, int nCmd);

[DllImport("user32.dll", EntryPoint = "GetWindowLongA", SetLastError = true)]
public static extern long GetWindowLong(IntPtr hwnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
public static extern bool PostMessage(IntPtr hwnd, uint Msg, uint wParam, uint lParam);

[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId", SetLastError = true,
  CharSet = CharSet.Unicode, ExactSpelling = true,
  CallingConvention = CallingConvention.StdCall)]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out long lpdwProcessId);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr OpenProcess(
  ProcessAccessFlags processAccess,
  bool bInheritHandle,
  int processId
);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool QueryFullProcessImageName([In]IntPtr hProcess, [In]int dwFlags,
 [Out]System.Text.StringBuilder lpExeName, ref int lpdwSize);

[DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);

[DllImport("user32.dll")]
public static extern int EnumProps(IntPtr hWnd, EnumPropsDelegate lpEnumFunc);

public struct WINDOWPLACEMENT
{
 public int length;
 public int flags;
 public int showCmd;
 public System.Drawing.Point ptMinPosition;
 public System.Drawing.Point ptMaxPosition;
 public System.Drawing.Rectangle rcNormalPosition;
}

[Flags]
public enum ProcessAccessFlags : uint
{
 All = 0x001F0FFF,
 Terminate = 0x00000001,
 CreateThread = 0x00000002,
 VirtualMemoryOperation = 0x00000008,
 VirtualMemoryRead = 0x00000010,
 VirtualMemoryWrite = 0x00000020,
 DuplicateHandle = 0x00000040,
 CreateProcess = 0x000000080,
 SetQuota = 0x00000100,
 SetInformation = 0x00000200,
 QueryInformation = 0x00000400,
 QueryLimitedInformation = 0x00001000,
 Synchronize = 0x00100000
}

public static string GetWindowText(IntPtr hwnd)
{
 int capacity = GetWindowTextLength(hwnd) * 2;
 System.Text.StringBuilder lpString = new System.Text.StringBuilder(capacity);
 GetWindowText(hwnd, lpString, lpString.Capacity);
 if (lpString.Length > 0)
 {
  return lpString.ToString();
 }
 return string.Empty;
}
#endregion Win32Api

看完上述内容,你们对怎么在c#项目中通过调用Win32Api关闭当前应用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI