IO驱动通信

Posted fentiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO驱动通信相关的知识,希望对你有一定的参考价值。

客户端通信是DLL 文件,看需求可以自己编译到exe上 下面是DLL调用过程

技术图片

 

 技术图片

 

 技术图片

 

 

客户端代码

技术图片
// DriveDll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#define READCODE CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ALL_ACCESS)
#define WRITECODE CTL_CODE(FILE_DEVICE_UNKNOWN,0x900,METHOD_BUFFERED,FILE_ALL_ACCESS)
#define SYMBOLICLINK_NAME "\\\\.\\My_Device"
HANDLE g_hDevice;
typedef struct DATA
{
    DWORD pid;
    UINT address;
    DWORD size;
    BYTE* data;
}Data;
extern "C"  _declspec(dllexport)
int Read(DWORD PID, UINT add,DWORD size)
{
    Data data;
    DWORD dwSize = 0;
    data.pid = PID;
    data.address = add;
    data.size = size;
    data.data = new BYTE[data.size];
    g_hDevice = CreateFile("\\\\.\\My_Device", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (g_hDevice == INVALID_HANDLE_VALUE)
    {
        return 1;
    }
    DeviceIoControl(g_hDevice, READCODE, &data, sizeof(data), &data, sizeof(data), &dwSize, NULL);
    CloseHandle(g_hDevice);
    return data.data[0];
}
extern "C"  _declspec(dllexport)
VOID Close()
{
    CloseHandle(g_hDevice);
}



BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    return TRUE;
}
客户端

 

驱动程序代码

技术图片
#include <ntifs.h>
#include<ntddk.h>
#include<windef.h>
#define READCODE CTL_CODE(FILE_DEVICE_UNKNOWN,0x800,METHOD_BUFFERED,FILE_ALL_ACCESS)
#define WRITECODE CTL_CODE(FILE_DEVICE_UNKNOWN,0x900,METHOD_BUFFERED,FILE_ALL_ACCESS)
VOID UnDriver(PDRIVER_OBJECT driver)
{
   

    DbgPrint("驱动程序停止运行了 . 
");

}
VOID IrpDeviceControlProc(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
    PIO_STACK_LOCATION  Stack;
    ULONG uIoControlCode;
    NTSTATUS status = STATUS_INVALID_DEVICE_REQUEST;
    // 设置临时变量的值
    Stack = IoGetCurrentIrpStackLocation(pIrp);

    switch (Stack->MajorFunction)
    {
           case IRP_MJ_CREATE:{
                     
        DbgPrint("IRP_MJ_CREATE触发");
                     break;
            }
           case IRP_MJ_CLOSE: {
                         DbgPrint("IRP_MJ_CLOSE触发");
                          break;
            }
          case IRP_MJ_DEVICE_CONTROL:{
        DbgPrint("IRP_MJ_DEVICE_CONTROL触发");
        break;

            }
     }
    pIrp->iostatus.Status = status;
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return status;
}

VOID DispatchClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
    pIrp->IoStatus.Status = STATUS_SUCCESS;
    DbgPrint("DispatchClose执行成功");
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}

VOID Dispatchcreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
    pIrp->IoStatus.Status = STATUS_SUCCESS;
    DbgPrint("Dispatchcreate执行成功");
    IoCompleteRequest(pIrp, IO_NO_INCREMENT);
    return STATUS_SUCCESS;
}
NTSTATUS CreateDriverObject(PDRIVER_OBJECT pDriver)
{
    NTSTATUS Status;
    PDEVICE_OBJECT pDevobj;
    UNICODE_STRING DriverName;
    PDRIVER_OBJECT SymLinkName;

    RtlInitUnicodeString(&DriverName, L"\\Device\\My_Device");
    Status=IoCreateDevice(pDriver, 0, &DriverName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevobj);
     DbgPrint("IoCreateDevice状态:%d",Status);
     pDriver->Flags |= DO_BUFFERED_IO;
     RtlInitUnicodeString(&SymLinkName, L"\\??\\My_Device");
     Status = IoCreateSymbolicLink(&SymLinkName, &DriverName);
     DbgPrint("IoCreateSymbolicLink状态:%d", Status);



     return STATUS_SUCCESS;



}


NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
    CreateDriverObject(driver);
    driver->MajorFunction[IRP_MJ_CREATE] = Dispatchcreate;
    driver->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
    driver->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IrpDeviceControlProc;
    driver->DriverUnload = UnDriver;



    return STATUS_SUCCESS;
}
驱动程序

 

以上是关于IO驱动通信的主要内容,如果未能解决你的问题,请参考以下文章

通过 c# 与 socket.io 服务器通信

固态驱动器是不是足够好,无需担心磁盘 IO 瓶颈?

在tablayout片段之间进行通信[重复]

内存映射 IO - 它是如何完成的?

java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段

与另一个片段通信的片段接口