/ Forside / Teknologi / Udvikling / VB/Basic / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
VB/Basic
#NavnPoint
berpox 2425
pete 1435
CADmageren 1251
gibson 1230
Phylock 887
gandalf 836
AntonV 790
strarup 750
Benjamin... 700
10  tom.kise 610
Lukke program via Task ID
Fra : Ukendt


Dato : 26-01-01 00:16

Hejsa

Kan man ikke på en nem måde lukke et program man har startet via:
TaskId = Shell("c:\bla bla bla")

På en anden måde end via appactivate og sendkeys?

mvh

David

Wormie(a)alberg.dk



 
 
David Mortensen (26-01-2001)
Kommentar
Fra : David Mortensen


Dato : 26-01-01 09:55

Hejsa

Kan man ikke på en nem måde lukke et program man har startet via:
TaskId = Shell("c:\bla bla bla")

På en anden måde end via appactivate og sendkeys?

mvh

David

Wormie(a)alberg.dk



Jesper Højgaard (26-01-2001)
Kommentar
Fra : Jesper Højgaard


Dato : 26-01-01 15:22

Du paster nedenstående kode ind i et modul og så bruger du KillProcess
TaskID til at lukke dit program.
Du kan også bruge IsProcessRunning til at checke om dit program kører eller
ej.

Denne kode er lavet til Windows NT / 2000, men jeg tror at det virker på Win
9x også, du skal måske fjerne alle kald til adjusttoken.

Mvh.
Jesper Højgaard
Deloitte & Touche

Option Explicit

Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal
ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As
Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess
As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal
TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As
TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As
TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As
Long, ByVal uExitCode As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias
"LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As
String, lpLuid As LUID) As Long
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const ANYSIZE_ARRAY = 1
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_DEBUG_NAME = "SeDebugPrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const PROCESS_TERMINATE = &H1

Private Type LUID
LowPart As Long
HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

Dim m_lProcessID As Long

Private Sub AdjustToken()
Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long

hdlProcessHandle = GetCurrentProcess()

OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY), hdlTokenHandle

LookupPrivilegeValue "", SE_DEBUG_NAME, tmpLuid
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
' Enable the kill privilege in the access token of this
' process.
AdjustTokenPrivileges hdlTokenHandle, False, tkp, _
Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
End Sub

Public Sub KillProcess(ProcessID As Long)
Dim hProcess As Long

AdjustToken
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID)
TerminateProcess hProcess, -1&
CloseHandle hProcess
End Sub

Public Function IsProcessRunning(ProcessID As Long) As Boolean
Dim hProcess As Long

AdjustToken
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID)
IsProcessRunning = (hProcess <> 0)
CloseHandle hProcess
End Function


David Mortensen <Wormie(a)alberg.dk> wrote in message
news:980457695.864965@angua.skjoldhoej.dk...
> Kan man ikke på en nem måde lukke et program man har startet via:
> TaskId = Shell("c:\bla bla bla")
> mvh
> David



Søg
Reklame
Statistik
Spørgsmål : 177551
Tips : 31968
Nyheder : 719565
Indlæg : 6408825
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste