Results 1 to 12 of 12

Thread: Forcing a Game program to terminate

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Philippines
    Posts
    85

    Unhappy Forcing a Game program to terminate

    Hi to all,

    I'm looking for a source code on how to force a game program like DOOM or COUNTER STRIKE which run in a full dos mode (without a window caption) unlike some dos program which has,
    the reason i'm doing this is because i've wanted to limit the time spent by my child in playing those games. (a program runs in the background that monitors these games) .

    Please help ASAP.

  2. #2
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Closing a game

    I also have this problem and still working on it. Temporarily, I use the ExitWindowsEX API and use uFlags = 0 to logoff the user because this API will close all programs running so there's not much use of your VB code after calling this API.

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Do you have the handle of the window?

  4. #4
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Game Handle

    yes, the game handle can be retrieved!!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    Philippines
    Posts
    85
    How do u do it?

  6. #6
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Game Handle

    From the processID of the shell, get the desktop handle and trace the child that matches the processID you got from the shell therefore, the handle of that child is the handle of your game or application for that matter.

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530

    At last some code

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
    4. Private Const SC_CLOSE = &HF060&
    5. Private Const WM_SYSCOMMAND = &H112
    6.  
    7.  
    8. Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
    9. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    10. Const PROCESS_ALL_ACCESS = &H1F0FFF
    11.  
    12. Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
    13. Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    14. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    15.  
    16.  
    17. Sub CloseApp(ByVal hwnd&)
    18.     Dim lPid                    As Long
    19.     Dim lHp                     As Long
    20.  
    21.     SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
    22.     '
    23.     ' If the window doesn't like gentle persuasion, bring out the nipple clamps to force it to close
    24.     '
    25.     If IsWindow(hwnd) Then
    26.         Call GetWindowThreadProcessId(hwnd, lPid)
    27.         lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
    28.         TerminateProcess lHp&, 0&
    29.         CloseHandle lHp
    30.     End If
    31.  
    32.  
    33. End Sub

  8. #8
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Closing a game

    Will try it out, I've been trying the CloseHandle for 3 days already, and the game is just too stubborn not to quit. May I try out your code? Thanks in advance for the help and will let you know in awhile!

  9. #9
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    tried Nucleus code

    The game kept on running. I tried also ExitProcess and my VB application ended instead. What am I doing wrong?

  10. #10
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    I don't have any games on this pc, but I started the dos emulator and was able to close the window with the following code. Are you sure you have the correct hwnd?

    VB Code:
    1. Option Explicit
    2. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Private Sub Command1_Click()
    4. Dim lhwnd&
    5.  
    6. lhwnd = FindWindow(vbNullString, "Command Prompt")
    7. CloseApp (lhwnd)
    8. End Sub

  11. #11
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    tried Nucleus code

    I used the following code that I got from vbnet to get the handle:

    Option Explicit
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' Copyright ©1996-2001 VBnet, Randy Birch, All Rights Reserved.
    ' Some pages may also contain other copyrights by the author.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' You are free to use this code within your own applications,
    ' but you are expressly forbidden from selling or otherwise
    ' distributing this source code without prior written consent.
    ' This includes both posting free demo projects made from this
    ' code as well as reproducing the code in text or html format.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Public Const GW_HWNDFIRST = 0
    Public Const GW_HWNDNEXT = 2
    Public Const GW_CHILD = 5

    Public Declare Function GetWindow Lib "user32" _
    (ByVal hwnd As Long, _
    ByVal wCmd As Long) As Long

    Public Declare Function GetDesktopWindow Lib "user32" () As Long

    Public Declare Function GetWindowThreadProcessId Lib "user32" _
    (ByVal hwnd As Long, _
    lpdwProcessId As Long) As Long

    Public Declare Function BringWindowToTop Lib "user32" _
    (ByVal hwnd As Long) As Long

    Public Declare Function SetWindowText Lib "user32" _
    Alias "SetWindowTextA" _
    (ByVal hwnd As Long, ByVal _
    lpString As String) As Long

    Public Declare Function GetClassName Lib "user32" _
    Alias "GetClassNameA" _
    (ByVal hwnd As Long, _
    ByVal lpClassName As String, _
    ByVal nMaxCount As Long) As Long

    Public Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

    Public Const WM_SETTEXT = &HC
    '--end block--'

    Option Explicit

    Private Sub Command1_Click()

    Dim hWndApp As Long

    'call the wrapper function to start shell,
    'and return the handle to the shelled app.
    hWndApp = StartShell("notepad.exe")

    'if found, prove it!
    If hWndApp Then

    'change its caption
    Call SetWindowText(hWndApp, "The handle to Notepad is " & CStr(hWndApp))

    'bring it to the top
    Call BringWindowToTop(hWndApp)

    'and add some text to its edit window
    Call PutTextIntoNotepad(hWndApp)

    End If

    End Sub


    Public Function GethWndFromProcessID(hProcessIDToFind As Long) As Long

    Dim hWndDesktop As Long
    Dim hWndChild As Long
    Dim hWndChildProcessID As Long

    On Local Error GoTo GethWndFromProcessID_Error

    'get the handle to the desktop
    hWndDesktop = GetDesktopWindow()

    'get the first child under the desktop
    hWndChild = GetWindow(hWndDesktop, GW_CHILD)

    'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0

    'get the ThreadProcessID of the window
    Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

    'if it matches the target, exit returning that value
    If hWndChildProcessID = hProcessIDToFind Then
    GethWndFromProcessID = hWndChild
    Exit Do
    End If

    'not found, so get the next hwnd
    hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

    Loop

    Exit Function

    GethWndFromProcessID_Error:
    GethWndFromProcessID = 0
    Exit Function

    End Function


    Private Function StartShell(sApplication As String) As Long

    Dim hProcessID As Long

    'start using shell, and get the process ID
    hProcessID = Shell(sApplication, vbNormalFocus)

    'return the hwnd to the shelled process
    StartShell = GethWndFromProcessID(hProcessID)

    End Function


    Private Sub PutTextIntoNotepad(hWndApp As Long)

    'This adds text to notepad, by locating its
    ''Edit' class window. This is similar to the
    'method used to locate the hwnd itself, but
    'here we know the parent (hWndApp) so only
    'have to search its child windows.

    Dim hWndChild As Long
    Dim sMsg As String
    Dim sBuffer As String * 32
    Dim nSize As Long

    'this string is split only to fit the browser window
    sMsg = "This method demonstrates using Shell() "
    sMsg = sMsg & "to launch notepad, obtain its hwnd using "
    sMsg = sMsg & "GetWindowThreadProcessId, and then use that"
    sMsg = sMsg & "hwnd to change its caption and place text into "
    sMsg = sMsg & "its Edit window using SetWindowText and SendMessage."

    'get the first child window in Notepad
    hWndChild = GetWindow(hWndApp, GW_CHILD)

    'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0

    'get the Class Name of the window
    nSize = GetClassName(hWndChild, sBuffer, 32)

    'if nSize > 0, it contains the length
    'of the class name retrieved
    If nSize Then

    'if the class name is "Edit",
    'set some text and exit
    If Left$(sBuffer, nSize) = "Edit" Then

    Call SendMessage(hWndChild, WM_SETTEXT, 0&, ByVal sMsg)
    Exit Sub

    End If

    End If

    'not found, so get the next hwnd
    hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

    Loop

    End Sub
    '--end block--'

  12. #12
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Try Matthew Gates' code

    I tried Gates' code on a game and it works. You can see his code on page 4 of this forum under the subject "shutting down an exe application", but it doesn't work on an NT or 2K.

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