Någon som kan berätta hur man går tillväga för att: E jävligt osäker, men IDn skall väl kunna returneras av Shell Det finns ett tips som du kanske kan använda dig av? Se: [Visa/avsluta aktiva processer i Windows95] Klipper in lite kod från en app jag har. Det är en app som vid start kontrollerar om en ActiveX-EXE är igång, om den inte är det så startas den. Det kanske åker med nån deklaration för mycket, mem...Starta en applikation & sedan döda dess process ?
1. Trycka på en knapp som startar en extern applikation.
2. Ta reda på dess processID
3. Med en annan knapp döda processen.
Dessutom så vill jag hela tiden ha koll på om programmet fortfarande
körs eller inte...
Någon???Sv: Starta en applikation & sedan döda dess process ?
Dim ProcessID As Long
ProcessID = Shell("Ditt Program.exe")Sv: Starta en applikation & sedan döda dess process ?
Sv: Starta en applikation & sedan döda dess process ?
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Integer = 260
Public Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Type LUID
lowpart As Long
highpart As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End Type
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
Const PROCESS_ALL_ACCESS = &H1F0FFF
Sub LämpligSub
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim r As Long
Dim lPid As Long
lPid = 0
hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = 0 Then
Exit Sub
End If
uProcess.dwSize = Len(uProcess)
r = ProcessFirst(hSnapShot, uProcess)
' Loopa igenom processerna
Do While r
r = ProcessNext(hSnapShot, uProcess)
If Len(uProcess.szExeFile) > 10 Then
' SRQDL6R.EXE är min ActiveX-exe
If UCase$(Mid(uProcess.szExeFile, 1, 11)) = "SRQDL6R.EXE" Then
' Spara undan processid
lPid = uProcess.th32ProcessID
End If
End If
Loop
Call CloseHandle(hSnapShot)
' Om pid=0 så var den ej igång, då startar vi den
If lPid = 0 Then
Shell App.Path & "\SRqDL6R.exe"
End If
' Så, i demosyfte, dödar vi procen igen. (Vi antar att lPid <> 0, egentligen
' måste vi ju, om vi på raden ovan, startat processen, loopa igenom och leta
' upp pidden igen.
KillProcess lPid
End Sub
Function KillProcess(ByVal hProcessID As Long, Optional ByVal ExitCode As Long) _
As Boolean
Dim hToken As Long
Dim hProcess As Long
Dim tp As TOKEN_PRIVILEGES
' Windows NT/2000 require a special treatment
' to ensure that the calling process has the
' privileges to shut down the system
' under NT the high-order bit (that is, the sign bit)
' of the value retured by GetVersion is cleared
If GetVersion() >= 0 Then
' open the tokens for the current process
' exit if any error
If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY, hToken) = 0 Then
GoTo CleanUp
End If
' retrieves the locally unique identifier (LUID) used
' to locally represent the specified privilege name
' (first argument = "" means the local system)
' Exit if any error
If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
GoTo CleanUp
End If
' complete the TOKEN_PRIVILEGES structure with the # of
' privileges and the desired attribute
tp.PrivilegeCount = 1
tp.Attributes = SE_PRIVILEGE_ENABLED
' try to acquire debug privilege for this process
' exit if error
If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, _
ByVal 0&) = 0 Then
GoTo CleanUp
End If
End If
' now we can finally open the other process
' while having complete access on its attributes
' exit if any error
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
If hProcess Then
' call was successful, so we can kill the application
' set return value for this function
KillProcess = (TerminateProcess(hProcess, ExitCode) <> 0)
' close the process handle
CloseHandle hProcess
End If
If GetVersion() >= 0 Then
' under NT restore original privileges
tp.Attributes = 0
AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
CleanUp:
If hToken Then CloseHandle hToken
End If
End Function
/S