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

📄 computer00usbiorw.cpp

📁 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, EP1_READ 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,
                            EP1_READ,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00UsbOutputText(_T("EP1_READ failed with error (%d)"), error);
                break;
            }

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

            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("EP1_WRITE"))) && ((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, EP1_WRITE 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, EP1_WRITE 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,
                            EP1_WRITE,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00UsbOutputText(_T("EP1_WRITE failed with error (%d)"), error);
                break;
            }

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

            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("EP2_READ"))) && ((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, EP2_READ 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, EP2_READ 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,
                            EP2_READ,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00UsbOutputText(_T("EP2_READ failed with error (%d)"), error);
                break;
            }

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

            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("EP2_WRITE"))) && ((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, EP2_WRITE 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, EP2_WRITE 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,
                            EP2_WRITE,
                            ioItem->InBuffer,
                            ioItem->InSize,
                            ioItem->OutBuffer,
                            ioItem->OutSize,
                            NULL,
                            &ioItem->IoOverlapped
                            )) &&
                 (GetLastError() != ERROR_IO_PENDING))
            {
                error = GetLastError();
                Computer00UsbOutputText(_T("EP2_WRITE failed with error (%d)"), error);
                break;
            }

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

            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 + -