VirtualBox

Changeset 39202 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Nov 4, 2011 11:07:23 AM (13 years ago)
Author:
vboxsync
Message:

VBoxService/Windows: Added gentle shutdown when user does CTRL+C/CTRL+BREAK/close the console window.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r38636 r39202  
    374374
    375375
     376#ifdef RT_OS_WINDOWS
     377static BOOL WINAPI VBoxServiceConsoleControlHandler(DWORD dwCtrlType)
     378{
     379    int rc = VINF_SUCCESS;
     380    bool fEventHandled = FALSE;
     381    switch (dwCtrlType)
     382    {
     383        /* User pressed CTRL+C or CTRL+BREAK or an external event was sent
     384         * via GenerateConsoleCtrlEvent(). */
     385        case CTRL_BREAK_EVENT:
     386        case CTRL_CLOSE_EVENT:
     387        case CTRL_C_EVENT:
     388            VBoxServiceVerbose(2, "ControlHandler: Received break/close event\n");
     389            rc = VBoxServiceStopServices();
     390            fEventHandled = TRUE;
     391            break;
     392        default:
     393            break;
     394        /** @todo Add other events here. */
     395    }
     396
     397    if (RT_FAILURE(rc))
     398        VBoxServiceError("ControlHandler: Event %ld handled with error rc=%Rrc\n",
     399                         dwCtrlType, rc);
     400    return fEventHandled;
     401}
     402#endif /* RT_OS_WINDOWS */
     403
     404
    376405/**
    377406 * Starts the service.
     
    555584    RTSemEventDestroy(g_hEvtWindowsService);
    556585    g_hEvtWindowsService = NIL_RTSEMEVENT;
    557 
    558586#else
    559587    /*
     
    864892         *          and return immediately.
    865893         */
     894#ifdef RT_OS_WINDOWS
     895# ifndef RT_OS_NT4
     896        /* Install console control handler. */
     897        if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)VBoxServiceConsoleControlHandler, TRUE /* Add handler */))
     898        {
     899            VBoxServiceError("Unable to add console control handler, error=%ld\n", GetLastError());
     900            /* Just skip this error, not critical. */
     901        }
     902# endif /* !RT_OS_NT4 */
     903#endif /* RT_OS_WINDOWS */
    866904        rc = VBoxServiceStartServices();
    867905        rcExit = RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
    868906        if (RT_SUCCESS(rc))
    869907            VBoxServiceMainWait();
     908#ifdef RT_OS_WINDOWS
     909# ifndef RT_OS_NT4
     910        /* Uninstall console control handler. */
     911        if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)NULL, FALSE /* Remove handler */))
     912        {
     913            VBoxServiceError("Unable to remove console control handler, error=%ld\n", GetLastError());
     914            /* Just skip this error, not critical. */
     915        }
     916# endif /* !RT_OS_NT4 */
     917#else /* !RT_OS_WINDOWS */
     918        /* On Windows - since we're running as a console application - we already stopped all services
     919         * through the console control handler. So only do the stopping of services here on other platforms
     920         * where the break/shutdown/whatever signal was just received. */
    870921        VBoxServiceStopServices();
     922#endif /* RT_OS_WINDOWS */
    871923    }
    872924    VBoxServiceReportStatus(VBoxGuestFacilityStatus_Terminated);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette