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

📄 computer00usbiorw.cpp

📁 使用driverstudio开发的一个USB驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                {
                    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;
                    Computer00USBOutputText(_T("Failed to create out buffer, READ_ENDP1 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,
                            READ_ENDP1,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00USBOutputText(_T("READ_ENDP1 failed with error (%d)"), error);
                break;
            }

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

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

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

            break;
        }

        if ((!_tcscmp(str, _T("WRITE_ENDP1"))) && ((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;
                    Computer00USBOutputText(_T("Failed to create in buffer, WRITE_ENDP1 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;
                    Computer00USBOutputText(_T("Failed to create out buffer, WRITE_ENDP1 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,
                            WRITE_ENDP1,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00USBOutputText(_T("WRITE_ENDP1 failed with error (%d)"), error);
                break;
            }

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

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

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

            break;
        }

        if ((!_tcscmp(str, _T("READ_ENDP2"))) && ((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;
                    Computer00USBOutputText(_T("Failed to create in buffer, READ_ENDP2 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;
                    Computer00USBOutputText(_T("Failed to create out buffer, READ_ENDP2 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,
                            READ_ENDP2,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00USBOutputText(_T("READ_ENDP2 failed with error (%d)"), error);
                break;
            }

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

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

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

            break;
        }

        if ((!_tcscmp(str, _T("WRITE_ENDP2"))) && ((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;
                    Computer00USBOutputText(_T("Failed to create in buffer, WRITE_ENDP2 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;
                    Computer00USBOutputText(_T("Failed to create out buffer, WRITE_ENDP2 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,
                            WRITE_ENDP2,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00USBOutputText(_T("WRITE_ENDP2 failed with error (%d)"), error);
                break;
            }

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

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

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

    Computer00USBOutputText(_T("Computer00USBExecuteIo--"));

    return error;
}

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

    HANDLE hIoCompletionThreadTerminationEvent = (HANDLE)Context;

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

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

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