CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2010
    Posts
    172

    Arrow Process List and Termination Win32 app

    Hello could you please help me


    Code:
    		case ID_TER:
    			TerminateProcess(hProcess,0);
    
    			break;
    		case ID_COM:
    			PROCESSENTRY32 entry;
    
        entry.dwFlags = sizeof( PROCESSENTRY32 ); 
    
     
     snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL ); 
    	
     
        if ( Process32First( snap, &entry ) == TRUE ) { 
           while ( Process32Next( snap, &entry ) == TRUE ){
             
    			ComboBox_InsertString(combox,0,&entry.szExeFile);
    			ComboBox_GetText(combox,buffer,10000);
    			strcpy(buff,buffer);
    			if(ComboBox_FindItemData(combox,0,buff)==0){
    			
    
    
    
    			
    			hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,entry.th32ProcessID);
    	
    			}
    
    		
    			
    			
    		
    	  
    	 
    	   }
        CloseHandle( snap );
    	  
    	   }
    What i have here is that i place the process list in a combobox then when i select the process and click terminate it does not work. But if i put strcpy(buff,"Process.exe"); and find that process in the combobox then click terminate it closes but what i want it to do is to let user choose any process and terminate it.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Process List and Termination Win32 app

    Did you debug your code?
    What is the value of hProcess in
    Code:
    TerminateProcess(hProcess,0);
    call?
    What does TerminateProcess return?
    If it is zero - what does GetLastError return?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Process List and Termination Win32 app

    Actually I miss the code before 'case ID_TER:'.

    Where did you get the hProcess from?

    Did you store the process handle to each entry of the combobox before by SetItemData (or ComboBox_SetItemData)?

    Can you post the full code of the function (with correct indentation please: select your code and do ALT+F8) ?

    Regards, Alex

  4. #4
    Join Date
    Apr 2010
    Posts
    172

    Re: Process List and Termination Win32 app

    This is the full code

    Code:
    #include "stdafx.h"
    #include "Process.h"
    
    #define MAX_LOADSTRING 100
    // Global Variables:
    HINSTANCE hInst;								// current instance
    TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
    HWND ter;
    HWND combox;
    HANDLE snap;
    HANDLE hProcess;
    char buff[10000];
    char buffer[10000];
     
    // Forward declarations of functions included in this code module:
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    BOOL				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
     	// TODO: Place code here.
    	MSG msg;
    	HACCEL hAccelTable;
    
    	// Initialize global strings
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_PROCESS, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (hInstance, nCmdShow))
    	{
    		return FALSE;
    	}
    
    	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROCESS));
    
    	// Main message loop:
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    
    	return (int) msg.wParam;
    }
    
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage are only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInstance;
    	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PROCESS));
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_PROCESS);
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
    	return RegisterClassEx(&wcex);
    }
    
    //
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
     ter =  CreateWindow("button", "Terminate",  WS_CHILD | WS_VISIBLE,
          200, 200, 100, 100, hWnd, (HMENU) ID_TER, hInstance, NULL);
    
    
        combox = CreateWindow("combobox", "",  WS_CHILD | WS_VISIBLE |CB_SHOWDROPDOWN|CBS_AUTOHSCROLL|CBS_DISABLENOSCROLL ,
          100, 100, 100, 300, hWnd, (HMENU) ID_COM, hInstance, NULL);
    
    
    	
    
    	
    			
    
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       ShowWindow(combox,nCmdShow);
       UpdateWindow(hWnd);
       UpdateWindow(combox);
    
       return TRUE;
    }
    
    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND	- process the application menu
    //  WM_PAINT	- Paint the main window
    //  WM_DESTROY	- post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message)
    	{
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    		switch (wmId)
    		{
    		case IDM_ABOUT:
    			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		case ID_TER:
    			TerminateProcess(hProcess,0);
    
    			break;
    		case ID_COM:
    			PROCESSENTRY32 entry;
    
        entry.dwFlags = sizeof( PROCESSENTRY32 ); 
    
     
     snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL ); 
    	
     
        if ( Process32First( snap, &entry ) == TRUE ) { 
           while ( Process32Next( snap, &entry ) == TRUE ){
             
    			ComboBox_InsertString(combox,0,&entry.szExeFile);
    			ComboBox_GetText(combox,buffer,10000);
    			strcpy(buff,buffer);
    			if(ComboBox_FindItemData(combox,0,buff)==0){
    			
    
    
    
    			
    			hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,entry.th32ProcessID);
    	
    			}
    
    		
    			
    			
    		
    	  
    	 
    	   }
        CloseHandle( snap );
    	  
    	   }
    			
    		break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    // Message handler for about box.
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		return (INT_PTR)TRUE;
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    	}
    	return (INT_PTR)FALSE;
    }

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Process List and Termination Win32 app

    Again: did you try to debug your App? (I'm afraid - you didn't. But you must!)

    Note that from the very begin the hProcess variable is not defined.
    And after executing ID_COM command it points to some process being the last in the process list? Or is it something like "invalid handle"? Or NULL?
    Victor Nijegorodov

  6. #6
    Join Date
    Apr 2010
    Posts
    172

    Re: Process List and Termination Win32 app

    I cant debug nothing happens when i click the terminate button

  7. #7
    Join Date
    Apr 2010
    Posts
    172

    Re: Process List and Termination Win32 app

    When i change the strcpy(buff,buffer); to strcpy(buff,"iexplore.exe"); then click terminate it then closes that process but why cant i select any entry and terminate from strcpy(buff,buffer);

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Process List and Termination Win32 app

    Quote Originally Posted by gaar321 View Post
    I cant debug
    Why?
    You will never write working programs if you "cant debug".
    If you don't know how to do it - then first learn debugging:
    Building, Debugging, and Testing
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2010
    Posts
    172

    Re: Process List and Termination Win32 app

    Sorry to have wasted your time but i have solved my annoying problem

  10. #10
    Join Date
    Apr 2010
    Posts
    172

    Smile Re: Process List and Termination Win32 app

    :L:L i didnt mean i cant debug i just needed to set a breakpoint oopss

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Process List and Termination Win32 app

    Quote Originally Posted by gaar321
    Sorry to have wasted your time but i have solved my annoying problem
    And what was a problem?
    Victor Nijegorodov

  12. #12
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Process List and Termination Win32 app

    You need to store the hProcess returned from OpenProcess (check if it is valid at all) with each entry in the ombobox by calling ComboBox_AddItemData or ComboBox_InsertItemData. The latter needs the current index of the entry.

    When the ID_TER button was issued you need to retrieve the currently selected entry in the combobox by ComboBox_GetCurSel and if successful retrieve the process handle associated to that entry by ComboBox_GetCurSel and following ComboBox_GetItemData. You need to cast the value retrieved to a HANDLE.

    Regards, Alex

    P.S. Don't forget to free all handles when you close the combobox.

  13. #13
    Join Date
    Apr 2010
    Posts
    172

    Arrow Re: Process List and Termination Win32 app

    could i have a example on how you apply that code please

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

Featured