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

📄 page06ntfileinfo.cpp

📁 This ZIP archive includes the sources (and executable) for the FileTest utility, written by Ladisla
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    TStructMember * pMemberInfo = (TStructMember *)pTVDispInfo->item.lParam;
    TCHAR szItemText[1024] = _T("");
    HWND hTreeView = pTVDispInfo->hdr.hwndFrom;
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    HWND hEdit;
    BOOL bCancelEdit = TRUE;

    // Verify the currently selected file info class.
    // Some info classes are not allowed to edit
    FileInfoClass = (FILE_INFORMATION_CLASS)(ComboBox_GetCurSel(hCombo) + 1);
    if(FileInfoData[(int)FileInfoClass - 1].bIsChain == TRUE ||
       FileInfoData[(int)FileInfoClass - 1].bIsEditable == FALSE)
    {
        MessageBoxRc(hDlg, IDS_ERROR, IDS_E_CANT_EDIT_THIS);
        SetWindowLongPtr(hDlg, DWLP_MSGRESULT, TRUE);
        return TRUE;
    }

    // If the item has an associated struct member, allow editing
    if(pMemberInfo != NULL && pMemberInfo->nMemberSize > 0)
    {
        // Get the edit control which will be shown
        hEdit = TreeView_GetEditControl(hTreeView);
        if(hEdit != NULL)
        {
            // Get the item text
            if(DataToItemText(pMemberInfo, szItemText, _tsize(szItemText), FALSE) == ERROR_SUCCESS)
            {
                SetWindowText(hEdit, szItemText);
                EnableExitButton(hDlg, FALSE);
                bCancelEdit = FALSE;
            }
        }
    }

    // Store the result info the dialog's private variables
    SetWindowLongPtr(hDlg, DWLP_MSGRESULT, bCancelEdit);
    return TRUE;
}

static int OnEndLabelEdit(HWND hDlg, NMTVDISPINFO * pTVDispInfo)
{
    TStructMember * pMemberInfo = (TStructMember *)pTVDispInfo->item.lParam;
    HWND hTreeView = pTVDispInfo->hdr.hwndFrom;
    BOOL bAcceptChanges = FALSE;

    // If the item has an associated member, analyze the result text
    if(pMemberInfo != NULL && pTVDispInfo->item.pszText != NULL)
    {
        // Try to convert the text to appropriate data item
        if(ItemTextToData(pMemberInfo, pTVDispInfo->item.pszText) == ERROR_SUCCESS)
        {
            HTREEITEM hParentItem;

            // We have to reload all items at the same level, because 
            // the ItemTextToData might have changed more than one item data
            // (Example: variable length strings editation change also the length)
            hParentItem = TreeView_GetNextItem(hTreeView, pTVDispInfo->item.hItem, TVGN_PARENT);
            PostMessage(hDlg, WM_RELOADITEMS, (WPARAM)hTreeView, (LPARAM)hParentItem);
            bAcceptChanges = TRUE;
        }
        else
        {
            // We should warn the user about that the item
            // has invalid format for its data type
            MessageBoxRc(hDlg, IDS_ERROR, IDS_E_VALUE_HAS_BAD_FORMAT);
        }
    }

    // Enable the exit button
    EnableExitButton(hDlg, TRUE);
    SetWindowLongPtr(hDlg, DWLP_MSGRESULT, bAcceptChanges);
    return TRUE;
}

static int OnEditLabel(HWND hDlg)
{
    HTREEITEM hItem;
    HWND hTreeView = GetDlgItem(hDlg, IDC_FILE_INFO);

    // Only start editing if the tree view has focus
    if(GetFocus() == hTreeView)
    {
        hItem = TreeView_GetSelection(hTreeView);
        if(hItem != NULL)
            TreeView_EditLabel(hTreeView, hItem);
    }

    return TRUE;
}


static int OnFileInfoClassChanged(HWND hDlg)
{
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    UINT nFileInfoClass = ComboBox_GetCurSel(hCombo) + 1;
    int nInputLength;

    ZeroMemory(pData->pbFileInfoBuff, pData->cbFileInfoBuff);
    nInputLength = FillDialogWithFileInfo(hDlg, (FILE_INFORMATION_CLASS)nFileInfoClass);
    SetDlgItemInt(hDlg, IDC_INPUT_LENGTH, nInputLength, FALSE);
    return TRUE;
}

