Kill EXCEL.EXE process generated by OWC11

V

v.mazzotta

I need to kill EXCEL.EXE generated by OWC11 to delete files saved fo
export on spreadsheet and chart, owns by ASPNET.
I test this solution in same C# page where i generate xls and gif file
but don't go :

System.Runtime.InteropServices.Marshal.ReleaseComObject(Range);
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oSpreadsheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objChart);
System.Runtime.InteropServices.Marshal.ReleaseComObject(objCSpace);
objChart = null;
objCSpace = null;
Range = null;
sheet = null;
oSpreadsheet = null;
GC.Collect();
System.Diagnostics.Process[] myProcesses;
myProcesses = System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach (System.Diagnostics.Process instance in myProcesses)
{
instance.CloseMainWindow();
}


If I manually kill by task manager EXCEL.EXE process I can delete xl
and gif files
 
A

Alvin Bruney - MVP ASP.NET

If I remember correctly, the automation invokes an instance of the excel
object (Excel.exe) to perform the tasks. releasing the object does not kill
the instance. You may have to write hack code to kill the exe using windows
api or whatever works

--
Regards,
Alvin Bruney

Shameless Author Plug
[The Microsoft Office Web Components Black Book with .NET]
www.lulu.com/owc, Amazon, Barnes & Noble etc
Forth-coming VSTO.NET
 
V

v.mazzotta

Hi Alvey Bruney, i try to write this function to kill excel process:

//CREATE NEW CLASS KILLER WIN32
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, // clas
name
string lpWindowName // window name
);
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
}



In my c# code call follow:

int iHandle=Win32.FindWindow("XLMain","EXCEL.EXE");
// Post a message to Application to end its existence.
int j=Win32.SendMessage(iHandle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE
0);

Problems founded:
Visual studio don't recognize [DllImport("user32.dll")] command.

Can help me with a sample.

Thanks
 
V

v.mazzotta

Solved part of problem,
[DllImport("user32.dll")] recognize adding follow using:
using System;
using System.Runtime.InteropServices;

C# execute without problem follow line :
int iHandle=Win32.FindWindow("XLMain", null);

Second parameter is null because i don't have a window caption t
identify application Excel.

but when execute follow line, application Visual C# NET is blocked bu
if i kill by task manager EXCEL.EXE process, Visual C# NET continue t
go (Debug mode) :
// Post a message to Application to end its existence.
int j=Win32.SendMessage(iHandle, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE
0);

Somebody can help me ?
Thank
 
V

v.mazzotta

Finally solution:
Create an application class that accept max 30 call opened to creat
excel.
The Class get excel.exe process, create an excel file, get ne
excel.exe process and find excel.exe linked to call spreadsheet export
kill correctly excel.exe process.

code to export excel, find process and kill excel.exe linked t
request:

// Get all excel.exe processes
Process [] processes = Process.GetProcessesByName("EXCEL");

// Create export xls file server-side
// XLSFilename string path file to create passed by val.
oSpreadsheet.Export(XLSFilename
OWC11.SheetExportActionEnum.ssExportActionOpenInExcel
OWC11.SheetExportFormat.ssExportAsAppropriate);

Process [] processesNew = Process.GetProcessesByName("EXCEL");
// comparing pids into array processesNew and processes find rigt
excel linked to request
....

// Kill correctly EXCEL.EXE process
Win32.TerminateProcess(processesNew.Handle, 0);


public class Win32
{
[DllImport("kernel32.dll")] public static extern in
TerminateProcess(IntPtr handle, uint exitCode);
[DllImport("kernel32.dll")] public static extern lon
OpenProcess(long AccessType, long InheritHandle, long ProcId);
}

I find an exception on EXCEL.EXE process created by OWC :
ExitCode <error: an exception of type
{System.InvalidOperationException} occurred>

It's like if OWC not close correctly EXCEL.EXE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top