• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

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
0
tygt
Asked:
tygt
1 Solution
 
farzinmCommented:
you can use error handling in your DLL code

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    
   function name = "Error occured " & err.decription
  'This will return the error back to your ASP code so that you would know what error occured
end if
0
 
iozturkCommented:
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.
0
 
tygtAuthor Commented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
farzinmCommented:
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


 
0
 
farzinmCommented:
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!
0
 
aelatikCommented:
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

0
 
GaryCommented:
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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now