static int OnDefaultLengthClick(HWND hDlg)
{
    TFileInfoData * pFileInfoData;
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    UINT nFileInfoClass = ComboBox_GetCurSel(hCombo);
    UINT nDataLength = 0;

    pFileInfoData = &FileInfoData[nFileInfoClass];
    nDataLength = GetStructLength(pFileInfoData->pStructMembers);

    SetDlgItemInt(hDlg, IDC_INPUT_LENGTH, nDataLength, FALSE);
    return TRUE;
}

static int OnMaximumLengthClick(HWND hDlg)
{
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);

    SetDlgItemInt(hDlg, IDC_INPUT_LENGTH, pData->cbFileInfoBuff, FALSE);
    return TRUE;
}

static int OnQueryInfoClick(HWND hDlg)
{
    FILE_INFORMATION_CLASS FileInfoClass;
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    IO_STATUS_BLOCK IoStatus = {0};
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG Length = 0;
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    BOOL bTranslated = FALSE;

    // Test if the handle is an NT file handle and get the file info class
    if(IsHandleValid(pData->hFile) == FALSE)
        return FALSE;
    FileInfoClass = (FILE_INFORMATION_CLASS)(ComboBox_GetCurSel(hCombo) + 1);

    // Get the input length
    Length = GetDlgItemInt(hDlg, IDC_INPUT_LENGTH, &bTranslated, FALSE);
    if(bTranslated == FALSE)
        Status = STATUS_UNSUCCESSFUL;
    if(Length > (ULONG)pData->cbFileInfoBuff)
        Length = (ULONG)pData->cbFileInfoBuff;

    if(NT_SUCCESS(Status))
    {
        Status = NtQueryInformationFile(pData->hFile,
                                        &IoStatus,
                                        pData->pbFileInfoBuff,
                                        pData->cbFileInfoBuff,
                                        FileInfoClass);
    }                                        

    // If succeeded, we have to fill the dialog with file info
    if(NT_SUCCESS(Status) || Status == STATUS_BUFFER_OVERFLOW)
        FillDialogWithFileInfo(hDlg, FileInfoClass);

    // Set the result status and return
    SetDlgItemText(hDlg, IDC_RESULT_STATUS, NtStatus2Text(Status));
    SetDlgItemInt(hDlg, IDC_RESULT_LENGTH, (UINT)IoStatus.Information, FALSE);
    return TRUE;
}


static int OnQueryDirClick(HWND hDlg)
{
    FILE_INFORMATION_CLASS FileInfoClass;
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    IO_STATUS_BLOCK IoStatus = {0};
    NTSTATUS Status = STATUS_SUCCESS;
    HANDLE hEvent = NULL;
    ULONG Length = 0;
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    BOOL bTranslated = FALSE;

    // Test if the handle is an NT file handle and get the file info class
    if(IsHandleValid(pData->hFile) == FALSE)
        return FALSE;
    FileInfoClass = (FILE_INFORMATION_CLASS)(ComboBox_GetCurSel(hCombo) + 1);

    // Get the input length
    Length = GetDlgItemInt(hDlg, IDC_INPUT_LENGTH, &bTranslated, FALSE);
    if(bTranslated == FALSE)
        Status = STATUS_UNSUCCESSFUL;
    if(Length > (ULONG)pData->cbFileInfoBuff)
        Length = (ULONG)pData->cbFileInfoBuff;

    if(NT_SUCCESS(Status))
    {
        hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if(hEvent == NULL)
            Status = STATUS_UNSUCCESSFUL;
    }

    if(NT_SUCCESS(Status))
    {
        Status = NtQueryDirectoryFile(pData->hFile,
                                      hEvent,
                                      NULL,
                                      NULL,
                                     &IoStatus,
                                      pData->pbFileInfoBuff,
                                      Length, 
                                      FileInfoClass,
                                      FALSE,
                                      NULL,
                                      TRUE);

        // If the operation is pending, we have to wait until it's complete
        if(Status == STATUS_PENDING)
        {
            WaitForSingleObject(hEvent, INFINITE);
            Status = IoStatus.Status;
        }
    }                                        

    // If succeeded, we have to fill the dialog with file info
    if(NT_SUCCESS(Status) || Status == STATUS_BUFFER_OVERFLOW)
        FillDialogWithFileInfo(hDlg, FileInfoClass);

    // Set the result status and return
    SetDlgItemText(hDlg, IDC_RESULT_STATUS, NtStatus2Text(Status));
    SetDlgItemInt(hDlg, IDC_RESULT_LENGTH, (UINT)IoStatus.Information, FALSE);
    if(hEvent != NULL)
        CloseHandle(hEvent);
    return TRUE;
}


