基于umdf2的驱动程序

源码下载:https://download.csdn.net/download/mao0514/88915667 

win10 64位系统+vs2019+wdk

inf安装:

 



VOID
UMDF2Driver1EvtIoDeviceControl(
    _In_ WDFQUEUE Queue,
    _In_ WDFREQUEST Request,
    _In_ size_t OutputBufferLength,
    _In_ size_t InputBufferLength,
    _In_ ULONG IoControlCode
    )
/*++

Routine Description:

    This event is invoked when the framework receives IRP_MJ_DEVICE_CONTROL request.

Arguments:

    Queue -  Handle to the framework queue object that is associated with the
             I/O request.

    Request - Handle to a framework request object.

    OutputBufferLength - Size of the output buffer in bytes

    InputBufferLength - Size of the input buffer in bytes

    IoControlCode - I/O control code.

Return Value:

    VOID

--*/
{
    TraceEvents(TRACE_LEVEL_INFORMATION, 
                TRACE_QUEUE, 
                "%!FUNC! Queue 0x%p, Request 0x%p OutputBufferLength %d InputBufferLength %d IoControlCode %d", 
                Queue, Request, (int) OutputBufferLength, (int) InputBufferLength, IoControlCode);
    CHAR		n, c[] = "壹贰叁肆伍陆柒捌玖零";
    PVOID buffer, outbuf;
    NTSTATUS  status = 0;

    switch (IoControlCode) 
    {

        case CHAR_IOCTL_800:
            if (InputBufferLength == 0 || OutputBufferLength == 0)
            {
                WdfRequestComplete(Request, STATUS_INVALID_PARAMETER);
                break;
            }
            else
            {
                status = WdfRequestRetrieveInputBuffer(Request, 1, &buffer, NULL);
                if (!NT_SUCCESS(status))
                {
                    WdfRequestComplete(Request, STATUS_UNSUCCESSFUL);
                    break;
                }

                status = WdfRequestRetrieveOutputBuffer(Request, 2, &outbuf/*outbuf指针的指针*/, NULL);
                if (!NT_SUCCESS(status))
                {
                    WdfRequestComplete(Request, STATUS_UNSUCCESSFUL);
                    break;
                }
                n = *(unsigned char*)buffer;
                KdPrint(("read base0 %x %x", 0, *(unsigned int*)buffer));
                memcpy((unsigned char*)outbuf, (unsigned char*)(c+2*n),2);
                WdfRequestCompleteWithInformation(Request, STATUS_SUCCESS, 2);

            }
       
            break;
      default:

        status = STATUS_INVALID_DEVICE_REQUEST;
        WdfRequestCompleteWithInformation(Request, status, 0);
        break;
    }
   // WdfRequestComplete(Request, STATUS_SUCCESS);

    return;
}

应用端:


//pub.h
#include <initguid.h>

DEFINE_GUID(GUID_DEVINTERFACE_UMDF2Driver1,
    0xd63d7565, 0xb448, 0x45cb, 0xa2, 0x4b, 0xe7, 0x32, 0x65, 0x1d, 0x45, 0x29);
// {d63d7565-b448-45cb-a24b-e732651d4529}


#define CHAR_IOCTL_800 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS)

//api
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <winioctl.h>
//

#pragma comment(lib,"setupapi.lib")
PCHAR
GetDevicePath(
    IN  LPGUID InterfaceGuid
)
{
    HDEVINFO HardwareDeviceInfo;
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
    PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData = NULL;
    ULONG Length, RequiredLength = 0;
    BOOL bResult;

    HardwareDeviceInfo = SetupDiGetClassDevs(
        InterfaceGuid,
        NULL,
        NULL,
        (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));

    if (HardwareDeviceInfo == INVALID_HANDLE_VALUE) {
        printf("SetupDiGetClassDevs failed!\n");
        exit(1);
    }

    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);

    bResult = SetupDiEnumDeviceInterfaces(HardwareDeviceInfo,
        0,
        InterfaceGuid,
        0,
        &DeviceInterfaceData);

    if (bResult == FALSE) {
        /*
                LPVOID lpMsgBuf;

                if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                                  FORMAT_MESSAGE_FROM_SYSTEM |
                                  FORMAT_MESSAGE_IGNORE_INSERTS,
                                  NULL,
                                  GetLastError(),
                                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                                  (LPSTR) &lpMsgBuf,
                                  0,
                                  NULL
                                  )) {

                    printf("Error: %s", (LPSTR)lpMsgBuf);
                    LocalFree(lpMsgBuf);
                }
        */
        printf("SetupDiEnumDeviceInterfaces failed.\n");

        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        exit(1);
    }

    SetupDiGetDeviceInterfaceDetail(
        HardwareDeviceInfo,
        &DeviceInterfaceData,
        NULL,
        0,
        &RequiredLength,
        NULL
    );

    DeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)LocalAlloc(LMEM_FIXED, RequiredLength);

    if (DeviceInterfaceDetailData == NULL) {
        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        printf("Failed to allocate memory.\n");
        exit(1);
    }

    DeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);

    Length = RequiredLength;

    bResult = SetupDiGetDeviceInterfaceDetail(
        HardwareDeviceInfo,
        &DeviceInterfaceData,
        DeviceInterfaceDetailData,
        Length,
        &RequiredLength,
        NULL);

    if (bResult == FALSE) {
        /*
                LPVOID lpMsgBuf;

                if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                              FORMAT_MESSAGE_FROM_SYSTEM |
                              FORMAT_MESSAGE_IGNORE_INSERTS,
                              NULL,
                              GetLastError(),
                              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                              (LPSTR) &lpMsgBuf,
                              0,
                              NULL
                              )) {

                    MessageBox(NULL, (LPCTSTR) lpMsgBuf, "Error", MB_OK);
                    LocalFree(lpMsgBuf);
                }
        */
        printf("Error in SetupDiGetDeviceInterfaceDetail\n");

        SetupDiDestroyDeviceInfoList(HardwareDeviceInfo);
        LocalFree(DeviceInterfaceDetailData);
        exit(1);
    }

    return DeviceInterfaceDetailData->DevicePath;

}


HANDLE hDevice = INVALID_HANDLE_VALUE;

void CMFCApplication1Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
    PCHAR  DevicePath;

    DevicePath = GetDevicePath((LPGUID)&GUID_DEVINTERFACE_UMDF2Driver1);

    hDevice = CreateFile(DevicePath,
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
    if (hDevice == INVALID_HANDLE_VALUE) {
        MessageBox("err.\n");
        return;
    }

    MessageBox("OK.\n");
}


void CMFCApplication1Dlg::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
    CHAR	bufInput[1];	// Input to device
    CHAR	bufOutput[2];	// Output from device
    ULONG	nOutput;	// Count written to bufOutput


    bufInput[0] = 2;
    if (!DeviceIoControl(hDevice,
        CHAR_IOCTL_800,
        bufInput,
        1,
        bufOutput,
        2,
        &nOutput,
        NULL)
        )
    {
        MessageBox("err.\n");
        return;
    }
    MessageBox(bufOutput);
    CloseHandle(hDevice);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛毛虫的爹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值