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

📄 charsampleiorw.cpp

📁 一个简单的wdm驱动开发实例,可以大概看开发流程作为入门
💻 CPP
字号:
//
// CharSampleIorw.cpp 
//
// Generated by C DriverWizard 3.1.0 (Build 1722)
// Requires DDK Only
// File created on 1/23/2009
//
#include "CharSampleApp.h"

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSampleIOCTL_800CompleteCallback
//      Callback to complete application processing of I/O request
//
//  Arguments:
//      IN  Context
//              Our I/O request item
//
//  Return Value:
//      none
//
VOID CharSampleIOCTL_800CompleteCallback(PVOID Context)
{
    PCHARSAMPLE_LIST_ITEM ioItem = (PCHARSAMPLE_LIST_ITEM)Context;

    CharSampleOutputText(
        _T("Executed IOCTL_800 request: in buffer size (%d), out buffer size (%d), return length (%d) error (%d)"), 
        ioItem->InSize, 
        ioItem->OutSize, 
        ioItem->ReturnLength, 
        ioItem->Error
        );

    // Dump the output buffer
    CharSampleOutputBuffer(ioItem->OutBuffer, ioItem->ReturnLength);

    // Free our buffer memory
    free(ioItem->InBuffer);
    free(ioItem->OutBuffer);

    // Close our overlapped event handle
    CloseHandle(ioItem->IoOverlapped.hEvent);

    // Free our ioItem memory
    free(ioItem);

    return;
}

//////////////////////////////////////////////////////////////////////////////
//  CharSampleExecuteIo
//      Routine to execute the user chosen I/O type
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      status.
//
ULONG CharSampleExecuteIo(HWND hDlg)
{
    TCHAR               str[MAX_STRING_LENGTH];
    HWND                hWnd;
    ULONG               error = ERROR_SUCCESS;
    DWORD               inPattern;
    DWORD               outPattern;
    DWORD               ii;
    DWORD               itemIndex;
    PCHARSAMPLE_LIST_ITEM  ioItem;

    CharSampleOutputText(_T("CharSampleExecuteIo++"));

    // Use do/while to handle cleanup
    do
    {
        // Allocate a list entry
        ioItem = (PCHARSAMPLE_LIST_ITEM)malloc(sizeof(CHARSAMPLE_LIST_ITEM));
        if (ioItem == NULL)
        {
            CharSampleOutputText(_T("Failed to create read I/O list entry, ReadFile not executed"));
            error = ERROR_OUTOFMEMORY;
            break;
        }

        ZeroMemory(ioItem, sizeof(CHARSAMPLE_LIST_ITEM));

        // Setup the overlapped struct

        // Zero the overlapped structure.  Make sure this is done anytime
        // an overlapped structure is reused as well.
        ZeroMemory(&ioItem->IoOverlapped, sizeof(OVERLAPPED));

        // Setup the event
        ioItem->IoOverlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        if (ioItem->IoOverlapped.hEvent == NULL)
        {
            error = GetLastError();
            CharSampleOutputText(_T("NewTextExecuteIo failed to allocate overlapped event error (%d)"), error);
            break;
        }

        // Get the buffer sizes
        GetDlgItemText(hDlg, IDC_IN_SIZE_EDIT, str, MAX_STRING_LENGTH); 
        ioItem->InSize = _ttol(str);

        GetDlgItemText(hDlg, IDC_OUT_SIZE_EDIT, str, MAX_STRING_LENGTH); 
        ioItem->OutSize = _ttol(str);

        // Get the data patterns
        GetDlgItemText(hDlg, IDC_IN_DATA_EDIT, str, MAX_STRING_LENGTH); 
        (VOID)_stscanf(str, _T("%x"), &inPattern);

        GetDlgItemText(hDlg, IDC_OUT_DATA_EDIT, str, MAX_STRING_LENGTH); 
        (VOID)_stscanf(str, _T("%x"), &outPattern);

        // Setup the transfer
        hWnd = GetDlgItem(hDlg, IDC_OP_TYPE_COMBO);

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

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

        if ((!_tcscmp(str, _T("IOCTL_800"))) && ((ioItem->OutSize > 0) || (ioItem->InSize > 0)))
        {
            // Setup the in buffer if specified
            if (ioItem->InSize > 0)
            {
                ioItem->InBuffer = (PCHAR)malloc(ioItem->InSize);
                if (ioItem->InBuffer == NULL)
                {
                    error = ERROR_OUTOFMEMORY;
                    CharSampleOutputText(_T("Failed to create in buffer, IOCTL_800 not executed"));
                    break;
                }

                // set the in buffer data
                for (ii = 0; ii < ioItem->InSize; ii += sizeof(DWORD))
                {
                    CopyMemory(
                        ioItem->InBuffer + ii, 
                        &inPattern, 
                        min(sizeof(DWORD), ioItem->InSize - ii)
                        );
                }
            }

            // Setup the out buffer if specified
            if (ioItem->OutSize > 0)
            {
                ioItem->OutBuffer = (PCHAR)malloc(ioItem->OutSize);
                if (ioItem->OutBuffer == NULL)
                {
                    error = ERROR_OUTOFMEMORY;
                    CharSampleOutputText(_T("Failed to create out buffer, IOCTL_800 not executed"));
                    break;
                }

                // set the out buffer data
                for (ii = 0; ii < ioItem->OutSize; ii += sizeof(DWORD))
                {
                    CopyMemory(
                        ioItem->OutBuffer + ii, 
                        &outPattern, 
                        min(sizeof(DWORD), ioItem->OutSize - ii)
                        );
                }
            }

            if ((!DeviceIoControl(
                            g_hDevice, 
                            IOCTL_800,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer, 
                            ioItem->OutSize, 
                            NULL, 
                            &ioItem->IoOverlapped
                            )) && 
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                CharSampleOutputText(_T("IOCTL_800 failed with error (%d)"), error);
                break;
            }

            // Setup the entry
            ioItem->Callback = CharSampleIOCTL_800CompleteCallback;

            CharSampleOutputText(_T("Adding entry to list"));

            // Get our list protection
            EnterCriticalSection(&g_IoListLock);

            // Add this entry to the end of the list
            ioItem->Next = &g_IoList;
            ioItem->Previous = g_IoList.Previous;

            g_IoList.Previous->Next = ioItem;
            g_IoList.Previous = ioItem;

            // Drop our list protection
            LeaveCriticalSection(&g_IoListLock);

            CharSampleOutputText(_T("Added entry to list"));

            break;
        }
    }
    while (FALSE);

    if (error != ERROR_SUCCESS)
    {
        // Free our buffers
        if (ioItem->InBuffer != NULL)
        {
            free(ioItem->InBuffer);
        }

        if (ioItem->OutBuffer != NULL)
        {
            free(ioItem->OutBuffer);
        }

        // Close our overlapped event handle
        if (ioItem->IoOverlapped.hEvent != NULL)
        {
            CloseHandle(ioItem->IoOverlapped.hEvent);
        }

        // Free the ioItem memory
        free(ioItem);
    }

    CharSampleOutputText(_T("CharSampleExecuteIo--"));

    return error;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSampleIoCompletionThread
//      Thread used complete processing of overlapped I/O requests.
//
//  Arguments:
//      IN  Context
//              Not used
//
//  Return Value:
//      Thread exit value
//
UINT __stdcall CharSampleIoCompletionThread(PVOID Context)
{
    PCHARSAMPLE_LIST_ITEM ioEntry;
    PCHARSAMPLE_LIST_ITEM tempEntry;

    HANDLE hIoCompletionThreadTerminationEvent = (HANDLE)Context;

    CharSampleOutputText(_T("IoCompletionThread"));

    while (1)
    {
        // Get our list protection
        EnterCriticalSection(&g_IoListLock);

        ioEntry = g_IoList.Next;

        // If the list is populated then go through and wait on each I/O to complete
        while (ioEntry != &g_IoList)
        {
            // Drop our list protection
            LeaveCriticalSection(&g_IoListLock);

            CharSampleOutputText(_T("I/O loop"));

            ioEntry->Error = ERROR_SUCCESS;

            assert(WaitForSingleObject(ioEntry->IoOverlapped.hEvent, INFINITE) == WAIT_OBJECT_0);
            assert(HasOverlappedIoCompleted(&ioEntry->IoOverlapped));

            if (!GetOverlappedResult(
                    g_hDevice, 
                    &ioEntry->IoOverlapped, 
                    &ioEntry->ReturnLength, 
                    TRUE
                    ))
            {
                ioEntry->Error = GetLastError();
            }

            CharSampleOutputText(_T("I/O for entry completed"));

            // Get our list protection
            EnterCriticalSection(&g_IoListLock);

            // Remove the entry from the list and get the next entry
            tempEntry = ioEntry;
            ioEntry = ioEntry->Next;

            tempEntry->Previous->Next = tempEntry->Next;
            tempEntry->Next->Previous = tempEntry->Previous;


            // Call the I/O callback
            tempEntry->Callback(tempEntry);
        }

        // Drop our list protection
        LeaveCriticalSection(&g_IoListLock);

        if (WaitForSingleObject(hIoCompletionThreadTerminationEvent, 0) == WAIT_OBJECT_0)
        {
            break;
        }

        Sleep(500);
    }

    return 0;
}

⌨️ 快捷键说明

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