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

📄 driverb_syniorw.cpp

📁 基于DDK的驱动间同步调用测试示例程序,DriverA是目标驱动,DriverB是主驱动,test是MFC测试示例
💻 CPP
字号:
//
// DriverB_SynIorw.cpp
//
// Generated by C DriverWizard 3.2.0 (Build 2485)
// Requires DDK Only
// File created on 3/12/2009
//
#include "DriverB_SynApp.h"

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

    DriverB_SynOutputText(
        _T("Executed ReadFile: buffer size (%d), return length (%d) error (%d)"),
        ioItem->OutSize,
        ioItem->ReturnLength,
        ioItem->Error
        );

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

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

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

    // Free our ioItem memory
    free(ioItem);

    return;
}

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

    DriverB_SynOutputText(
        _T("Executed WriteFile: buffer size (%d), return length (%d) error (%d)"),
        ioItem->InSize,
        ioItem->ReturnLength,
        ioItem->Error
        );

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

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

    // Free our ioItem memory
    free(ioItem);

    return;
}

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

    DriverB_SynOutputText(_T("DriverB_SynExecuteIo++"));

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

        ZeroMemory(ioItem, sizeof(DRIVERB_SYN_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();
            DriverB_SynOutputText(_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("ReadFile"))) && (ioItem->OutSize > 0))
        {
            // Setup the read buffer
            ioItem->OutBuffer = (PCHAR)malloc(ioItem->OutSize);
            if (ioItem->OutBuffer == NULL)
            {
                DriverB_SynOutputText(_T("Failed to create read buffer, ReadFile not executed"));
                error = ERROR_OUTOFMEMORY;
                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 ((!ReadFile(
                    g_hDevice,
                    ioItem->OutBuffer,
                    ioItem->OutSize,
                    NULL,
                    &ioItem->IoOverlapped)) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                DriverB_SynOutputText(_T("ReadFile failed with error (%d)"), error);
                break;
            }

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

            DriverB_SynOutputText(_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);

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

            break;
        }

        if ((!_tcscmp(str, _T("WriteFile"))) && (ioItem->InSize > 0))
        {
            // Setup the write buffer
            ioItem->InBuffer = (PCHAR)malloc(ioItem->InSize);
            if (ioItem->InBuffer == NULL)
            {
                DriverB_SynOutputText(_T("Failed to create write buffer, WriteFile not executed"));
                error = ERROR_OUTOFMEMORY;
                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)
                    );
            }

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

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

            DriverB_SynOutputText(_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);

            DriverB_SynOutputText(_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);
    }

    DriverB_SynOutputText(_T("DriverB_SynExecuteIo--"));

    return error;
}

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

    HANDLE hIoCompletionThreadTerminationEvent = (HANDLE)Context;

    DriverB_SynOutputText(_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);

            DriverB_SynOutputText(_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();
            }

            DriverB_SynOutputText(_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 + -