驱动层:
/*
与驱动中有4中模式 分为
1、 输入输出缓冲I/O(METHOD_BUFFERED) 参数为0
2、 直接输入缓冲输出I/O(METHOD_IN_DIRECT) 参数为1
3、 缓冲输入直接输出I/O(METHOD_OUT_DIRECT) 参数为2
4、 上面三种方法都不是(METHOD_NEITHER) 参数为3
我用的是第4种
四种模式的接收数据与输出数据的位置都不同 如下:
接收数据:
传输类型 位置
METHOD_IN_DIRECT pIrp->AssociatedIrp.SystemBuffer
METHOD_OUT_DIRECT pIrp->AssociatedIrp.SystemBuffer
METHOD_BUFFERED pIrp->AssociatedIrp.SystemBuffer
METHOD_NEITHER pIrpStack->Parameters.DeviceIoControl.Type3InputBuffer
输出数据:
METHOD_IN_DIRECT pIrp->MdlAddress
METHOD_OUT_DIRECT pIrp->MdlAddress
METHOD_BUFFERED pIrp->AssociatedIrp.SystemBuffer
METHOD_NEITHER pIrp->UserBuffer
*/
#if DBG
#define dprintf DbgPrint
#else
#define dprintf
#endif
//不支持符号链接用户相关性
#define DEVICE_NAME L"\\Device\\devtest" // Driver Name
#define SYMBOLIC_LINK_NAME L"\\DosDevices\\test" // Symbolic Link Name
#define WIN32_LINK_NAME "\\\\.\\test" // Win32 Link Name
//支持符号链接用户相关性
#define SYMBOLIC_LINK_GLOBAL_NAME L"\\DosDevices\\Global\\test" // Symbolic Link Name
#define IOCTL_HELLO_WORLD \
CTL_CODE \
( \
FILE_DEVICE_UNKNOWN, \
0x800, \
METHOD_NEITHER, \ //其他模式也就是第4种模式
FILE_ANY_ACCESS \
)
int add(int a,int b) //这里做个加法函数
{
return a+b;
}
................................
NTSTATUS
DispatchDeviceControl(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp) //处理控制请求
{
NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
PIO_STACK_LOCATION pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
ULONG uIoControlCode = 0;
PVOID pIoBuffer = NULL;
ULONG uInSize = 0;
ULONG uOutSize = 0;
int sum = 0;
int* OutinputBuffer = NULL;
uIoControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;
uInSize = pIrpStack->Parameters.DeviceIoControl.InputBufferLength;
uOutSize = pIrpStack->Parameters.DeviceIoControl.OutputBufferLength;
if (uIoControlCode == IOCTL_HELLO_WORLD) //如果为我们的控制码则
{
struct
{
int a;
int b;
} *InputBuffer; //定义一个结构 供我们做运算的两个参数
InputBuffer= pIrpStack->Parameters.DeviceIoControl.Type3InputBuffer;//接收的数据
dprintf("a=%d\nb=%d\n",InputBuffer->a,InputBuffer->b); //打印出接收的a跟b
sum = add(InputBuffer->a,InputBuffer->b); //进行加法运算
OutinputBuffer= pIrp->UserBuffer; // 输出运算好之后的数据定义为OutinputBuffer
RtlCopyMemory(OutinputBuffer,&sum,uOutSize);//复制运算后的数据给OutinputBuffer
dprintf("a+b=%d\n",*(int*)OutinputBuffer); //打印出a+b的和
pIrp->IoStatus.Information = uOutSize; //处理字节数
pIrp->IoStatus.Status = STATUS_SUCCESS; //成功
}
else //如果不是我们的控制码则
{
pIrp->IoStatus.Information = 0; //处理失败
status = STATUS_INVALID_PARAMETER;//处理失败
}
IoCompleteRequest(pIrp, IO_NO_INCREMENT); //完成IRP
return status; //返回处理结果
}
//======================================================================================
#include "windows.h"
#include "common.h"
#include <winioctl.h>
struct InputBuffer
{
int a;
int b;
} ;
InputBuffer ipb;
int main(int argc, char* argv[])
{
HANDLE hDevice = CreateFile(WIN32_LINK_NAME,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("错误代码: ", GetLastError());
getchar();
return -1;
}
DWORD dwWrite;
int z;
ipb.a = 520;
ipb.b = 1314;
DeviceIoControl(hDevice, IOCTL_HELLO_WORLD , &ipb, sizeof(ipb), &z, sizeof(z), &dwWrite, NULL);
printf("%d",z);
getchar();
CloseHandle(hDevice);
return 0;
}