terminating wrong process

This is a discussion on terminating wrong process within the Windows Programming forums, part of the Platform Specific Boards category; This script is terminating itself rather than the desktop. What's wrong? Code: HANDLE dwProcess; DWORD dwProcessId; GetWindowThreadProcessId(HWND_DESKTOP, &dwProcessId); GetExitCodeProcess(dwProcess, &dwProcessId); ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Exclamation terminating wrong process

    This script is terminating itself rather than the desktop.
    What's wrong?

    Code:
        HANDLE dwProcess;
        DWORD dwProcessId;
        GetWindowThreadProcessId(HWND_DESKTOP, &dwProcessId);
        GetExitCodeProcess(dwProcess, &dwProcessId);
        TerminateProcess(dwProcess, dwProcessId);

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    At the moment dwProcess is 0 (maybe 0 means the current process).

    Maybe this would work:
    Code:
        HANDLE dwProcess;
        DWORD dwProcessId;
        DWORD dwExitId;
        GetWindowThreadProcessId(HWND_DESKTOP, &dwProcessId);
        dwProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,dwProcessId);
        GetExitCodeProcess(dwProcess, &dwExitId);
        TerminateProcess(dwProcess, dwExitId);
        CloseHandle(dwProcess);
    Last edited by maxorator; 09-08-2006 at 08:09 AM.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Code:
        HANDLE dwProcess = NULL;
        DWORD dwProcessId, dwExitId;
        TOKEN_PRIVILEGES tpPrevToken;
        GetWindowThreadProcessId(HWND_DESKTOP, &dwProcessId);
        dwProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, dwProcessId);
        if(dwProcess == NULL)
        MessageBox(0, "OpenProcess()", "Error", 0);
        if(AdjustTokenPrivileges(dwProcess, TRUE, 0, sizeof(SE_PRIVILEGE_ENABLED), &tpPrevToken, 0) != ERROR_SUCCESS)
        MessageBox(0, "AdjustTokenPrivileges()", "Error", 0);
        if(GetExitCodeProcess(dwProcess, &dwExitId) == 0)
        MessageBox(0, "GetExitCodeProcess()", "Error", 0);
        if(TerminateProcess(dwProcess, dwExitId) == 0)
        MessageBox(0, "TerminateProcess()", "Error", 0);
        if(CloseHandle(dwProcess) == 0)
        MessageBox(0, "CloseHandle()", "Error", 0);
    I think OpenProcess() is failing, causing all the following to fail.

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Okay, now when I change HWND_DESKTOP to FindWindow("Notepad", 0) it will close my notepad with no errors at all. So now the only thing that I can think of is that AdjustTokenPrivileges() isn't doing it's job.

    Got any idea on what to do now?

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If I would be on my home computer, I would test thousands of different things to do that, but that's not possible at the moment...

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Ok, I think HWND_DESKTOP is not the desktop window (explorer.exe), but the major window, that contains everything on the screen. E.g. if you use GetDC(HWND_DESKTOP) and then write something on that device context, it comes on any screen or window, not only to the desktop. So you must find the correct process.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    It must be, capture the desktop with WinSpy++, you will see that the process is explorer.exe, also when you terminate an explorer window it also takes the desktop with it, but the system restores it right away.

    EDIT:

    I think that the exlorer windows are just threads rather than indapendit programs. Also becuase there is always only ONE process of explorer.exe, even when you have multipule windows open, Infact through the task manager you can see the thread count go up when you open a new explorer window.
    Last edited by Queatrix; 09-08-2006 at 10:59 AM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I can just close explorer.exe from the task manager and just mess around without desktop and start menu.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Are you closing it from the Application tab? If so, it won't work, you have to do it in the Processes tab.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Of course I close it from the processes tab, when I don't need the desktop, because it uses some memory

  11. #11
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by maxorator
    I think HWND_DESKTOP is not the desktop window (explorer.exe), but the major window, that contains everything on the screen.
    I know they are not the same window, however, they are the same process. (explorer.exe)
    Code:
    if(FindWindow("SysListView32", 0) == HWND_DESKTOP) Beep(2000,100);
    To prove it, this beeped.

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That proves nothing. HWND_DESKTOP is defined as (HWND)0, and FindWindow returns NULL if it fails; thus, your call to FindWindow failed.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    This closes desktop window and all explorer windows (explorer.exe), but the system restores it:
    Code:
    DWORD process_id_array[1024];
    DWORD bytes_returned;
    DWORD num_processes;
    HANDLE hProcess;
    char image_name[256];
    char buffer[256];
    DWORD exitcode;
    EnumProcesses(process_id_array, 1024*sizeof(DWORD), &bytes_returned);
    num_processes = bytes_returned/sizeof(DWORD);
    for(int i=0;i<num_processes;i++){
    	hProcess=OpenProcess(PROCESS_ALL_ACCESS,TRUE,process_id_array[i]);
    	if(GetModuleBaseName(hProcess,0,image_name,256)){
    		if(!stricmp(image_name,"Explorer.EXE")){
    			GetExitCodeProcess(hProcess, &exitcode);
    			if(exitcode==STILL_ACTIVE){
    				TerminateProcess(hProcess,0);
    			}
    		}
    	}
    	CloseHandle(hProcess);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 05:24 PM
  2. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 06:16 PM
  3. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 12:08 PM
  4. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 03:00 PM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21