⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gpolicy.c

📁 这是一个开放源代码的与WINNT/WIN2K/WIN2003兼容的操作系统
💻 C
📖 第 1 页 / 共 2 页
字号:
    BOOL Ret = FALSE;

    EnterCriticalSection(&GPNotifyLock);

    /* create the thread notification event */
    if (hNotificationThreadEvent == NULL)
    {
        hNotificationThreadEvent = CreateEvent(NULL,
                                               TRUE,
                                               FALSE,
                                               NULL);
        if (hNotificationThreadEvent == NULL)
        {
            goto Cleanup;
        }
    }

    /* create or open the machine group policy event */
    if (hMachineGPAppliedEvent == NULL)
    {
        lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
        if (lpSecurityDescriptor == NULL)
        {
            goto Cleanup;
        }

        hMachineGPAppliedEvent = CreateGPEvent(TRUE,
                                               lpSecurityDescriptor);
        if (hMachineGPAppliedEvent == NULL)
        {
            goto Cleanup;
        }
    }

    /* create or open the local group policy event only if necessary */
    if (!bMachine && hLocalGPAppliedEvent == NULL)
    {
        if (lpSecurityDescriptor == NULL)
        {
            lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
            if (lpSecurityDescriptor == NULL)
            {
                goto Cleanup;
            }
        }

        hLocalGPAppliedEvent = CreateGPEvent(FALSE,
                                             lpSecurityDescriptor);
        if (hLocalGPAppliedEvent == NULL)
        {
            goto Cleanup;
        }
    }

    if (hNotificationThread == NULL)
    {
        hNotificationThread = CreateThread(NULL,
                                           0,
                                           GPNotificationThreadProc,
                                           NULL,
                                           0,
                                           NULL);
    }

    if (hNotificationThread != NULL)
    {
        Notify = (PGP_NOTIFY)LocalAlloc(LMEM_FIXED,
                                        sizeof(GP_NOTIFY));
        if (Notify != NULL)
        {
            /* add the item to the beginning of the list */
            Notify->Next = NotificationList;
            Notify->hEvent = hEvent;
            Notify->bMachine = bMachine;

            NotificationList = Notify;

            /* notify the thread */
            GPNotificationAction = gpaUpdate;
            SetEvent(hNotificationThreadEvent);

            Ret = TRUE;
        }
    }

Cleanup:
    LeaveCriticalSection(&GPNotifyLock);

    if (lpSecurityDescriptor != NULL)
    {
        LocalFree((HLOCAL)lpSecurityDescriptor);
    }

    /* NOTE: don't delete the events or close the handles created */

    return Ret;
}

BOOL WINAPI
UnregisterGPNotification(IN HANDLE hEvent)
{
    PGP_NOTIFY Notify = NULL, *NotifyLink;
    BOOL Ret = FALSE;

    EnterCriticalSection(&GPNotifyLock);

    Notify = NotificationList;
    NotifyLink = &NotificationList;

    while (Notify != NULL)
    {
        if (Notify->hEvent == hEvent)
        {
            /* remove and free the item */
            *NotifyLink = Notify->Next;
            LocalFree((HLOCAL)Notify);

            /* notify the thread */
            if (hNotificationThread != NULL &&
                hNotificationThreadEvent != NULL)
            {
                GPNotificationAction = gpaUpdate;
                SetEvent(hNotificationThreadEvent);
            }

            Ret = TRUE;
            break;
        }

        NotifyLink = &Notify->Next;
        Notify = Notify->Next;
    }

    LeaveCriticalSection(&GPNotifyLock);

    return Ret;
}

BOOL WINAPI
RefreshPolicy(IN BOOL bMachine)
{
    HANDLE hEvent;
    BOOL Ret = TRUE;

    hEvent = OpenEventW(EVENT_MODIFY_STATE,
                        FALSE,
                        (bMachine ? szMachineGPRefreshEvent : szLocalGPRefreshEvent));
    if (hEvent != NULL)
    {
        Ret = SetEvent(hEvent);
        CloseHandle(hEvent);
    }

    /* return TRUE even if the mutex doesn't exist! */
    return Ret;
}

BOOL WINAPI
RefreshPolicyEx(IN BOOL bMachine,
                IN DWORD dwOptions)
{
    if (dwOptions & ~RP_FORCE)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    if (dwOptions & RP_FORCE)
    {
        HANDLE hEvent;
        BOOL Ret = TRUE;

        hEvent = OpenEventW(EVENT_MODIFY_STATE,
                            FALSE,
                            (bMachine ? szMachineGPForceRefreshEvent : szLocalGPForceRefreshEvent));
        if (hEvent != NULL)
        {
            Ret = SetEvent(hEvent);
            CloseHandle(hEvent);
        }

        /* return TRUE even if the mutex doesn't exist! */
        return Ret;
    }
    else
    {
        return RefreshPolicy(bMachine);
    }
}

HANDLE WINAPI
EnterCriticalPolicySection(IN BOOL bMachine)
{
    SECURITY_ATTRIBUTES SecurityAttributes;
    PSECURITY_DESCRIPTOR lpSecurityDescriptor;
    HANDLE hSection;

    /* create or open the mutex */
    lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
    if (lpSecurityDescriptor != NULL)
    {
        SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
        SecurityAttributes.lpSecurityDescriptor = lpSecurityDescriptor;
        SecurityAttributes.bInheritHandle = FALSE;

        hSection = CreateMutexW(&SecurityAttributes,
                                FALSE,
                                (bMachine ? szMachineGPMutex : szLocalGPMutex));

        LocalFree((HLOCAL)lpSecurityDescriptor);

        if (hSection != NULL)
        {
            /* wait up to 10 minutes */
            if (WaitForSingleObject(hSection,
                                    600000) != WAIT_FAILED)
            {
                return hSection;
            }

            CloseHandle(hSection);
        }
    }

    return NULL;
}

BOOL WINAPI
LeaveCriticalPolicySection(IN HANDLE hSection)
{
    BOOL Ret;

    if (hSection == NULL)
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }

    Ret = ReleaseMutex(hSection);
    CloseHandle(hSection);

    return Ret;
}

BOOL WINAPI
WaitForUserPolicyForegroundProcessing(VOID)
{
    HANDLE hEvent;
    BOOL Ret = FALSE;

    hEvent = OpenEventW(SYNCHRONIZE,
                        FALSE,
                        szLocalGPDoneEvent);
    if (hEvent != NULL)
    {
        Ret = WaitForSingleObject(hEvent,
                                  INFINITE) != WAIT_FAILED;
        CloseHandle(hEvent);
    }

    return Ret;
}

BOOL WINAPI
WaitForMachinePolicyForegroundProcessing(VOID)
{
    HANDLE hEvent;
    BOOL Ret = FALSE;

    hEvent = OpenEventW(SYNCHRONIZE,
                        FALSE,
                        szMachineGPDoneEvent);
    if (hEvent != NULL)
    {
        Ret = WaitForSingleObject(hEvent,
                                  INFINITE) != WAIT_FAILED;
        CloseHandle(hEvent);
    }

    return Ret;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -