TheCodeCracker Posted November 16, 2007 Posted November 16, 2007 hiI like to ask that how can i kill or close a process from my codesuppose 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).
oricode Posted November 16, 2007 Posted November 16, 2007 (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 November 16, 2007 by oricode
GaBoR Posted November 16, 2007 Posted November 16, 2007 (edited) You can use these functions:GetWindowThreadProcessId, OpenProcess, TerminateProcess.Look them up here for details:http://msdn2.microsoft.com/en-us/default.aspx Edited November 16, 2007 by GaBoR
ragdog Posted November 16, 2007 Posted November 16, 2007 (edited) hiuse this to kill a another app.dataszProcs db "notepad.exe",0KillProcess Proc _IdProcess:DWORDLOCAL uProcess:PROCESSENTRY32LOCAL 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 retKillProcess endpgreetsragdog Edited November 16, 2007 by ragdog
TheCodeCracker Posted November 17, 2007 Author Posted November 17, 2007 Thanks All of you for your kind suggestions. I have understood all the three styles to Kill another process. Thank you all again
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now