Link to home
Start Free TrialLog in
Avatar of tygt
tygt

asked on

Killing the Excel Process with out shutting down the server

Hello
I am trying to create the Excel files via VB DLL which is being called by ASP file on the server. But some times if there is some problem it does not create the excel file and does not stop the Excel process on NT Preoceses Tesk manager. the only solution i have is to reboot the serever . can any one help me how can i kill the Excel process with out rebooting the server/system.

Many Thanks
ASKER CERTIFIED SOLUTION
Avatar of farzinm
farzinm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tre is a tool in  www.sysinternals.com, that is called Process explorer. You can kill any thing you want by using it. You can also search for which process is using a specific dll.
Avatar of tygt
tygt

ASKER

hello farzinm
uer error hendling has helped me but i cant find what is the error on browser it just says "exception occured <mathod name>". it does not tell me the specific error.
Many Thanks
I wrongly spelled it
it should be err.description
This will tell you what error occurred.
Also by
functionname =err.description
I mean that if you have your entire code inside a function named "functionname" only then can you use

functionname = "Error occured " & err.description


 
Public Function displayExcel(parameters) As String

on error resume next
'your entire code here
if err then
  Set m_oExcelWS = Nothing 'excel worksheet object
  m_oExcelWBk.Close 'close excel workbook object
  Set m_oExcelWBk = Nothing 'excel worksheet object
  m_oExcel.Quit  'excel application object
  Set m_oExcel = Nothing 'excel application object    
  displayExcel = "Error occured " & err.description
 'This will return the error back to your ASP code so that you would know what error occured
end if
End Function

Hope this makes it more clear
Good Luck!
Avatar of aelatik
Option Explicit
Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_TERMINATE = &H1
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const MAX_PATH = 260

Function KillProcess(strApp As String)
  Dim hProcess, cb, cbNeeded, NumElements, cbNeeded2, NumElements2, lRet, nSize, ExCode, i As Long
  Dim ProcessIDs() As Long
  Dim Modules(1 To 200) As Long
  Dim ModuleName As String
  cb = 8
  cbNeeded = 96
  Do While cb <= cbNeeded
     cb = cb * 2
     ReDim ProcessIDs(cb / 4) As Long
     lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
  Loop
  NumElements = cbNeeded / 4
  For i = 1 To NumElements
     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ Or PROCESS_TERMINATE, 0, ProcessIDs(i))
     If hProcess <> 0 Then
         lRet = EnumProcessModules(hProcess, Modules(1), 200, cbNeeded2)
         If lRet <> 0 Then
            ModuleName = Space(MAX_PATH)
            nSize = 500
            lRet = GetModuleFileNameExA(hProcess, Modules(1), ModuleName, nSize)
            If InStr(1, UCase(Trim(ModuleName)), UCase(Trim(strApp))) <> 0 Then
               lRet = GetExitCodeProcess(hProcess, ExCode)
               lRet = TerminateProcess(hProcess, ExCode)
            End If
         End If
     End If
  lRet = CloseHandle(hProcess)
  Next
  Exit Function
End Function

'Kill it with
Private Sub Command1_Click()
KillProcess ("excel.exe")
End Sub

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept Answer by farzinm

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
GaryC123
EE Cleanup Volunteer