int xMain()

in src/native/windows/apps/prunmgr/prunmgr.c [1810:1967]


int xMain(void)
#else
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
#endif
{
    MSG    msg;
    LPAPXCMDLINE lpCmdline;
    HANDLE mutex = NULL;
    BOOL quiet = FALSE;

    apxHandleManagerInitialize();
    hPool     = apxPoolCreate(NULL, 0);

    /* Parse the command line */
    if ((lpCmdline = apxCmdlineParse(hPool, _options, _commands, _altcmds)) == NULL) {
        /* TODO: dispalay error message */
        apxDisplayError(TRUE, NULL, 0, "Error parsing command line");
        goto cleanup;
    }

    if (!lpCmdline->dwCmdIndex) {
        /* Skip sytem error message */
        SetLastError(ERROR_SUCCESS);
        apxDisplayError(TRUE, NULL, 0,
                        apxLoadResourceA(IDS_ERRORCMD, 0),
                        lpCmdLine);
        goto cleanup;
    }
    else if (lpCmdline->dwCmdIndex == 4)
        quiet = TRUE;
    else if (lpCmdline->dwCmdIndex >= 2)
        bEnableTry = TRUE;
    hService = apxCreateService(hPool, SC_MANAGER_CONNECT, FALSE);
    if (IS_INVALID_HANDLE(hService)) {
        if (!quiet)
            apxDisplayError(TRUE, NULL, 0, "Unable to open the Service Manager");
        goto cleanup;
    }
    /* Open the main service handle */
    if (!apxServiceOpen(hService, lpCmdline->szApplication,
                        SERVICE_ALL_ACCESS)) {
        LPWSTR w = lpCmdline->szApplication + lstrlenW(lpCmdline->szApplication) - 1;
        if (*w == L'w')
            *w = L'\0';
        if (!apxServiceOpen(hService, lpCmdline->szApplication,
                            SERVICE_ALL_ACCESS)) {
            if (!apxServiceOpen(hService, lpCmdline->szApplication,
                                GENERIC_READ | GENERIC_EXECUTE)) {
                if (!quiet)
                    apxDisplayError(TRUE, NULL, 0, "Unable to open the service '%S'",
                                    lpCmdline->szApplication);
                goto cleanup;
            }
        }
    }
    /* Obtain service parameters and status */
    if (!(_currentEntry = apxServiceEntry(hService, TRUE))) {
        if (!quiet)
            apxDisplayError(TRUE, NULL, 0, "Unable to query the service '%S' status",
                            lpCmdline->szApplication);
        goto cleanup;
    }
    _gui_store = apxGuiInitialize(MainWndProc, lpCmdline->szApplication);
    if (!_gui_store) {
        if (!quiet)
            apxDisplayError(TRUE, NULL, 0, "Unable to initialize GUI manager");
        goto cleanup;
    }
    hIcoRun  = LoadImage(_gui_store->hInstance, MAKEINTRESOURCE(IDI_ICONRUN),
                         IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
    hIcoStop = LoadImage(_gui_store->hInstance, MAKEINTRESOURCE(IDI_ICONSTOP),
                         IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

    /* Handle //MQ// option */
    if (lpCmdline->dwCmdIndex == 4) {
        HANDLE hOther = FindWindow(_gui_store->szWndClass, NULL);
        if (hOther)
            SendNotifyMessage(hOther, WM_CLOSE, 0, 0);
        goto cleanup;
    }

    if (!_options[0].dwValue) {
        mutex = CreateMutex(NULL, FALSE, _gui_store->szWndMutex);
        if ((mutex == NULL) || (GetLastError() == ERROR_ALREADY_EXISTS)) {
            HANDLE hOther = FindWindow(_gui_store->szWndClass, NULL);
            if (hOther) {
                SetForegroundWindow(hOther);
                SendNotifyMessage(hOther, WM_COMMAND, MAKEWPARAM(IDM_TM_CONFIG, 0), 0);
            }
            else {
                /* Skip sytem error message */
                SetLastError(ERROR_SUCCESS);
                if (!quiet)
                    apxDisplayError(TRUE, NULL, 0, apxLoadResourceA(IDS_ALREAY_RUNING, 0),
                                    lpCmdline->szApplication);
            }
            goto cleanup;
        }
    }
    hRegistry = apxCreateRegistryW(hPool, KEY_ALL_ACCESS, NULL,
                                  apxLoadResourceW(IDS_APPLICATION, 0),
                                  APXREG_USER);
    loadConfiguration();
    hRegserv = apxCreateRegistryW(hPool, KEY_READ | KEY_WRITE | KREG_WOW6432,
                                  PRG_REGROOT,
                                  lpCmdline->szApplication,
                                  APXREG_SOFTWARE | APXREG_SERVICE);

    if (IS_INVALID_HANDLE(hRegserv)) {
        if (!quiet)
            apxDisplayError(TRUE, NULL, 0, apxLoadResourceA(IDS_ERRSREG, 0));
        return FALSE;
    }
    isManagerRunning = TRUE;
    CreateThread(NULL, 0, refreshThread, NULL, 0, NULL);
    /* Create main invisible window */
    _gui_store->hMainWnd = CreateWindow(_gui_store->szWndClass,
                                        apxLoadResourceW(IDS_APPLICATION, 0),
                                        0, 0, 0, 0, 0,
                                        NULL, NULL,
                                        _gui_store->hInstance,
                                        NULL);

    if (!_gui_store->hMainWnd) {
        goto cleanup;
    }
    if (lpCmdline->dwCmdIndex == 3)
        PostMessage(_gui_store->hMainWnd, WM_COMMAND, IDM_TM_START, 0);
    while (GetMessage(&msg, NULL, 0, 0))  {
        if(!TranslateAccelerator(_gui_store->hMainWnd,
                                 _gui_store->hAccel, &msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    isManagerRunning = FALSE;
    saveConfiguration();

cleanup:
    if (hIcoStop)
        DestroyIcon(hIcoStop);
    if (hIcoRun)
        DestroyIcon(hIcoRun);
    if (mutex)
        CloseHandle(mutex);
    if (lpCmdline)
        apxCmdlineFree(lpCmdline);
    apxCloseHandle(hService);
    apxHandleManagerDestroy();
    ExitProcess(0);
    return 0;
	UNREFERENCED_PARAMETER(hInstance);
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(nCmdShow);
}