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

📄 charsample_ddkapp.cpp

📁 该源码是用DDK编写的WDM驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    )
{ 
    HWND    hWnd;
    TCHAR   str[MAX_STRING_LENGTH];
    DWORD   itemIndex;
    DWORD   error = ERROR_SUCCESS;
    DWORD   ii;
    BOOL    bInBufferEnable = TRUE;
    BOOL    bOutBufferEnable = TRUE;

    DEV_BROADCAST_DEVICEINTERFACE   notificationFilter;
    PDEV_BROADCAST_HDR              pHdr;

    switch (uMsg) 
    { 
    case WM_INITDIALOG: 

        // Initialize our device handle
        g_hDevice = INVALID_HANDLE_VALUE;
        
        notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
        notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        notificationFilter.dbcc_classguid = GUID_DEVINTERFACE_CHARSAMPLE_DDK;
        g_hInterfaceNotification = RegisterDeviceNotification(hDlg, &notificationFilter, 0);

        // Populate the list box
        CharSample_DDKEnumerateDevices(hDlg);

        // Setup our operation combo box
        hWnd = GetDlgItem(hDlg, IDC_OP_TYPE_COMBO);

        for(ii = 0; _tcscmp(g_TransferTypeArray[ii], _T("")); ii++)
        {
            // Add the transfer type string to the combo box
            SendMessage(hWnd, CB_ADDSTRING, 0, (LPARAM)g_TransferTypeArray[ii]);
        }

        // Initialize the window. 
        return 1; 

    case WM_DEVICECHANGE:
        if (((wParam & 0xC000) == 0x8000) && (lParam != NULL))
        {
            pHdr = (PDEV_BROADCAST_HDR)lParam;

            if (pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)
            {
            }
            else if (pHdr->dbch_devicetype == DBT_DEVTYP_HANDLE)
            {
            }

            // Populate the list box
            CharSample_DDKEnumerateDevices(hDlg);
        }
        break;

    case WM_COMMAND:

        switch (LOWORD(wParam)) 
        {

        case IDOK:
            EndDialog(hDlg, 0);
            PostQuitMessage(0); 
            break;

        case IDC_OPEN_BUTTON:

            // Open the selected device
            g_hDevice = CharSample_DDKOpenDevice(hDlg);
            if (g_hDevice == INVALID_HANDLE_VALUE)
            {
                CharSample_DDKOutputText(_T("Failed to open device, all options will be unavailable"));
                return 0;
            } 

            // If we opened the device, disable the open button, only one
            // open at a time in this simple app
            EnableWindow(GetDlgItem(hDlg, IDC_OPEN_BUTTON), FALSE);

            // Enable the close button
            EnableWindow(GetDlgItem(hDlg, IDC_CLOSE_BUTTON), TRUE);

            // Enable the operation combo box
            EnableWindow(GetDlgItem(hDlg, IDC_OPERATION_TYPE_STATIC), TRUE);
            EnableWindow(GetDlgItem(hDlg, IDC_OP_TYPE_COMBO), TRUE);

            // Clean up window-specific data objects. 
            return 0; 

        case IDC_CLOSE_BUTTON:

            // Open the selected device
            if (!CloseHandle(g_hDevice))
            {
                CharSample_DDKOutputText(_T("Failed to close device"));
                return 0;
            } 

            CharSample_DDKOutputText(_T("Device closed"));

            // Set our handle to an invalid state
            g_hDevice = INVALID_HANDLE_VALUE;

            // If we closed the device, disable the close button
            EnableWindow(GetDlgItem(hDlg, IDC_CLOSE_BUTTON), FALSE);

            // See if we have a current selection
            // Reflect the change in the current selection box 
            itemIndex = (DWORD)SendMessage(
                            GetDlgItem(hDlg, IDC_DEVICE_INSTANCE_LIST), 
                            LB_GETCURSEL, 
                            0, 
                            0
                            ); 
           
            // If we have a selection, enable the open button
            EnableWindow(GetDlgItem(hDlg, IDC_OPEN_BUTTON), TRUE);

            // Clear the selection in the current selection edit box
            SetDlgItemText(hDlg, IDC_SELECTED_DEVICE_EDIT, NULL); 

            // Disable the operation combo box
            EnableWindow(GetDlgItem(hDlg, IDC_OPERATION_TYPE_STATIC), FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_OP_TYPE_COMBO), FALSE);

            // Disable the input data pattern edit box
            EnableWindow(GetDlgItem(hDlg, IDC_IN_DATA_STATIC), FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_IN_DATA_EDIT), FALSE);

            // Disable the output data pattern edit box
            EnableWindow(GetDlgItem(hDlg, IDC_OUT_DATA_STATIC), FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_OUT_DATA_EDIT), FALSE);

            // Disable the input buffer size edit box
            EnableWindow(GetDlgItem(hDlg, IDC_IN_SIZE_STATIC), FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_IN_SIZE_EDIT), FALSE);

            // Disable the output buffer size edit box
            EnableWindow(GetDlgItem(hDlg, IDC_OUT_SIZE_STATIC), FALSE);
            EnableWindow(GetDlgItem(hDlg, IDC_OUT_SIZE_EDIT), FALSE);

            // Disable the execute button
            EnableWindow(GetDlgItem(hDlg, IDC_EXECUTE_BUTTON), FALSE);

            // Disable the cancel I/O button
            EnableWindow(GetDlgItem(hDlg, IDC_CANCEL_IO_BUTTON), FALSE);

            // remove current I/O selection
            SendMessage(GetDlgItem(hDlg, IDC_OP_TYPE_COMBO), CB_SETCURSEL, -1, 0);

            // Clean up window-specific data objects. 
            return 0; 

        case IDC_DEVICE_INSTANCE_LIST: 
            switch (HIWORD(wParam)) 
            { 
            case LBN_SELCHANGE: 

                hWnd = (HWND)lParam;

                // Reflect the change in the current selection box 
                itemIndex = (DWORD)SendMessage(hWnd, LB_GETCURSEL, 0, 0); 

                if ((itemIndex != LB_ERR) && (g_hDevice == INVALID_HANDLE_VALUE))
                {
                    // If we have a selection, enable the open button
                    // If we closed the device, disable the close button
                    EnableWindow(GetDlgItem(hDlg, IDC_OPEN_BUTTON), TRUE);
                }                

                return 0; 
            }
            break;

        case IDC_OP_TYPE_COMBO: 
            switch (HIWORD(wParam)) 
            { 
            case CBN_SELCHANGE:

                hWnd = (HWND)lParam;

                // Get the current selection
                itemIndex = (DWORD)SendMessage(hWnd, CB_GETCURSEL, 0, 0); 

                // Get the selection text
                SendMessage(hWnd, CB_GETLBTEXT, (WPARAM)itemIndex, (LPARAM)str); 

                // Enable the execute button
                EnableWindow(GetDlgItem(hDlg, IDC_EXECUTE_BUTTON), TRUE);

                // Enable/disable the input data pattern edit box
                EnableWindow(GetDlgItem(hDlg, IDC_IN_DATA_STATIC), bInBufferEnable);
                EnableWindow(GetDlgItem(hDlg, IDC_IN_DATA_EDIT), bInBufferEnable);

                // Enable/disable the input buffer size edit box
                EnableWindow(GetDlgItem(hDlg, IDC_IN_SIZE_STATIC), bInBufferEnable);
                EnableWindow(GetDlgItem(hDlg, IDC_IN_SIZE_EDIT), bInBufferEnable);

                // Enable/disable the output data pattern edit box
                EnableWindow(GetDlgItem(hDlg, IDC_OUT_DATA_STATIC), bOutBufferEnable);
                EnableWindow(GetDlgItem(hDlg, IDC_OUT_DATA_EDIT), bOutBufferEnable);

                // Enable/disable the output buffer size edit box
                EnableWindow(GetDlgItem(hDlg, IDC_OUT_SIZE_STATIC), bOutBufferEnable);
                EnableWindow(GetDlgItem(hDlg, IDC_OUT_SIZE_EDIT), bOutBufferEnable);

                return 0;
            }
            break;

        case IDC_EXECUTE_BUTTON: 

            CharSample_DDKExecuteIo(hDlg);

            // Enable the cancel I/O button
            EnableWindow(GetDlgItem(hDlg, IDC_CANCEL_IO_BUTTON), TRUE);
            break;

        case IDC_CANCEL_IO_BUTTON: 

            CancelIo(g_hDevice);

            // Disable the cancel I/O button
            EnableWindow(GetDlgItem(hDlg, IDC_CANCEL_IO_BUTTON), FALSE);
            break;

        default: 
            break;
        }
        break;

    case WM_CLOSE:

        if (g_hDevice != INVALID_HANDLE_VALUE)
        {
            CloseHandle(g_hDevice);
            g_hDevice = INVALID_HANDLE_VALUE;
        }

        // Terminate our I/O completion thread
        SetEvent(g_hIoCompletionThreadTerminationEvent);
        WaitForSingleObject(g_hIoCompletionThread, INFINITE);

        UnregisterDeviceNotification(g_hInterfaceNotification);
        EndDialog(hDlg, 0);
        PostQuitMessage(0); 

        // Clean up window-specific data objects. 
        return 0; 

    // Process other messages. 

    default: 
        return 0;
    } 

    return 0; 
} 

///////////////////////////////////////////////////////////////////////////////////////////////////
//  WinMain
//      Application entry point
//
//  Arguments:
//      IN  hInstance
//              Handle to current instance
//
//      IN  hPrevInstance
//              unused
//
//      IN  lpCmdLine
//              command line
//
//      IN  nCmdShow
//              unused
//
//  Return Value:
//      status.
//
int APIENTRY WinMain(
    HINSTANCE   hInstance, 
    HINSTANCE   hPrevInstance, 
    LPSTR       lpCmdLine, 
    int         nCmdShow
                )
{
    // do init stuff
    InitCommonControls();

    // Initialize the global io list
    g_IoList.Next = &g_IoList;
    g_IoList.Previous = &g_IoList;

    // Initialize the global list lock
    InitializeCriticalSection(&g_IoListLock);

    // Initialize the global I/O completion thread termination event.
    // This is a manual reset event
    g_hIoCompletionThreadTerminationEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    UINT nIoCompletionThreadID;

    // Start a new thread to handle I/O completion
    g_hIoCompletionThread = (HANDLE)_beginthreadex(
        NULL, 
        0, 
        CharSample_DDKIoCompletionThread, 
        g_hIoCompletionThreadTerminationEvent, 
        0, 
        &nIoCompletionThreadID
        );

    // start dialog box
    int retVal = (int)DialogBox(hInstance, _T("CHARSAMPLE_DDKAPP"), NULL, (DLGPROC)CharSample_DDKMainDlgProc);

    // Free allocated resources
    DeleteCriticalSection(&g_IoListLock);
    CloseHandle(g_hIoCompletionThreadTerminationEvent);

    CloseHandle(g_hIoCompletionThread);

    return retVal;
} 

⌨️ 快捷键说明

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