Posted November 16, 200717 yr 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).
November 16, 200717 yr 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, 200717 yr by oricode
November 16, 200717 yr 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, 200717 yr by GaBoR
November 16, 200717 yr 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, 200717 yr by ragdog
November 17, 200717 yr Author Thanks All of you for your kind suggestions. I have understood all the three styles to Kill another process. Thank you all again
Create an account or sign in to comment