static int OnSetInfoClick(HWND hDlg)
{
    FILE_INFORMATION_CLASS FileInfoClass;
    TPropSheetData * pData = (TPropSheetData *)GetWindowLongPtr(hDlg, DWLP_USER);
    IO_STATUS_BLOCK IoStatus = {0};
    NTSTATUS Status = STATUS_SUCCESS;
    ULONG Length = 0;
    HWND hCombo = GetDlgItem(hDlg, IDC_FILE_INFO_CLASS);
    BOOL bTranslated = FALSE;

    // Test if the handle is an NT file handle and get the file info class
    if(IsHandleValid(pData->hFile) == FALSE)
        return FALSE;
    FileInfoClass = (FILE_INFORMATION_CLASS)(ComboBox_GetCurSel(hCombo) + 1);

    // Get the length of the input data
    Length = GetDlgItemInt(hDlg, IDC_INPUT_LENGTH, &bTranslated, FALSE);
    if(bTranslated == FALSE)
        Status = STATUS_UNSUCCESSFUL;
    if(Length > (ULONG)pData->cbFileInfoBuff)
        Length = (ULONG)pData->cbFileInfoBuff;

    // Call NtSetInformationFile
    if(NT_SUCCESS(Status))
    {
        Status = NtSetInformationFile(pData->hFile,
                                     &IoStatus,
                                      pData->pbFileInfoBuff,
                                      Length,
                                      FileInfoClass);
    }                                        

    // Set the result status and return
    SetDlgItemText(hDlg, IDC_RESULT_STATUS, NtStatus2Text(Status));
    SetDlgItemInt(hDlg, IDC_RESULT_LENGTH, (UINT)IoStatus.Information, FALSE);
    return TRUE;
}

static int OnCommand(HWND hDlg, UINT nNotify, UINT nIDCtrl)
{
    // From an accelerator
    if(nNotify == 1 && nIDCtrl == ID_EDIT_LABEL)
        return OnEditLabel(hDlg);

    if(nNotify == BN_CLICKED)
    {
        switch(nIDCtrl)
        {
            case IDC_DEFAULT_LENGTH:
                return OnDefaultLengthClick(hDlg);

            case IDC_MAXIMUM_LENGTH:
                return OnMaximumLengthClick(hDlg);

            case IDC_QUERYINFO:
                return OnQueryInfoClick(hDlg);

            case IDC_QUERYDIR:
                return OnQueryDirClick(hDlg);

            case IDC_SETINFO:
                return OnSetInfoClick(hDlg);
        }
    }

    // If the selection has been changed, reflect it in the dialog
    if(nNotify == CBN_SELCHANGE && nIDCtrl == IDC_FILE_INFO_CLASS)
    {
        OnFileInfoClassChanged(hDlg);
        return TRUE;
    }

    return FALSE;
}

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

        case TVN_BEGINLABELEDIT:
            return OnBeginLabelEdit(hDlg, (NMTVDISPINFO *)pNMHDR);

        case TVN_ENDLABELEDIT:
            return OnEndLabelEdit(hDlg, (NMTVDISPINFO *)pNMHDR);
    }
    return FALSE;
}

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

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

        case WM_RELOADITEMS:
            return OnReloadItems(hDlg, (HWND)wParam, (HTREEITEM)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 + -