码迷,mamicode.com
首页 > 其他好文 > 详细

IO驱动通信

时间:2020-07-26 15:25:12      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:fine   驱动   变量   location   bgp   drive   data   click   code   

客户端通信是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("驱动程序停止运行了 . \r\n");

}
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驱动通信

标签:fine   驱动   变量   location   bgp   drive   data   click   code   

原文地址:https://www.cnblogs.com/Fentiao/p/13379346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!