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

📄 charsample_ddkapp.cpp

📁 该源码是用DDK编写的WDM驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//
// CharSample_DDKApp.cpp  : Defines the entry point for the application.
//
// Generated by C DriverWizard 3.1.0 (Build 1722)
// Requires DDK Only
// File created on 7/11/2007
//
#include "CharSample_DDKApp.h"

// Array of possible transfer types
PTCHAR g_TransferTypeArray[] = 
{
    _T("IOCTL_800"),
    _T("")
};

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

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

    va_start(vaList, Format);

    _vstprintf(str, Format, vaList);

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

    va_end(vaList);

    return;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSample_DDKOutputBuffer
//      method to output text in the output window
//
//  Arguments:
//      IN  Buffer
//              Data Buffer
//
//      IN  Size
//              Size of Data Buffer
//
//  Return Value:
//      None.
//
VOID CharSample_DDKOutputBuffer(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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSample_DDKEnumerateDevices
//      Finds devices associated with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      status.
//
DWORD CharSample_DDKEnumerateDevices(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();
        CharSample_DDKOutputText(_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_CHARSAMPLE_DDK, 
                    NULL, 
                    NULL,
                    DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
                    );     
    if (hDeviceInfo == INVALID_HANDLE_VALUE) 
    {
        lastError = GetLastError();
        CharSample_DDKOutputText(_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_CHARSAMPLE_DDK,
            ii, 
            &interfaceData);
         ++ii) 
    {
        // Found our device instance
        if (!SetupDiGetDeviceInterfaceDetail(
                hDeviceInfo, 
                &interfaceData, 
                NULL, 
                0,
                &bufferSize, 
                NULL)) 
        {
            if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) 
            {
                CharSample_DDKOutputText(_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) 
        {
            CharSample_DDKOutputText(_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)) 
        {
            CharSample_DDKOutputText(_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)
    {
        CharSample_DDKOutputText(_T("No devices found"));
    }

    return ERROR_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  CharSample_DDKOpenDevice
//      Opens the nth device found with the given interface class
//
//  Arguments:
//      IN  hDlg
//              Handle to dialog
//
//  Return Value:
//      Handle to device.
//
HANDLE CharSample_DDKOpenDevice(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) 
    {
        CharSample_DDKOutputText(_T("Error: CreateFile failed for device %s (%d)\n"), path, GetLastError());
        return INVALID_HANDLE_VALUE;
    }

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

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

    return hDev;
}

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

⌨️ 快捷键说明

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