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

📄 page07ea.cpp

📁 This ZIP archive includes the sources (and executable) for the FileTest utility, written by Ladisla
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                                   &FileEaInfo,
                                    sizeof(FILE_EA_INFORMATION),
                                    FileEaInformation);
    
    // Allocate the buffer large enough to hold the EAs.
    if(Status == STATUS_SUCCESS && FileEaInfo.EaSize > 0)
    {
        EaBufferSize = FileEaInfo.EaSize;
        EaBuffer = (PFILE_FULL_EA_INFORMATION)HeapAlloc(g_hHeap, 0, EaBufferSize);
        if(EaBuffer == NULL)
            Status = STATUS_INSUFFICIENT_RESOURCES;
    }

    // Query the EAs, if any. If the buffer is not large enough,
    // double its size and try again
    if(Status == STATUS_SUCCESS && EaBufferSize > 0)
    {
        BOOL bEndLoop = FALSE;

        while(bEndLoop == FALSE)
        {
            Status = NtQueryEaFile(pData->hFile,
                                  &IoStatus,
                                   EaBuffer,
                                   EaBufferSize,
                                   FALSE,
                                   NULL,
                                   0,
                                   NULL,
                                   TRUE);

            switch(Status)
            {
                // We succeeded or not "buffer too small" => break    
                default:
                case STATUS_SUCCESS:    
                    bEndLoop = TRUE;
                    break;

                // We need to increment buffer size
                case STATUS_BUFFER_OVERFLOW:
                case STATUS_BUFFER_TOO_SMALL:
                    EaBufferSize *= 2;
                    EaBuffer = (PFILE_FULL_EA_INFORMATION)HeapReAlloc(g_hHeap, 0, EaBuffer, EaBufferSize);
                    Status = STATUS_SUCCESS;
                    if(EaBuffer == NULL)
                    {
                        Status = STATUS_INSUFFICIENT_RESOURCES;
                        bEndLoop = TRUE;
                    }
                    break;
            }
        }
    }

    // If we got something, fill the listview
    if(Status == STATUS_SUCCESS)
    {
        ListView_DeleteAllItems(hListView);

        // Load the listview with the extended attributes
        if(EaBuffer != NULL)
        {
            EaBufferItem = EaBuffer;
            for(;;)
            {
                // Create the EA copy and insert it to the list
                EaItemSize = GetEaEntrySize(EaBufferItem);
                EaItemCopy = (PFILE_FULL_EA_INFORMATION)(new char [EaItemSize]);
                CopyMemory(EaItemCopy, EaBufferItem, EaItemSize);
                EaItemCopy->NextEntryOffset = EaItemSize;
                SetListViewEaEntry(hListView, -1, EaItemCopy, TRUE);

                // Move to the next item
                if(EaBufferItem->NextEntryOffset == 0)
                    break;
                EaBufferItem = (PFILE_FULL_EA_INFORMATION)((PBYTE)EaBufferItem + EaBufferItem->NextEntryOffset);
            }
        }
    }

    // Set the result to the dialog
    SetDlgItemText(hDlg, IDC_RESULT_STATUS, NtStatus2Text(Status));
    SetDlgItemInt(hDlg, IDC_RESULT_LENGTH, (UINT)IoStatus.Information, FALSE);
    if(EaBuffer != NULL)
        HeapFree(g_hHeap, 0, EaBuffer);
    return TRUE;
}


