c# 调用Win32Api关闭当前应用的方法
Win32API
Win32API即为Microsoft32位平台的应用程序编程接口(ApplicationProgrammingInterface)。所有在Win32平台上运行的应用程序都可以调用这些函数
- 使用Win32API,应用程序可以充分挖掘Windows的32位操作系统的潜力。Microsoft的所有32位平台都支持统一的API,包括函数、结构、消息、宏及接口。使用Win32API不但可以开发出在各种平台上都能成功运行的应用程序,而且也可以充分利用每个平台特有的功能和属性。
- 在具体编程时,程序实现方式的差异依赖于相应平台的底层功能的不同。最显著的差异是某些函数只能在更强大的平台上实现其功能。例如,安全函数只能在WindowsNT操作系统下使用。另外一些主要差别就是系统限制,比如值的范围约束,或函数可管理的项目个数等等。
本文介绍Windows系统下使用Win32API获取当前应用并关闭的方法。
思路
- 使用EnumWindows接口枚举当前窗口;
- 过滤掉不可用、隐藏、最小化的窗口;
- 过滤掉子窗口;
- 通过标题、类名过滤掉系统窗口;
- 使用PostMessage发送关闭窗口信息。
具体实现
//过滤掉系统的一些窗口
privatestaticstring[]filterTitles=newstring[1]{"programmanager"};
privatestaticstring[]filterClasses=newstring[5]{"shell_traywnd","workerw","button","progman","windows.ui.core.corewindow"};
privatevoidCloseCurrentApp()
{
CallBacksort=newCallBack(EnumCallback);
EnumWindows(sort,0);
return;
}
privateboolEnumCallback(IntPtrhwnd,intlParam)
{
stringtitle=GetWindowText(hwnd);
StringBuilderclassName=newStringBuilder(256);
intnRet=GetClassName(hwnd,className,className.Capacity);
if(nRet==0)
className.Append("");
if(!IsWindowVisible(hwnd))
returntrue;
if(!IsWindowEnabled(hwnd))
returntrue;
if(IsIconic(hwnd))
returntrue;
//过滤掉子窗口
IntPtrparent=GetParent(hwnd);
stringparentTitle=GetWindowText(parent);
if(parent!=IntPtr.Zero)
{
if(IsWindowVisible(parent)&&IsWindowEnabled(parent))
returntrue;
}
IntPtrowner=GetWindow(hwnd,GW_OWNER);
if(owner!=IntPtr.Zero)
{
if(IsWindowVisible(owner)&&IsWindowEnabled(owner))
returntrue;
}
if(!filterTitles.Contains(title.ToLower())&&!filterClasses.Contains(className.ToString().ToLower()))
{
PostMessage(hwnd,WM_SYSCOMMAND,SC_CLOSE,0);
Console.WriteLine("关闭窗口(句柄:{0},标题:{1})!",hwnd,title);
#region获取窗口信息
intprocessID=-1;
longthreadID=-1;
processID=GetWindowThreadProcessId(hwnd,outthreadID);
boolisiconic=IsIconic(hwnd);
uintgwlStyle=(uint)GetWindowLong(hwnd,GWL_STYLE);
IntPtrhProcess=OpenProcess(ProcessAccessFlags.QueryInformation,false,processID);
stringfullPath="";
if(hProcess!=IntPtr.Zero)
{
intcapacity=1024;
StringBuilderprocessName=newStringBuilder(capacity);
QueryFullProcessImageName(hProcess,0,processName,refcapacity);
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);
WINDOWPLACEMENTplacement=newWINDOWPLACEMENT();
placement.length=System.Runtime.InteropServices.Marshal.SizeOf(placement);
GetWindowPlacement(hwnd,refplacement);
Console.WriteLine("====placement:{0}====",placement.showCmd);
EnumPropsDelegateprop=newEnumPropsDelegate(EnumPropsProc);
EnumProps(hwnd,prop);
#endregion获取窗口信息
returnfalse;
}
returntrue;
}
privateboolEnumPropsProc(IntPtrhwnd,IntPtrlpszString,IntPtrhData)
{
stringpropName=System.Runtime.InteropServices.Marshal.PtrToStringAnsi(lpszString);
Console.WriteLine("====属性:{0}数据:{1}====",propName,hData);
returntrue;
}
#regionWin32Api
publicconstintGWL_STYLE=(-16);
publicconstintGWL_EXSTYLE=(-20);
publicconstintGW_OWNER=4;
publicconstintWS_EX_TOOLWINDOW=0x00000080;
publicconstintWM_SYSCOMMAND=0x0112;
publicconstintWM_CLOSE=0x10;
publicconstintSC_CLOSE=0xF060;
publicdelegateboolCallBack(IntPtrhwnd,intlparam);
publicdelegateboolEnumPropsDelegate(IntPtrhwnd,IntPtrlpszString,IntPtrhData);
[DllImport("user32.dll")]
publicstaticexternintEnumWindows(CallBackx,inty);
[DllImport("user32.dll",CharSet=CharSet.Auto)]
internalstaticexternintGetWindowText(IntPtrhWnd,System.Text.StringBuilderlpString,intnMaxCount);
[DllImport("user32.dll",CharSet=CharSet.Auto)]
publicstaticexternintGetWindowTextLength(IntPtrhWnd);
[DllImport("user32.dll",SetLastError=true,CharSet=CharSet.Auto)]
publicstaticexternintGetClassName(IntPtrhWnd,System.Text.StringBuilderlpClassName,intnMaxCount);
[DllImport("user32.dll")]
publicstaticexternboolIsWindowVisible(IntPtrhwnd);
[DllImport("user32.dll")]
publicstaticexternboolIsWindowEnabled(IntPtrhwnd);
[DllImport("user32.dll",EntryPoint="IsIconic")]
publicstaticexternboolIsIconic(IntPtrhWnd);
[DllImport("user32.dll",SetLastError=true)]
publicstaticexternIntPtrGetParent(IntPtrhwnd);
[DllImport("user32.dll",SetLastError=true)]
publicstaticexternIntPtrGetWindow(IntPtrhwndParent,intnCmd);
[DllImport("user32.dll",EntryPoint="GetWindowLongA",SetLastError=true)]
publicstaticexternlongGetWindowLong(IntPtrhwnd,intnIndex);
[DllImport("user32.dll",EntryPoint="PostMessageA",SetLastError=true)]
publicstaticexternboolPostMessage(IntPtrhwnd,uintMsg,uintwParam,uintlParam);
[DllImport("user32.dll",EntryPoint="GetWindowThreadProcessId",SetLastError=true,
CharSet=CharSet.Unicode,ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
publicstaticexternintGetWindowThreadProcessId(IntPtrhWnd,outlonglpdwProcessId);
[DllImport("kernel32.dll",SetLastError=true)]
publicstaticexternIntPtrOpenProcess(
ProcessAccessFlagsprocessAccess,
boolbInheritHandle,
intprocessId
);
[DllImport("kernel32.dll",SetLastError=true)]
publicstaticexternboolQueryFullProcessImageName([In]IntPtrhProcess,[In]intdwFlags,
[Out]System.Text.StringBuilderlpExeName,refintlpdwSize);
[DllImport("coredll.dll",SetLastError=true,CallingConvention=CallingConvention.Winapi,CharSet=CharSet.Auto)]
[return:MarshalAs(UnmanagedType.Bool)]
publicstaticexternboolCloseHandle(IntPtrhObject);
[DllImport("user32.dll",SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
publicstaticexternboolGetWindowPlacement(IntPtrhWnd,refWINDOWPLACEMENTlpwndpl);
[DllImport("user32.dll")]
publicstaticexternintEnumProps(IntPtrhWnd,EnumPropsDelegatelpEnumFunc);
publicstructWINDOWPLACEMENT
{
publicintlength;
publicintflags;
publicintshowCmd;
publicSystem.Drawing.PointptMinPosition;
publicSystem.Drawing.PointptMaxPosition;
publicSystem.Drawing.RectanglercNormalPosition;
}
[Flags]
publicenumProcessAccessFlags: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
}
publicstaticstringGetWindowText(IntPtrhwnd)
{
intcapacity=GetWindowTextLength(hwnd)*2;
System.Text.StringBuilderlpString=newSystem.Text.StringBuilder(capacity);
GetWindowText(hwnd,lpString,lpString.Capacity);
if(lpString.Length>0)
{
returnlpString.ToString();
}
returnstring.Empty;
}
#endregionWin32Api
以上就是c#调用Win32Api关闭当前应用的方法的详细内容,更多关于c#调用Win32Api关闭应用的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。