iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >怎么在c#项目中通过调用Win32Api关闭当前应用
  • 450
分享到

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

2023-06-06 17:06:14 450人浏览 泡泡鱼
摘要

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

今天就跟大家聊聊有关怎么在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 Win32Apipublic 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关闭当前应用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网精选频道,感谢大家的支持。

--结束END--

本文标题: 怎么在c#项目中通过调用Win32Api关闭当前应用

本文链接: https://www.lsjlt.com/news/247965.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么在c#项目中通过调用Win32Api关闭当前应用
    今天就跟大家聊聊有关怎么在c#项目中通过调用Win32Api关闭当前应用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Win32 APIWin32 API即为Microsoft 32...
    99+
    2023-06-06
  • DEDECMS怎么调用当前所在栏目的顶级栏目ID
    本篇内容介绍了“DEDECMS怎么调用当前所在栏目的顶级栏目ID”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所...
    99+
    2024-04-02
  • 怎么在Python项目中调用C++进行封装
    怎么在Python项目中调用C++进行封装?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。Python主要用来做什么Python主要应用于:1、Web开发;2、数据科学研究;...
    99+
    2023-06-06
  • System.Windows.Interactivity怎么在c# 项目中使用
    这篇文章给大家介绍System.Windows.Interactivity怎么在c# 项目中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1 引入命名空间  通过在代码中引入System.Windows.Inter...
    99+
    2023-06-06
  • Newtonsoft.Json怎么在c#项目中使用
    本篇文章为大家展示了 Newtonsoft.Json怎么在c#项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1 实体类的 Json 序列化和反序列化我们以如下的 Person 类举例,其中...
    99+
    2023-06-06
  • atomic怎么在c++11项目中使用
    这期内容当中小编将会给大家带来有关atomic怎么在c++11项目中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。std::atomic_flag  std::atomic_flag是一个原子的布尔类...
    99+
    2023-06-06
  • C#中怎么通过调用Windows API托管对象
    本篇文章为大家展示了C#中怎么通过调用Windows API托管对象,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。C#中调用Windows API实例下面: FileStream ...
    99+
    2023-06-18
  • 怎么在java项目中运用回调机制
    怎么在java项目中运用回调机制?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。代码如下:public class Student { private Str...
    99+
    2023-05-31
    java ava
  • java多线程在项目中怎么应用
    Java多线程在项目中的应用主要有以下几个方面:1. 提高程序的并发性:多线程可以同时处理多个任务,提高程序的并发性,使得程序的执行...
    99+
    2023-09-29
    java
  • filter过滤器怎么在Java项目中使用
    这篇文章将为大家详细讲解有关filter过滤器怎么在Java项目中使用,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Filter过滤器技术。通过过滤器,可以对来自客户端的请求进行拦截,进行预...
    99+
    2023-05-31
    java ava filter
  • Echart图表在项目中的前后端怎么用
    这篇文章主要介绍了Echart图表在项目中的前后端怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。一、项目架构我的文章会涉及图片中的表格使用,如果你都学会了,可以去Ech...
    99+
    2023-06-21
  • DataGridView控件怎么在C#项目中使用
    DataGridView控件怎么在C#项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1) 创建课程信息表创建课程信息表的 SQL 语句如下。use&n...
    99+
    2023-06-08
  • static关键字怎么在java项目中使用
    本篇文章给大家分享的是有关static关键字怎么在java项目中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。静态方法通常在一个类中定义一个方法为static,即为静态方法...
    99+
    2023-05-31
    java static ava
  • 怎么在R语言项目中调用seq()函数
    这篇文章给大家介绍怎么在R语言项目中调用seq()函数,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。首先,我们来看一个seq()函数应用的实例!x <- seq(0, 10,&nbs...
    99+
    2023-06-08
  • 怎么在项目中应用SpringSecurity权限控制
    本篇内容主要讲解“怎么在项目中应用SpringSecurity权限控制”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么在项目中应用SpringSecurity权限控制”吧!要进行认证和授权需要...
    99+
    2023-07-02
  • Win10怎么关闭在应用商店中查找应用的窗口
    这篇文章给大家分享的是有关Win10怎么关闭在应用商店中查找应用的窗口的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。设置方法如下:打开开始菜单,直接输入“regedit”搜索并打开注册表编辑器定位到以下路径“HK...
    99+
    2023-06-10
  • inline内联函数怎么在C++项目中使用
    本篇文章为大家展示了 inline内联函数怎么在C++项目中使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。指定内联函数的方法很简单,只需要在函数定义处增加 inline 关键字。请看下面的例子:...
    99+
    2023-06-06
  • 怎么在android中通过调用H5显示加载中效果
    这期内容当中小编将会给大家带来有关怎么在android中通过调用H5显示加载中效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。wv.setWebViewClient(new WebViewCl...
    99+
    2023-05-30
    android h5
  • 怎么在win10系统中强制关闭应用程序
    这篇文章将为大家详细讲解有关怎么在win10系统中强制关闭应用程序,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。win10系统强制关闭应用程序方法/步骤:右键选择任务栏空白处,在弹出的快捷选项中选择“任务...
    99+
    2023-06-27
  • 怎么在vue项目中调用浏览器分享功能
    怎么在vue项目中调用浏览器分享功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。为什么要使用VueVue是一款友好的、多用途且高性能的JavaScript框架,使用vue...
    99+
    2023-06-14
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作