locked
Killing Excel works on XP but not in 2003 Server RRS feed

  • Question

  • User-1349019444 posted

    Hi!

    I was working with interop to create and manage some excel files, but as you know, sometimes Excel doesnt close, so you have to write some code to kill process like folowing:

     

    Declare Function EndTask Lib "user32.dll" (ByVal hWnd As IntPtr) As Integer
        Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
               (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        Declare Function GetWindowThreadProcessId Lib "user32.dll" _
               (ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
        Declare Function SetLastError Lib "kernel32.dll" (ByVal dwErrCode As Integer) As IntPtr
     
        Public Sub KillProcess(ByVal MainWindowHandle As IntPtr, ByVal Caption As String)
            SetLastError(0)
            ' for Excel versions <10, this won't be set yet
            If IntPtr.Equals(MainWindowHandle, IntPtr.Zero) Then _
          MainWindowHandle = FindWindow(Nothing, Caption)
            If IntPtr.Equals(MainWindowHandle, IntPtr.Zero) Then _
          Exit Sub 
            Dim iRes, iProcID As Integer
            iRes = GetWindowThreadProcessId(MainWindowHandle, iProcID)
            If iProcID = 0 Then 
                If EndTask(MainWindowHandle) <> 0 Then Exit Sub 
                Throw New ApplicationException("Failed to close.")
            End If
            Dim proc As System.Diagnostics.Process
            proc = System.Diagnostics.Process.GetProcessById(iProcID)
            proc.CloseMainWindow()
            proc.Refresh()
            If proc.HasExited Then Exit Sub
            proc.Kill()
        End Sub 
    
    
     

    This solution works fine on Windows XP, but when i publish solution on windows 2003 server, it doesnt work.

    Do you know if i have to do some configurations on W2K3 Server?

    This code has been extracted from, http://www.devcity.net/Articles/163/1/Articles.aspx?a=ViewArticle&ArticleID=163&Page=1#FeedbackPanel

     Thanks

     

     

    Friday, March 27, 2009 4:57 PM

Answers

  • User-319574463 posted

     >Do you know if i have to do some configurations on W2K3 Server?

    Whilst I have not used Excel on Server 2003, I have used Word of 2003 in the same way. So here are a few pointers.

    • Log on to the 2003 Server and run excel interactively. (You may need to change the service account to allow interactive logon and use that account as well at least once. Afterwards interactive logon can be disabled.
    • After an error occurs you may need to log on interactively again.
    • Look very carefully at the messages in the Event logs as you may need to use the ComponentServices MMC to set permissions on the components of Excel.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 31, 2009 8:56 AM

All replies

  • User1052258516 posted

    Excel is notorious for not closing down properly, you would be best to find a different solution to create your excel files by either using a 3rd party component or use something like this.

    http://www.codedigest.com/Articles/ASPNET/13_Export_dataset_to_Excel_with_XSLT_in_AspNet_20.aspx

    The output is a larger file (because it's made up of Xml) but at least you don't have to worry about your server running out or resources when there are possible thousands of EXCEL.EXE processes sitting doing nothing.

    Saturday, March 28, 2009 12:39 PM
  • User-1349019444 posted

    Problem is that i dont want to export. I want to modify an Excel updated file.

    It works fine on my development machine with Windows XP, but in 2k3 server doesnt.

    Saturday, March 28, 2009 1:19 PM
  • User-319574463 posted

     >Do you know if i have to do some configurations on W2K3 Server?

    Whilst I have not used Excel on Server 2003, I have used Word of 2003 in the same way. So here are a few pointers.

    • Log on to the 2003 Server and run excel interactively. (You may need to change the service account to allow interactive logon and use that account as well at least once. Afterwards interactive logon can be disabled.
    • After an error occurs you may need to log on interactively again.
    • Look very carefully at the messages in the Event logs as you may need to use the ComponentServices MMC to set permissions on the components of Excel.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 31, 2009 8:56 AM