Results 1 to 16 of 16

Thread: Remove something from memory?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Remove something from memory?

    After I quit a program (Diablo2), it doesn't completely remove it from memory, which causes my computer to not shutdown properly. When I run Spy++, I can see that it's still in memory, but when I press ctrl+alt+del, it's not in the task list. Question is: How can I wipe it from memory?
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    Use Process Viewer.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Well, I tried process viewer, and that didn't kill it completely either. I'd click the process (Game.exe) and it'd ask if I wanted to kill it and I'd click yes... it refreshed with the process still running. Any other ideas?
    Last edited by Oafo; Aug 14th, 2002 at 06:44 PM.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Cmon Megatron, you're supposed to be the good one here
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  5. #5
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    Sorry, my computer crashed a couple days ago, and I had to re-install everything

    Have you tried using the TerminateProcess API?

    I'll see if I can find an example for you.

  6. #6
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    OK, here's an example.
    VB Code:
    1. Private Declare Function ProcessFirst Lib "kernel32" _
    2. Alias "Process32First" (ByVal hSnapshot As Long, uProcess _
    3. As PROCESSENTRY32) As Long
    4.  
    5. Private Declare Function ProcessNext Lib "kernel32" _
    6. Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    7. PROCESSENTRY32) As Long
    8.  
    9. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    10. Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    11. lProcessID As Long) As Long
    12.  
    13. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
    14. As Long) As Long
    15.  
    16. Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    17. dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    18. ByVal dwProcessId As Long) As Long
    19.  
    20. Private Declare Function TerminateProcess Lib "kernel32" _
    21. (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    22.  
    23.  
    24.  
    25. Type PROCESSENTRY32
    26.     dwSize As Long
    27.     cntUsage As Long
    28.     th32ProcessID As Long
    29.     th32DefaultHeapID As Long
    30.     th32ModuleID As Long
    31.     cntThreads As Long
    32.     th32ParentProcessID As Long
    33.     pcPriClassBase As Long
    34.     dwFlags As Long
    35.     szexeFile As String * MAX_PATH
    36.     End Type
    37.  
    38.  
    39. Public Function KillApp(myName As String) As Boolean
    40.     Const PROCESS_ALL_ACCESS = 0
    41.     Dim uProcess As PROCESSENTRY32
    42.     Dim rProcessFound As Long
    43.     Dim hSnapshot As Long
    44.     Dim szExename As String
    45.     Dim exitCode As Long
    46.     Dim myProcess As Long
    47.     Dim AppKill As Boolean
    48.     Dim appCount As Integer
    49.     Dim i As Integer
    50.     On Local Error GoTo Finish
    51.     appCount = 0
    52.    
    53.     Const TH32CS_SNAPPROCESS As Long = 2&
    54.    
    55.     uProcess.dwSize = Len(uProcess)
    56.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    57.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    58.    
    59.     Do While rProcessFound
    60.         i = InStr(1, uProcess.szexeFile, Chr(0))
    61.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    62.         If Right$(szExename, Len(myName)) = LCase$(myName) Then
    63.             KillApp = True
    64.             appCount = appCount + 1
    65.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    66.             AppKill = TerminateProcess(myProcess, exitCode)
    67.             Call CloseHandle(myProcess)
    68.         End If
    69.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    70.     Loop
    71.  
    72.     Call CloseHandle(hSnapshot)
    73. Finish:
    74. End Function
    75.  
    76.  
    77. 'Usage
    78.  
    79.  
    80. Call KillApp("C:\Program.exe")

  7. #7
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    If you set every object variable to Nothing when it is no longer needed..(like database connections, recordsets, or any object)
    If you unload each form when your application is closed.. Then
    The program will be clean sweaped automatically from memory. right?

  8. #8
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    But he's dealing with an external process here

  9. #9
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    Try rewriting it's entire process memory first. Make it all zero, the either it will cease functioning and exit, generate a huge error and exit, or be in an exposed position for you to terminate it.

  10. #10
    PowerPoster
    Join Date
    Aug 2000
    Location
    India
    Posts
    2,288
    Originally posted by snakeeyes1000
    Try rewriting it's entire process memory first. Make it all zero, the either it will cease functioning and exit, generate a huge error and exit, or be in an exposed position for you to terminate it.
    I think this mehtod will shut down your application rather than the target application by causing an illegal operation error. I don't think you an write to another process's memory.

  11. #11
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    yes you can. Open the process with the WriteProcessMemory flag and use the WriteProcessMemory API. At least I think... been a while since I used it.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    I think that would require you having to go through each and every memory address and set it to zero, which could take a while depending on what program it is. Afterall, it IS VB ya know
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  13. #13
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    Did my code with TerminateProcess work?

  14. #14
    Hyperactive Member
    Join Date
    Jun 2002
    Posts
    299
    Oafo:not at all,please look at and test WriteProcessMemory with OpenProcess before you say it won't work. but there should be an easier way to do it.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Megatron: I still haven't tested it but I'll let ya know.

    snakeeyes: I didn't say it wouldn't work, I just said it would be slow.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  16. #16
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    I just the following in MSDN
    Terminating a process does not necessarily remove the process object from the system. A process object is deleted when the last handle to the process is closed.
    So basically, if your process has any child processes, you might have to do a little extra work to get rid of it.

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