CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2004
    Posts
    249

    Question How to use Kill statement

    can anyone tell me how to use the syntax 'Kill' in visual basic.... suppose i m dealing with a string
    Last edited by Cimperiali; September 28th, 2004 at 03:55 AM. Reason: Moderator note: "Urgent Need" is not a meaningfull title - when asking a question, use the title to give us a clue of what you're looking for

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Urgent Need

    Hi best would be to use the Dir$ Function as well to see if the file exists, then the Kill to remove the file, then RmDir to remove the Directory

    example
    [code]
    If Dir$("C:\WINDOWS\Start Menu\Programs\Vh_1\Vh_1.lnk", vbNormal) <> "" Then
    Kill "C:\WINDOWS\Start Menu\Programs\Vh_1\Vh_1.lnk"
    RmDir "C:\WINDOWS\Start Menu\Programs\VH_1"

    Hannes

  3. #3
    Join Date
    May 2004
    Posts
    249

    Re: Urgent Need

    but i would like to kill the strings entered but the user.... do i have to use any sort of file streaming. i want to make a program that will kill the process that has been entered but the user. n not directories

  4. #4
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Urgent Need

    The Kill function in VB is used to delete a file, not to terminate a process.
    To terminate a process, you will need to make use of API functions. Here's an example:

    Code:
    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 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 Sub Form_Load()
        Dim intCnt As Integer
        intCnt = KillProcByName("\calc.exe")
        Call MsgBox("Program was terminated " & intCnt & " time(s).")
    End Sub
    Private Function KillProcByName(ByVal strProcName As String) As Integer
        Dim lngRet As Long
        Dim lngCb As Long, lngCbNeeded As Long, lngCbNeeded2 As Long
        Dim intLoop As Integer
        Dim lngProc As Long, lngProcIDs() As Long
        Dim lngModules(0) As Long, strModule As String
        lngCb = 8
        lngCbNeeded = 96
        Do While lngCb <= lngCbNeeded
            lngCb = lngCb * 2
            ReDim lngProcIDs(lngCb / 4) As Long
            lngRet = EnumProcesses(lngProcIDs(0), lngCb, lngCbNeeded)
        Loop
        For intLoop = 1 To (lngCbNeeded / 4)
            lngProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ Or PROCESS_TERMINATE, 0, lngProcIDs(intLoop))
            If lngProc <> 0 Then
                lngRet = EnumProcessModules(lngProc, lngModules(0), 1, lngCbNeeded2)
                If lngRet <> 0 Then
                    strModule = Space(256)
                    Call GetModuleFileNameExA(lngProc, lngModules(0), strModule, Len(strModule))
                    strModule = Left(strModule, InStr(1, strModule, vbNullChar) - 1)
                    If StrComp(Right(strModule, Len(strProcName)), strProcName, vbTextCompare) = 0 Then
                        Call TerminateProcess(lngProc, 0)
                        KillProcByName = KillProcByName + 1
                    End If
                End If
            End If
            Call CloseHandle(lngProc)
        Next intLoop
    End Function
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  5. #5
    Join Date
    May 2004
    Posts
    249

    Re: Urgent Need

    i wanted this program to kill a process that the user wants to kill... i.e if the process name has been entered, then it should kill the process whenever it is run. it should be kinda in a loop

  6. #6
    Join Date
    May 2004
    Posts
    249

    Re: Urgent Need

    i ment a process killing something like the one i hvae attached....i wanted to make something like that.... so can anyone of u help me out on that please
    Attached Files Attached Files

  7. #7
    Join Date
    May 2004
    Posts
    249

    Re: Urgent Need

    so can anyone help me with the code of the attached application i have given........i need that urgently so please someone help me out with the code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured