Nedenstående kode har jeg ærligt og redeligt kopieret fra et sted på nettet.
Jeg har to spørgsmål som jeg søger svar på.
1: Hvordan kan det være at når laver en watch på "objWMIcimv2", som efter min mening burde indeholde alt info om alle kørende processor, så er der meget lidt at se, hvorimod "objProcess" og "objList" tilsyneladende indeholder langt flere data?
2: Hvordan kan jeg målrette koden til blot at terminere en bestemt instans af det program jeg vil lukke? (Jeg kender PID, idet jeg selv har startet instansen tidligere i koden)
<Code> Dim strTerminateThis As String 'The variable to hold the process to terminate Dim objWMIcimv2 As Object, objProcess As Object, objList As Object Dim intError As Integer
'Process to terminate - you could specify and .exe program name here strTerminateThis = "CALC.EXE"
Set objWMIcimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'") For Each objProcess In objList intError = objProcess.Terminate 'Terminates a process and all of its threads. 'Return value is 0 for success. Any other number is an error. If intError <> 0 Then Exit For Next
'ALL instances of exe (strTerminateThis) have been terminated Set objWMIcimv2 = Nothing Set objList = Nothing Set objProcess = Nothing </Code>
Jeg fandt aldrig ud af svaret til spg. 1. Selv om det, for mig stadig virker mærkeligt, har jeg ikke behov for at komme mysteriet nærmere pt.
Ang. spg. 2, så er den stump kode fra mit spg. ikke brugbar i forbindelse med at terminere en proces på baggrund af en kendt PID.
Jeg har til gengæld fundet en anden stump kode som kan netop det. Al kredit til den oprindelige kodeskribent: Leith Ross
Trigger til funktionen
<Code> CloseWorkbookByPID AppPID </Code>
Selve funktionen:
<Code> 'Written: April 15, 2011 'Author: Leith Ross
'Declare API constants used. Private Const GW_CHILD As Long = 5 Private Const GW_HWNDNEXT = 2 Private Const MAX_SIZE As Long = 260 Private Const WM_CLOSE As Long = &H10
'API calls Private Declare Function GetWindow _ Lib "user32.dll" _ (ByVal hWnd As Long, _ ByVal wCmd As Long) _ As Long
Private Declare Function GetDesktopWindow _ Lib "user32.dll" () As Long
Private Declare Function GetWindowThreadProcessId _ Lib "user32.dll" _ (ByVal hWnd As Long, _ ByRef lpdwProcessId As Long) _ As Long
Private Declare Function GetClassName _ Lib "user32.dll" Alias "GetClassNameA" _ (ByVal hWnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) _ As Long
Private Declare Function SendMessageA _ Lib "user32.dll" _ (ByVal hWnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lparam As Long) _ As Long
'VBA Macro Sub CloseWorkbookByPID(ByVal Process_Id As Long)
Dim ClassName As String Dim hWnd As Long Dim L As Long Dim N As Long Dim Pid As Long Dim RetVal As Long
'Start with the Top most window on the Desktop hWnd = GetWindow(GetDesktopWindow, GW_CHILD)
'Loop while the hWnd returned by GetWindow is valid. While hWnd <> 0 'Get the Window Class name ClassName = String(MAX_SIZE, Chr$(0)) L = GetClassName(hWnd, ClassName, MAX_SIZE) ClassName = IIf(L > 0, Left(ClassName, L), "")
'Is this window an Excel Application window? If ClassName = "XLMAIN" Then 'The Process Id is unique to each instance RetVal = GetWindowThreadProcessId(hWnd, Pid)
'Is this the instance to close? If Pid = Process_Id Then RetVal = SendMessageA(hWnd, WM_CLOSE, 0&, 0&) If RetVal <> 0 Then MsgBox "Workbook failed to Close." End If Exit Sub End If End If
'Get the next Window hWnd = GetWindow(hWnd, GW_HWNDNEXT)
'Process other events DoEvents Wend
End Sub </Code>
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.