Login

 
jextensions.com

Site Menu

Recently someone in the #winapi channel I frequent ask about EndTask. It doesn't seem to be declared in windows.h. Well, it was a late comer to the API after Windows XP and it doesn't even seem to be in windows.h for Visual Studio 2012's included SDK either.

 If you are interested in a simpler implementation without using LoadLibrary & GetProcAddress I have written another article here.

Using EndTask is still simple, but requires you do all the work yourself using LoadLibrary and GetProcAddress. Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
typedef BOOL (CALLBACK* EndTaskProc)(HWND, BOOL, BOOL);
BOOL WINAPI EndTask(_In_ HWND hWnd, _In_ BOOL fShutDown, _In_ BOOL fForce)
{
    HMODULE user32 = ::LoadLibrary(_T("User32.dll"));
    if(user32 == NULL) return FALSE;
    else {
        BOOL result = FALSE;
        EndTaskProc endtask = (EndTaskProc) ::GetProcAddress(user32, "EndTask");
        if(endtask != NULL) result = endtask(hWnd, fShutDown, fForce);
        ::FreeLibrary(user32);
        return result;
    }
}

Don't forget you'll need to include windows.h since we are using some window's types. The usage is the same as the documentation shows at MSDN.

If you were interested in a usage example, here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h" /* Windows.h is included in this header, but also iostream and string. */
 
/* Your EndTask code can go here. */
 
int _tmain(int argc, _TCHAR* argv[])
{
    std::wstring window_text;
    std::wcout << L"Enter a window title to close: ";
    std::wcin >> window_text;
 
    HWND hWnd = ::FindWindowW(NULL, window_text.c_str());
    if(hWnd == NULL) std::wcout << L"Failed to find window." << std::endl;
    else std::wcout << L"Using EndTask (Force Close) on 0x" << hWnd << L"." << std::endl;
 
    if(EndTask(hWnd, FALSE, TRUE)) std::wcout << L"Successfully used EndTask." << std::endl;
    else std::wcout << L"EndTask failed with error " << ::GetLastError() << L"." << std::endl;
 
    return 0;
}



https://globerx24.com/nexium