process_ctrlc_stub.cpp 5.96 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/* A stub for Windows console processes (like nmake) that is able to terminate
 * its child process via a generated Ctrl-C event.
 * The termination is triggered by sending a custom message to the HWND of
 * this process. */

#ifndef WINVER
#define WINVER 0x0501
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#include <wchar.h>
#include <cstdlib>
#include <cstdio>

50 51
const wchar_t szTitle[] = L"qtcctrlcstub";
const wchar_t szWindowClass[] = L"wcqtcctrlcstub";
52
UINT uiShutDownWindowMessage;
53
UINT uiInterruptMessage;
54 55 56
HWND hwndMain = 0;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
57 58
BOOL WINAPI shutdownHandler(DWORD dwCtrlType);
BOOL WINAPI interruptHandler(DWORD dwCtrlType);
59
bool isSpaceOrTab(const wchar_t c);
60 61 62 63 64 65 66 67 68
bool startProcess(wchar_t pCommandLine[]);

int main(int argc, char **)
{
    if (argc < 2) {
        fprintf(stderr, "This is an internal helper of Qt Creator. Do not run it manually.\n");
        return 1;
    }

69
    uiShutDownWindowMessage = RegisterWindowMessage(L"qtcctrlcstub_shutdown");
70
    uiInterruptMessage = RegisterWindowMessage(L"qtcctrlcstub_interrupt");
71

72
    WNDCLASSEX wcex = {0};
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    wcex.cbSize = sizeof(wcex);
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = GetModuleHandle(0);
    wcex.lpszClassName = szWindowClass;
    if (!RegisterClassEx(&wcex))
        return 1;

    hwndMain = CreateWindow(szWindowClass, szTitle, WS_DISABLED,
                            0, 0, 0, 0, NULL, NULL, wcex.hInstance, NULL);
    if (!hwndMain)
        return FALSE;

    // Get the command line and remove the call to this executable.
    wchar_t *strCommandLine = _wcsdup(GetCommandLine());
    const size_t strCommandLineLength = wcslen(strCommandLine);
88
    size_t pos = 0;
89 90 91 92 93 94 95 96 97 98 99 100
    bool quoted = false;
    while (pos < strCommandLineLength) {
        if (strCommandLine[pos] == L'"') {
            quoted = !quoted;
        } else if (!quoted && isSpaceOrTab(strCommandLine[pos])) {
            while (isSpaceOrTab(strCommandLine[++pos]));
            break;
        }
        ++pos;
    }

    bool bSuccess = startProcess(strCommandLine + pos);
101 102 103
    free(strCommandLine);

    if (!bSuccess)
104
        return -1;
105 106

    MSG msg;
107
    DWORD dwExitCode = -1;
108 109
    while (GetMessage(&msg, NULL, 0, 0))
    {
110 111
        if (msg.message == WM_DESTROY)
            dwExitCode = msg.wParam;
112 113 114 115
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

116
    return (int)dwExitCode;
117 118 119 120 121
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == uiShutDownWindowMessage) {
122 123
        SetConsoleCtrlHandler(interruptHandler, FALSE);
        SetConsoleCtrlHandler(shutdownHandler, TRUE);
124
        GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
125 126
        PostQuitMessage(0);
        return 0;
127 128 129 130 131
    } else if (message == uiInterruptMessage) {
        SetConsoleCtrlHandler(interruptHandler, TRUE);
        SetConsoleCtrlHandler(shutdownHandler, FALSE);
        GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
        return 0;
132 133 134 135 136 137 138 139 140 141 142 143 144
    }

    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

145
bool isSpaceOrTab(const wchar_t c)
146
{
147
    return c == L' ' || c == L'\t';
148 149
}

150
BOOL WINAPI shutdownHandler(DWORD /*dwCtrlType*/)
151 152 153 154 155
{
    PostMessage(hwndMain, WM_DESTROY, 0, 0);
    return TRUE;
}

156 157 158 159 160
BOOL WINAPI interruptHandler(DWORD /*dwCtrlType*/)
{
    return TRUE;
}

161
DWORD WINAPI processWatcherThread(LPVOID lpParameter)
162 163 164
{
    HANDLE hProcess = reinterpret_cast<HANDLE>(lpParameter);
    WaitForSingleObject(hProcess, INFINITE);
165 166 167 168
    DWORD dwExitCode;
    if (!GetExitCodeProcess(hProcess, &dwExitCode))
        dwExitCode = -1;
    PostMessage(hwndMain, WM_DESTROY, dwExitCode, 0);
169 170 171 172 173
    return 0;
}

bool startProcess(wchar_t *pCommandLine)
{
174
    SECURITY_ATTRIBUTES sa = {0};
175 176 177
    sa.nLength = sizeof(sa);
    sa.bInheritHandle = TRUE;

178
    STARTUPINFO si = {0};
179 180 181 182 183 184
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi;
    DWORD dwCreationFlags = 0;
    BOOL bSuccess = CreateProcess(NULL, pCommandLine, &sa, &sa, TRUE, dwCreationFlags, NULL, NULL, &si, &pi);
    if (!bSuccess) {
185
        fwprintf(stderr, L"qtcreator_ctrlc_stub: Command line failed: %s\n", pCommandLine);
186 187 188 189 190
        return false;
    }

    HANDLE hThread = CreateThread(NULL, 0, processWatcherThread, reinterpret_cast<void*>(pi.hProcess), 0, NULL);
    if (!hThread) {
191
        fwprintf(stderr, L"qtcreator_ctrlc_stub: The watch dog thread cannot be started.\n");
192 193 194 195 196
        return false;
    }
    CloseHandle(hThread);
    return true;
}