Results 1 to 9 of 9

Thread: ending a game program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19

    ending a game program

    Hi guys! I'm working on an internet cafe timer app (client/server). However, I have one problem. I can't get the client code to terminate a game (for ex. counterstrike/battle realms/red alert, etc.) so I could lock the pc. I need a way to end the program(which almost always is a game program) that the user is currently using after his/her time is up. What i need is code to stop the program without me needing to know what program is being run by the user at the moment. How do I go about it? If you can provide code, I'd be very grateful. Your help is greatly appreciated. Thanks.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: ending a game program

    One of the first questions I have is; are the client computers owned by the internet cafe? Or are you just providing internet access to the people at the cafe (and they use their own laptops, ect)?

  3. #3
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: ending a game program

    I once used the myProcesses = Process.GetProcesses function and then ran

    VB Code:
    1. for each thisProcess in myProcesses
    2.     thisProcess.kill
    3. next for

    On my pc, I got the "MS Blue Screen of Death". My coworkers machine just rebooted.

    I guess what I'm getting at is you could find the exact process and try to just kill it. However, this is a .net solution. I don't know what you are programing in.
    Visual Studio .NET 2005/.NET Framework 2.0

  4. #4
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    413

    Re: ending a game program

    You could always use the shutdown command using the process.start method and shutdown using this command.


    To Shutdown and reboot:
    shutdown -r -f -t 00

    To just Shutdown
    shutdown -s -f -t 00
    Visual Studio .NET 2005/.NET Framework 2.0

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19

    Re: ending a game program

    Sevenhalo, the computers are owned by the internet cafe. Pennystock, i'm programming in vb6. and no, there's no need to do a reboot everytime. i just want to end the process/task/program that the user is currently using without me needing to know in particular what that program is. that is, i don't have to supply the name of the program to some sub or function that kills the current program. Most of the time however it is a game program since the internet cafe is also a gaming center. so how do i go about it guys?

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: ending a game program

    The way I would do it is, I would save all the processes(their pid or name) that should be running when the computer starts up, save it in a list, then when the timer says times up, get all the name(or pid) of the processes that are currently running, and any process that does not exist in the "list", kill that process.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19

    Re: ending a game program

    ok benmartin101. how do i get a list of all processes? what API should i use? Please provide sample code. thanks.

  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: ending a game program

    well, here's the code I made for a dll, which you can just use in vb, or you could translate it to vb, whichever you prefer.
    Code:
    #include "stdafx.h"
    #include "Tlhelp32.h"
    #include "stdlib.h"
    
    PROCESSENTRY32 *lpProc;
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    { 
        return TRUE;
    }
    
    
    int killProcess(DWORD pid)
    {
    	HANDLE procHnd;
    	DWORD *eCode;
    	eCode = new DWORD;
    	procHnd = ::OpenProcess(PROCESS_ALL_ACCESS, false, pid);
    	::GetExitCodeProcess(procHnd, eCode);
    	if(::TerminateProcess(procHnd, (unsigned int)*eCode))
    		return 1;
    	return 0;
    }
    
    
    
    __declspec(dllexport) int WINAPI TerminateProcByName(char * exe)
    {	
    	char * target = exe;
    	char * str;
    	str = new char[255];
    
    	lpProc = new PROCESSENTRY32;
    	lpProc->dwSize = sizeof(PROCESSENTRY32);
    
    	HANDLE snapSht = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    	int ret = ::Process32First(snapSht, lpProc);
    	while(true)
    	{
    		str = lpProc->szExeFile;
    		if (strcmp(str, target) == 0)
    		{
    			if(killProcess(lpProc->th32ProcessID))
    				return 1;
    		}
    		const long err = (const long)::GetLastError();
    		if (ERROR_NO_MORE_FILES == err)
    			return 0;
    		::Process32Next(snapSht, lpProc);
    	}
    
    	return 0;
    }
    
    __declspec(dllexport) int WINAPI TerminateProcByPid(DWORD pid)
    {
    	if(killProcess(pid))
    		return 1;
    	return 0;
    }
    
    HANDLE gsnapSht;
    PROCESSENTRY32 *glpProc;
    __declspec(dllexport) int WINAPI GetFirstProcNameId(int & name, DWORD & pid)
    {
    
    
    	glpProc = new PROCESSENTRY32;
    	glpProc->dwSize = sizeof(PROCESSENTRY32);
    
    	gsnapSht = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    	int ret = ::Process32First(gsnapSht, glpProc);
    
    	name = (int)glpProc->szExeFile;
    	pid = glpProc->th32ProcessID;
    	if (ret)
    		return 1;
    	return 0;
    }
    
    __declspec(dllexport) int WINAPI GetNextProcNameId(int & name, DWORD & pid)
    {
    	int ret = ::Process32Next(gsnapSht, glpProc);
    
    	const long err = (const long)::GetLastError();
    	if (ERROR_NO_MORE_FILES == err)
    		return 0;
    
    	name = (int)glpProc->szExeFile;
    	pid = glpProc->th32ProcessID;
    	if (ret)
    		return 1;
    	return 0;
    }
    If you decide to use it as a dll, you could just call GetFirstProcNameId() and GetNextProcNameId() to get all the process' name and id. Both functions return the pointer to the name(string). It also returns the pid. You would declare in vb something like:

    GetFirstProcNameId(byref name as long, byref pid as long)

    Then use TerminateProcByName or TerminateProcById() to kill the process.

  9. #9
    Junior Member
    Join Date
    May 2006
    Location
    My Computer
    Posts
    30

    Re: ending a game program

    Quote Originally Posted by PENNYSTOCK
    On my pc, I got the "MS Blue Screen of Death". My coworkers machine just rebooted.
    I think we've all made a mistake like than one once or twice. My favorite is from my *nix days.

    rm -rf Fo *

    Luckily I was usually able to hit CTRL-C fast enough
    --
    JaredP

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