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

📄 computer00usbapp.cpp

📁 使用driverstudio开发的一个USB驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// Computer00USBApp.cpp  : Defines the entry point for the application.
//
// Generated by C DriverWizard 3.2.0 (Build 2485)
// Requires DDK Only
// File created on 3/10/2008
//
#include "Computer00USBApp.h"

// Array of possible transfer types
PTCHAR g_TransferTypeArray[] =
{
    _T("ReadFile"),
    _T("WriteFile"),
    _T("READ_ENDP1"),
    _T("WRITE_ENDP1"),
    _T("READ_ENDP2"),
    _T("WRITE_ENDP2"),
    _T("")
};

HANDLE              g_hDevice = INVALID_HANDLE_VALUE;
HDEVNOTIFY          g_hInterfaceNotification = NULL;
COMPUTER00USB_LIST_ITEM   g_IoList;
CRITICAL_SECTION    g_IoListLock;
HANDLE              g_hIoCompletionThreadTerminationEvent;
HANDLE              g_hIoCompletionThread;

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBOutputText
//      method to output text in the output window
//
//  Arguments:
//      IN  Format
//              Text format to print to output window
//
//  Return Value:
//      None.
//
VOID Computer00USBOutputText(LPCTSTR Format, ...)
{
    TCHAR       str[MAX_STRING_LENGTH];
    va_list     vaList;

    va_start(vaList, Format);

    _vstprintf(str, Format, vaList);

    OutputDebugString(_T("Computer00USB: "));
    OutputDebugString(str);
    OutputDebugString(_T("\n"));

    va_end(vaList);

    return;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBOutputBuffer
//      method to output text in the output window
//
//  Arguments:
//      IN  Buffer
//              Data Buffer
//
//      IN  Size
//              Size of Data Buffer
//
//  Return Value:
//      None.
//
VOID Computer00USBOutputBuffer(PVOID Buffer, ULONG Size)
{
    TCHAR       str[MAX_STRING_LENGTH];
    LONG        length = (LONG)Size;
    PUCHAR      p = (PUCHAR)Buffer;
    TCHAR       data[MAX_STRING_LENGTH];
    TCHAR       rawData[MAX_STRING_LENGTH];
    ULONG       i;
    ULONG       j;

    for (i = 0; i < Size; i += 16)
    {
        ZeroMemory(str, sizeof(str));
        ZeroMemory(rawData, sizeof(rawData));

        _stprintf(str, _T("%04.4X  "), i);

        for (j = 0; j < 16; ++j, ++p)
        {
            if (length > 0)
            {
                _stprintf(data, _T("%02X "), *p);
            }
            else
            {
                _stprintf(data, _T("   "));
            }

            _tcsncat(str, data, 3);

            if (length > 0)
            {
                TCHAR c = (TCHAR)(*p);

                if (_istalnum(_TUCHAR(c)))
                {
                    _stprintf(data, _T("%c"), c);
                }
                else
                {
                    _tcsncat(rawData, _T("?"), 1);
                }

                --length;
            }
        }

        _tcsncat(str, _T("  "), 2);
        _tcsncat(str, rawData, 16);

        OutputDebugString(str);
        OutputDebugString(_T("\n"));
    }

    return;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBEnumerateDevices
//      Finds devices associated with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      status.
//
DWORD Computer00USBEnumerateDevices(HWND hDlg)
{
    HWND                                hList;
    DWORD                               lastError;
    LONG                                ii = 0;
    HDEVINFO                            hDeviceInfo;
    DWORD                               bufferSize;
    SP_DEVICE_INTERFACE_DATA            interfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA    deviceDetail;

    // Get the handle to the device instance list box
    hList = GetDlgItem(hDlg, IDC_DEVICE_INSTANCE_LIST);
    if (hList == NULL)
    {
        lastError = GetLastError();
        Computer00USBOutputText(_T("GetDlgItem failed, GetLastError() = %d"), lastError);
        return lastError;
    }

    // Clear the instance list box
    SendMessage(hList, LB_RESETCONTENT, 0, 0);

    // Find all devices that have our interface
    hDeviceInfo = SetupDiGetClassDevs(
                    (LPGUID)&GUID_DEVINTERFACE_COMPUTER00USB,
                    NULL,
                    NULL,
                    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
                    );
    if (hDeviceInfo == INVALID_HANDLE_VALUE)
    {
        lastError = GetLastError();
        Computer00USBOutputText(_T("SetupDiGetClassDevs failed, GetLastError() = %d"), lastError);
        return lastError;
    }

    // Setup the interface data struct
    interfaceData.cbSize = sizeof(interfaceData);

    for (ii = 0;
         SetupDiEnumDeviceInterfaces(
            hDeviceInfo,
            NULL,
            (LPGUID)&GUID_DEVINTERFACE_COMPUTER00USB,
            ii,
            &interfaceData);
         ++ii)
    {
        // Found our device instance
        if (!SetupDiGetDeviceInterfaceDetail(
                hDeviceInfo,
                &interfaceData,
                NULL,
                0,
                &bufferSize,
                NULL))
        {
            if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
            {
                Computer00USBOutputText(_T("Error: couldn't get interface detail, (%d)"), GetLastError());

                continue;
            }
        }

        // Allocate a big enough buffer to get detail data
        deviceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(bufferSize);
        if (deviceDetail == NULL)
        {
            Computer00USBOutputText(_T("Error: Buffer allocation failed"));
            continue;
        }

        // Setup the device interface struct
        deviceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

        // Try again to get the device interface detail info
        if (!SetupDiGetDeviceInterfaceDetail(
                hDeviceInfo,
                &interfaceData,
                deviceDetail,
                bufferSize,
                NULL,
                NULL))
        {
            Computer00USBOutputText(_T("Error: SetupDiGetDeviceInterfaceDetail failed (%d)"), GetLastError());
            free(deviceDetail);
            continue;
        }

        // Add device instance info to list
        SendMessage(
            hList,
            LB_ADDSTRING,
            0,
            (LPARAM)deviceDetail->DevicePath
            );

        // Free our allocated buffer
        free(deviceDetail);
    }

    SetupDiDestroyDeviceInfoList(hDeviceInfo);

    if (ii == 0)
    {
        Computer00USBOutputText(_T("No devices found"));
    }

    return ERROR_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBOpenDevice
//      Opens the nth device found with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      Handle to device.
//
HANDLE Computer00USBOpenDevice(HWND hDlg)
{
    HWND    hList;
    HANDLE  hDev;
    TCHAR   path[MAX_STRING_LENGTH];
    DWORD   itemIndex;

    // Get handle to device instance list box
    hList = GetDlgItem(hDlg, IDC_DEVICE_INSTANCE_LIST);

    // Find the current selection
    itemIndex = (DWORD)SendMessage(hList, LB_GETCURSEL, 0, 0);

    // Get the path string from the list box
    (LPTSTR)SendMessage(
                hList,
                LB_GETTEXT,
                itemIndex,
                (LPARAM)path);

    // Open handle to device
    hDev = CreateFile(
                 path,
                 GENERIC_READ | GENERIC_WRITE,
                 FILE_SHARE_READ | FILE_SHARE_WRITE,
                 NULL,
                 OPEN_EXISTING,
                 FILE_FLAG_OVERLAPPED,
                 0
                 );

    if (hDev == INVALID_HANDLE_VALUE)
    {
        Computer00USBOutputText(_T("Error: CreateFile failed for device %s (%d)\n"), path, GetLastError());
        return INVALID_HANDLE_VALUE;
    }

    Computer00USBOutputText(_T("Opened device %s"), path);

    // Reflect the selection in the current selection edit box
    SetDlgItemText(hDlg, IDC_SELECTED_DEVICE_EDIT, path);

    return hDev;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBMainDlgProc
//      Message loop
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//      IN  uMsg
//              Message id
//
//      IN  wParam
//              WPARAM
//
//      IN  lParam
//              LPARAM
//
//  Return Value:
//      status.
//
LRESULT CALLBACK Computer00USBMainDlgProc(
    HWND    hDlg,
    UINT    uMsg,
    WPARAM  wParam,
    LPARAM  lParam
    )
{

⌨️ 快捷键说明

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