永发信息网

如何用vb关闭一个应用程序

答案:4  悬赏:70  手机版
解决时间 2021-12-28 21:50
  • 提问者网友:你挡着我发光了
  • 2021-12-28 08:21
如何用vb关闭一个应用程序
最佳答案
  • 五星知识达人网友:你哪知我潦倒为你
  • 2022-01-06 05:54
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。

'下面的代码放到模块中 
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 'FindWindowAPI函数
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 'PostMessageAPI函数
Public Const WM_CLOSE = &H10 '常用变量定义

'程序代码
Dim winHwnd As Long '定义一个长整形变量winHwnd
Dim RetVal As Long '定义一个长整形变量RetVal
winHwnd = FindWindow(vbNullString, "Calculator") 'API函数查找"Calculator"这个窗口
Debug.Print winHwnd '显示这个窗口句柄
If winHwnd <> 0 Then '如不为0,表示找到窗口
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) '向这个窗口发送关闭信息
If RetVal = 0 Then '如果返回信息为0.表示失败,未发送成功
MsgBox "Error posting message." '提示发送失败
End If
Else
MsgBox "The Calculator is not open." '提示没有找到打开的程序
End If
全部回答
  • 1楼网友:山君与见山
  • 2022-01-06 08:41
控件:microsoft common dialog contral 的 :listviwe 两个命令按钮: 一个时间控件 一个listviwe &apos;先复制此代码在最前 &apos;======================用于查找进程和终止进程的api函数常数定义================ ===== private declare function createtoolhelpsnapshot lib &quot;kernel32&quot; alias &quot;createtoolhelp32snapshot&quot; (byval lflags as long, byval lprocessid as long) as long private declare function processfirst lib &quot;kernel32&quot; alias &quot;process32first&quot; (byval hsnapshot as long, uprocess as processentry32) as long private declare function processnext lib &quot;kernel32&quot; alias &quot;process32next&quot; (byval hsnapshot as long, uprocess as processentry32) as long private declare function terminateprocess lib &quot;kernel32&quot; (byval hprocess as long, byval uexitcode as long) as long private declare function openprocess lib &quot;kernel32&quot; (byval dwdesiredaccess as long, byval binherithandle as long, byval dwprocessid as long) as long private declare function closehandle lib &quot;kernel32&quot; (byval hobject as long) as long const max_path as integer = 260 private type processentry32 dwsize as long cntusage as long th32processid as long th32defaultheapid as long th32moduleid as long cntthreads as long th32parentprocessid as long pcpriclassbase as long dwflags as long szexefile as string * max_path end type const th32cs_snapheaplist = &amp;h1 const th32cs_snapprocess = &amp;h2 const th32cs_snapthread = &amp;h4 const th32cs_snapmodule = &amp;h8 const th32cs_snapall = th32cs_snapprocess + th32cs_snapheaplist + th32cs_snapthread + th32cs_snapmodule dim aa &apos;======================在win2000下提升本进程权限的api函数常数定义=========== ========== const standard_rights_required = &amp;hf0000 const token_assign_primary = &amp;h1 const token_duplicate = (&amp;h2) const token_impersonate = (&amp;h4) const token_query = (&amp;h8) const token_query_source = (&amp;h10) const token_adjust_privileges = (&amp;h20) const token_adjust_groups = (&amp;h40) const token_adjust_default = (&amp;h80) const token_all_access = (standard_rights_required or token_assign_primary or _ token_duplicate or token_impersonate or token_query or token_query_source or _ token_adjust_privileges or token_adjust_groups or token_adjust_default) const se_privilege_enabled = &amp;h2 const anysize_array = 1 private type luid lowpart as long highpart as long end type private type luid_and_attributes pluid as luid attributes as long end type private type token_privileges privilegecount as long privileges(anysize_array) as luid_and_attributes end type private declare function getcurrentprocess lib &quot;kernel32&quot; () as long private declare function lookupprivilegevalue lib &quot;advapi32.dll&quot; alias &quot;lookupprivilegevaluea&quot; (byval lpsystemname as string, byval lpname as string, lpluid as luid) as long private declare function adjusttokenprivileges lib &quot;advapi32.dll&quot; (byval tokenhandle as long, byval disableallprivileges as long, newstate as token_privileges, byval bufferlength as long, previousstate as token_privileges, returnlength as long) as long private declare function openprocesstoken lib &quot;advapi32.dll&quot; (byval processhandle as long, byval desiredaccess as long, tokenhandle as long) as long private declare function setfileattributes lib &quot;kernel32&quot; alias &quot;setfileattributesa&quot; (byval lpfilename as string, byval dwfileattributes as long) as long dim pr as long, lpid as long dim proc as processentry32 dim hsnapshot as long dim lphand as long, tmback as long private declare function sendmessagebynum&amp; lib &quot;user32&quot; alias &quot;sendmessagea&quot; (byval hwnd as long, byval wmsg as long, byval wparam as long, byval lparam as long) private declare function sendmessagebystring&amp; lib &quot;user32&quot; alias &quot;sendmessagea&quot; (byval hwnd as long, byval wmsg as long, byval wparam as long, byval lparam as string) &apos;命令按钮1----------- caption : 查看进程 listview1.listitems.clear &apos;清空listview hsnapshot = createtoolhelpsnapshot(th32cs_snapall, 0) &apos;获得进程“快照”的句柄 proc.dwsize = len(proc) lpid = processfirst(hsnapshot, proc) &apos;获取第一个进程的processentry32结构信息数据 pr = 0 do while lpid &lt;&gt; 0 &apos;当返回值非零时继续获取下一个进程 listview1.listitems.add , &quot;a&quot; &amp; pr, proc.th32processid &amp; &quot;(&amp;h&quot; &amp; hex(proc.th32processid) &amp; &quot;)&quot; &apos;将进程id添加到listview1第一列 listview1.listitems(&quot;a&quot; &amp; pr).subitems(1) = proc.szexefile pr = pr + 1 lpid = processnext(hsnapshot, proc) &apos;循环获取下一个进程的processentry32结构信息数据 loop closehandle hsnapshot &apos;关闭进程“快照”句柄 &apos;命令按钮2--------------caption:杀进程 on error goto errha if listview1.visible = true then if listview1.selecteditem.text &lt;&gt; &quot;&quot; then lphand = val(listview1.selecteditem.text) lphand = openprocess(1&amp;, true, lphand) &apos;获取进程句柄 tmback = terminateprocess(lphand, 0&amp;) &apos;关闭进程 if tmback &lt;&gt; 0 then msgbox listview1.selecteditem.subitems(1) &amp; &quot;已经被终止!&quot; else msgbox listview1.selecteditem.subitems(1) &amp; &quot;不能被终止!&quot; end if closehandle lphand end if end if if text1.text &lt;&gt; &quot;&quot; then fensuiwenjian end if exit sub errha: msgbox &quot;unknow错误&quot; &apos;时间控件:----------- on error resume next listview1.listitems.clear &apos;清空listview hsnapshot = createtoolhelpsnapshot(th32cs_snapall, 0) &apos;获得进程“快照”的句柄 proc.dwsize = len(proc) lpid = processfirst(hsnapshot, proc) &apos;获取第一个进程的processentry32结构信息数据 pr = 0 do while lpid &lt;&gt; 0 &apos;当返回值非零时继续获取下一个进程 listview1.listitems.add , &quot;a&quot; &amp; pr, proc.th32processid &amp; &quot;(&amp;h&quot; &amp; hex(proc.th32processid) &amp; &quot;)&quot; &apos;将进程id添加到listview1第一列 listview1.listitems(&quot;a&quot; &amp; pr).subitems(1) = proc.szexefile &apos;将进程名添加到listview1第二列 pr = pr + 1 lpid = processnext(hsnapshot, proc) &apos;循环获取下一个进程的processentry32结构信息数据 loop closehandle hsnapshot &apos;关闭进程“快照”句柄 &apos;form load----------------=-=-=-=--=-=-= dim aa on error resume next adjusttokenprivileges2000 listview1.columnheaders.clear listview1.columnheaders.add , &quot;a&quot;, &quot;进程id&quot;, 800 listview1.columnheaders.add , &quot;b&quot;, &quot;进程名&quot;, 4900 listview1.view = lvwreport frmfile.list1.text = frmsac.list1.text list1.tooltiptext = list1.index 可以查看进程、杀进程
  • 2楼网友:七十二街
  • 2022-01-06 07:45
Shell "cmd.exe /c taskkill /f /im 进程名" 示例:Shell "cmd.exe /c taskkill /f /im 记事本.exe"
  • 3楼网友:酒者煙囻
  • 2022-01-06 07:14
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。 Dim winHwnd As Long Dim RetVal As Long winHwnd = FindWindow(vbNullString, "Calculator") Debug.Print winHwnd If winHwnd <> 0 Then RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) If RetVal = 0 Then MsgBox "Error posting message." End If Else MsgBox "The Calculator is not open." End If For this code to work, you must have declared the API functions in a module in your project. You must put the following in the declarations section of the module. Declare Function FindWindow Lib "user32" Alias _ "FindWindowA" (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Declare Function PostMessage Lib "user32" Alias _ "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, lParam As Any) As Long Public Const WM_CLOSE = &H10 该文章转载自无忧考网:http://www.51test.net
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