源码下载: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 theI/O request.Request - Handle to a framework request object.OutputBufferLength - Size of the output buffer in bytesInputBufferLength - Size of the input buffer in bytesIoControlCode - 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 deviceCHAR bufOutput[2]; // Output from deviceULONG nOutput; // Count written to bufOutputbufInput[0] = 2;if (!DeviceIoControl(hDevice,CHAR_IOCTL_800,bufInput,1,bufOutput,2,&nOutput,NULL)){MessageBox("err.\n");return;}MessageBox(bufOutput);CloseHandle(hDevice);
}