Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing an interop Application process

I have the following in a program (written in VB.NET):

Imports Microsoft.Office.Interop.Excel

Public Class Form1
    Dim eApp As New Excel.Application
    Dim w As Excel.Workbook
    w = eApp.Workbooks.Open( "path.xls", ReadOnly:=True)
    .. Processing Code ..
    //Attempts at killing the excel application
    w.Close()
    eApp.Workbooks.Close()
    eApp.Quit()
End Class

When I run this a couple of times, I get a bunch of EXCEL.EXE instances in my task manager. How can I kill these processes in code? All of the ones in the code have been tried and did not work.

like image 288
Anthony Potts Avatar asked Jan 23 '23 23:01

Anthony Potts


1 Answers

I had to do this a while back in NET 1.1, so please forgive the rust.

On the eApp, there was a Hwind (a win32 window handle - http://msdn.microsoft.com/en-us/library/bb255823.aspx ) or similar object. I had to use that and a pInvoke (http://www.pinvoke.net/default.aspx/user32.GetWindowThreadProcessId ) to get the process id. With that I was able to do a Process.Kill() on the exe.

There maybe a better way to do it now, but this should work.

like image 145
StingyJack Avatar answered Jan 31 '23 11:01

StingyJack