首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

VB.net Kill() 彻底结束进程解决方案

2012-06-09 
VB.net Kill() 彻底结束进程Private Sub ProcessKill(ByVal myExcel As Excel.Application)Dim p As New S

VB.net Kill() 彻底结束进程
Private Sub ProcessKill(ByVal myExcel As Excel.Application)
  Dim p As New System.Diagnostics.Process
  Dim inst As Process
  Dim myProcess() As Process

  myProcess = System.Diagnostics.Process.GetProcessesByName("EXCEL")
  For Each inst In myProcess
  'If inst.Handle.ToInt32 = myExcel.Hinstance Then

  p = System.Diagnostics.Process.GetProcessById(inst.Id)
  p.Kill()
  'End If

  Next

我想关闭当前使用的进程 myExcel  
怎么判断它的位置呢?myExcel.Hwnd 与 inst.Handle.ToInt32 不相等。。。。。。。。。。。。

[解决办法]
Imports System.Runtime.InteropServices

Public Class Form1
Public Const WM_CLOSE = &H10

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim EXCELApp As Excel.Application

EXCELApp = New Excel.Application()
EXCELApp.Visible = True
'EXCELApp.Workbooks.Add()
EXCELApp.Workbooks.Open(System.Environment.CurrentDirectory & "\Book1.xls")


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim hwndPPt As Integer

hwndPPt = MyApi.FindWindow("XLMAIN", "Microsoft Excel - Book1.xls")

'MsgBox("hwndEXCEL (" & Hex(hwndPPt) & ") has the Window handle to " & _
' "EXCEL's Main Window." & vbCr & " Click OK to close EXCEL.")

'EXCELApp.Quit()
MyApi.SendMessageA(hwndPPt, WM_CLOSE, 0, 0)
End Sub
End Class
Public Class MyApi
<DllImport("user32.dll")> Public Shared Function _
FindWindow(ByVal strClassName As String, ByVal strWindowName _
As String) As Integer
End Function

<DllImport("user32.dll")> Public Shared Function _
SendMessageA(ByVal hWnd As Integer, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
End Function


End Class
要添加对EXCEL的引用

热点排行