Jump to content
Tuts 4 You

How To Kill A Process From Your Application


TheCodeCracker

Recommended Posts

TheCodeCracker
Posted

hi

I like to ask that how can i kill or close a process from my code

suppose a process is already running in the memory e.g Notepad opened or some game is running than how can we close it from our code.

(languages can be any :masm, c++, vb).

Posted (edited)

Hi,I will explain this using the example of "Notepad". Suppose you have an instance of Notepad running, with the title "untitled - Notepad". To kill this from your application , you need to do the following :-

Declare the functions from user32.dll

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")]
public static extern int FindWindow(
string lpClassName, // class name
string lpWindowName // window name
);
[DllImport("user32.dll")]
public static extern int SendMessage(
int hWnd, // handle to destination window
uint Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);

Then use the following to close the procces

		
string myClass = "notepad"; string myTitle = "untitled - Notepad";
int HND = FindWindow(myclass, myTitle); //returns the handle of the window.
if (HND != 0 )
{
int retn = SendMessage(HND, Win32.WM_SYSCOMMAND, Win32.SC_CLOSE, 0); //Sends the message to that process to close.
}

The above is in C#, hope you can do this in VB(almost the same).

Oricode.

Edited by oricode
Posted (edited)

hi

use this to kill a another app

.data
szProcs db "notepad.exe",0
KillProcess Proc _IdProcess:DWORD
LOCAL uProcess:PROCESSENTRY32
LOCAL hSnapshot:DWORD
mov uProcess.dwSize, sizeof uProcess
invoke CreateToolhelp32Snapshot, TH32CS_SNAPPROCESS, 0
mov hSnapshot, eax
invoke Process32First, hSnapshot,addr uProcess
.while eax
xor ecx, ecx
lea esi, [uProcess.szExeFile]
mov ebx, esi
dec ebx
invoke lstrlen, esi
add esi, eax
.while esi!=ebx
invoke lstrcmpi, esi, _IdProcess
.if !eax
invoke OpenProcess, PROCESS_TERMINATE, 1, uProcess.th32ProcessID
invoke TerminateProcess, eax, 0
jmp done
.endif
; optimize this
dec esi
.endw
invoke Process32Next, hSnapshot, ADDR uProcess
.endw
done:
invoke CloseHandle, hSnapshot
ret
KillProcess endp

greets

ragdog

Edited by ragdog
TheCodeCracker
Posted

Thanks All of you for your kind suggestions. I have understood all the three styles to Kill another process.

Thank you all again :flowers::flowers::flowers::flowers::flowers:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...