static int OnSetEa(HWND hDlg)
{
    PFILE_FULL_EA_INFORMATION EaInfoItem;
    PFILE_FULL_EA_INFORMATION EaInfo;
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    IO_STATUS_BLOCK IoStatus = {0};
    NTSTATUS Status = STATUS_SUCCESS;
    LPBYTE EaBuffer = NULL;
    ULONG EaBufferSize = 0;
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    int nItemCount = ListView_GetItemCount(hListView);
    int nIndex;

    // Calculate the buffer necessary to set the EA.
    for(nIndex = 0; nIndex < nItemCount; nIndex++)
    {
        EaInfoItem = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nIndex);

        if(EaInfoItem != NULL)
            EaBufferSize += EaInfoItem->NextEntryOffset;
    }

    // Allocate buffer for the EAs
    if(EaBufferSize > 0)
    {
        EaBuffer = new BYTE[EaBufferSize];
        if(EaBuffer == NULL)
            Status = STATUS_INSUFFICIENT_RESOURCES;
    }

    // Create the extended attributes array
    if(Status == STATUS_SUCCESS)
    {
        // Clear the buffer
        if(EaBufferSize > 0)
            ZeroMemory(EaBuffer, EaBufferSize);
        EaInfo = (PFILE_FULL_EA_INFORMATION)EaBuffer;

        // Convert the EA items
        for(nIndex = 0; nIndex < nItemCount; nIndex++)
        {
            EaInfoItem = (PFILE_FULL_EA_INFORMATION)ListView_GetItemParam(hListView, nIndex);

            if(EaInfoItem != NULL)
            {
                CopyMemory(EaInfo, EaInfoItem, EaInfoItem->NextEntryOffset);

                // If not the last item, move to the next
                if(nIndex < nItemCount - 1)
                    EaInfo = (PFILE_FULL_EA_INFORMATION)((PBYTE)EaInfo + EaInfo->NextEntryOffset);
                else
                    EaInfo->NextEntryOffset = 0;
            }
        }

        // Set the extended attributes
        Status = NtSetEaFile(pData->hFile,
                            &IoStatus,
                             EaBuffer,
                             EaBufferSize);
    }

    // Set the result to the dialog
    SetDlgItemText(hDlg, IDC_RESULT_STATUS, NtStatus2Text(Status));
    SetDlgItemInt(hDlg, IDC_RESULT_LENGTH, (UINT)IoStatus.Information, FALSE);
    if(EaBuffer != NULL)
        delete [] EaBuffer;
    
    return TRUE;
}

static int OnDeleteItem(HWND /* hDlg */, LPNMLISTVIEW pNMListView)
{
    PFILE_FULL_EA_INFORMATION EaInfo = (PFILE_FULL_EA_INFORMATION)pNMListView->lParam;

    if(EaInfo != NULL)
        delete [] EaInfo;
    return TRUE;
}

                
static int OnDoubleClick(HWND hDlg)
{
    HWND hListView = GetDlgItem(hDlg, IDC_EA_LIST);
    int nSelected = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);

    if(nSelected == -1)
        return OnInsertEa(hDlg);
    else
        return OnEditEa(hDlg);
}

static int OnCommand(HWND hDlg, UINT nNotify, UINT nIDCtrl)
{
    if(nNotify == BN_CLICKED)
    {
        switch(nIDCtrl)
        {
            case IDC_UP:
                MoveItem(hDlg, TRUE);
                return TRUE;

            case IDC_DOWN:
                MoveItem(hDlg, FALSE);
                return TRUE;

            case IDC_INSERT:
                return OnInsertEa(hDlg);

            case IDC_EDIT:
                return OnEditEa(hDlg);

            case IDC_DELETE:
                return OnDeleteEa(hDlg);

            case IDC_QUERY_EA:
                return OnQueryEa(hDlg);

            case IDC_SET_EA:
                return OnSetEa(hDlg);
        }
    }

    return FALSE;
}

static int OnNotify(HWND hDlg, NMHDR * pNMHDR)
{
    switch(pNMHDR->code)
    {
        case PSN_SETACTIVE:
            return OnSetActive(hDlg);

        case LVN_DELETEITEM:
            return OnDeleteItem(hDlg, (LPNMLISTVIEW)pNMHDR);

        case LVN_ITEMCHANGED:
            UpdateDialogButtons(hDlg);
            return TRUE;

        case NM_DBLCLK:
            if(pNMHDR->idFrom == IDC_EA_LIST)
                return OnDoubleClick(hDlg);
            break;
    }
    return FALSE;
}

//-----------------------------------------------------------------------------
// Public functions

INT_PTR CALLBACK PageProc07(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_INITDIALOG:
            return OnInitDialog(hDlg, lParam);

        case WM_COMMAND:
            return OnCommand(hDlg, HIWORD(wParam), LOWORD(wParam));

        case WM_NOTIFY:
            return OnNotify(hDlg, (NMHDR *)lParam);
    }
    return FALSE;
}

⌨️ 快捷键说明

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