c# 调用Win32Api关闭当前应用的方法

Win32 API

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

  • 使用Win32 API,应用程序可以充分挖掘Windows的32位操作系统的潜力。 Microsoft的所有32位平台都支持统一的API,包括函数、结构、消息、宏及接口。使用 Win32 API不但可以开发出在各种平台上都能成功运行的应用程序,而且也可以充分利用每个平台特有的功能和属性。
  • 在具体编程时,程序实现方式的差异依赖于相应平台的底层功能的不同。最显著的差异是某些函数只能在更强大的平台上实现其功能。例如,安全函数只能在Windows NT操作系统下使用。另外一些主要差别就是系统限制,比如值的范围约束,或函数可管理的项目个数等等。

本文介绍Windows系统下使用Win32API获取当前应用并关闭的方法。

思路

  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关闭当前应用的方法的详细内容,更多关于c# 调用Win32Api关闭应用的资料请关注我们其它相关文章!

(0)

相关推荐

  • c# WPF设置软件界面背景为MediaElement并播放视频

    在我们的常见的软件界面设计中我们经常会设置软件的背景为SolidColorBrush或者LinerColorBrush.RadialGradientBrush 等一系列的颜色画刷为背景,有时我们也会使用ImageBrush添加图片来作为界面的背景,另外常用的还有DrawingBrush以及今天需要进行总结的VisualBrush,这些我们都是比较容易实现的,那么我们如果想将软件的界面设计成一个动画或者干脆播放一段视频作为背景,这个对于整个软件的效果又是一个巨大的提升. 首先我们来看看backgr

  • C# 中对象序列化XML的方法

    今天我们来看一下在C#中对象序列化XML的方法. 不得不说,在这个Json横行的年代,XML虽然式微,但也的确是一股子清流.(个人感觉) 不多说,直接开始. 首先先说怎么用 需要用到的是这两个命名空间(主要) using System.Xml; using System.Xml.Serialization; 然后序列化和反序列化的方式和Json一样.(后面提供封装方法) string result = XmlSerializeHelper.Serialize<test>(new test {

  • C# MJPEG 客户端简单实现方法

    MJPEG协议在此不在过多描述,这里主要介绍一下使用C#中的PictureBox控件频繁刷新MJPEG传输过来的图片,高频率的图片刷新实现视频播放效果: 环境: 服务端 MJPEG服务器使用的是手机的DroidCam,很方便的一个MJPEG服务器,端口4747,打开软件就能使用,并且还附带了web端展示. 客户端 MJPEG客户端使用C# Http请求,并获取到响应MJPEG视频流,截取到图片数据部分,用PictureBox展示图片内容. 整体流程: 1. C# 向MJPEG发送请求URL,请求

  • C# WPF 自定义按钮的方法

    本文介绍WPF一种自定义按钮的方法. 实现效果 使用图片做按钮背景: 自定义鼠标进入时效果: 自定义按压效果: 自定义禁用效果 实现效果如下图所示: 实现步骤 创建CustomButton.cs,继承自Button: 创建一个资源文件ButtonStyles.xaml: 在资源文件中设计按钮的Style: 在CustomButton.cs中添加Style中需要的依赖属性: 在程序中添加资源并引用(为了方便在不同的程序中引用自定义按钮,自定义按钮放在独立的类库中,应用程序中进行资源合并即可). 示

  • C# 基于消息发布订阅模型的示例(下)

    一 背景 在上面的一篇文章中我们介绍了一个完整地基于消息发布和订阅的模型,这篇文章我将介绍一种简单的基于消息的发布和订阅模型,在这个模型中我们将通过构建一个Publisher类来完成对特定的事件和事件订阅进行封装,这个是一个更加轻量级别的方式,使用这个的主要目的是降低类之间彼此的耦合程度,从而方便代码的扩展和访问,最终使代码结构更加合理. 我们首先来看看具体的Publisher类的构成,后面我们将会对这个类做一个详细的讲解和分析. using System; using System.Colle

  • C# WPF Image控件的绑定方法

    在我们平时的开发中会经常用到Image控件,通过设置Image控件的Source属性,我们可以加载图片,设置Image的source属性时可以使用相对路径也可以使用绝对路径,一般情况下建议使用绝对路径,类似于下面的形式Source="/Demo;Component/Images/Test.jpg"其中Demo表示工程的名称,后面表示具体哪个文件夹下面的哪个图片资源,在程序中,我们甚至可以为Image控件设置X:Name属性,在后台代码中动态去改变Image的Source,但我个人认为这

  • C# 基于消息发布订阅模型的示例(上)

    在我们的开发过程中,我们经常会遇到这样的场景就是一个对象的其中的一些状态依赖于另外的一个对象的状态,而且这两个对象之间彼此是没有关联的,及两者之间的耦合性非常低,特别是在这种基于容器模型的开发中遇到的会非常多,比如Prism框架或者MEF这种框架中,而我们会发现在这样的系统中我们经常使用一种Publish和Subscribe的模式来进行交互,这种交互有什么好处呢?基于带着这些问题的思考,我们来一步步来剖析! 首先第一步就是定义一个叫做IEventAggregator的接口,里面定义了一些重载的S

  • c# WPF中如何自定义MarkupExtension

    在介绍这一篇文章之前,我们首先来回顾一下WPF中的一些基础的概念,首先当然是XAML了,XAML全称是Extensible Application Markup Language (可扩展应用程序标记语言),是专门用于WPF技术中的UI设计语言,通过使用XAML语言,我们能够快速设计软件界面,同时能够通过绑定这种机制能够很好地实现界面和实现逻辑之间的解耦,这个就是MVVM模式的核心了,那么今天我们介绍的MarkupExtension和XAML之间又有哪些的关系呢? Markup Extensio

  • c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)

    背景 在很多的时候我们需要编辑DataGrid中每一个Cell,编辑后保存数据,原生的WPF中的DataGrid并没有提供这样的功能,今天通过一个具体的例子来实现这一个功能,在这个例子中DataGrid中的数据类型可能是多种多样的,有枚举.浮点类型.布尔类型.DateTime类型,每一种不同的类型需要双击以后呈现不同的效果,本文通过使用Xceed.Wpf.DataGrid这个动态控件库来实现这个功能,当前使用的Dll版本是2.5.0.0,不同的版本可能实现上面有差别,这个在使用的时候需要特别注意

  • c#使用win32api实现获取光标位置

    方法一:需要调用win32api,winform.wpf通用 [DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT lpPoint); [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT(int x, int y) { this.X = x;

随机推荐