zoukankan      html  css  js  c++  java
  • 驱动开发学习一

    关于驱动开发确实是有难度的,我在这里简单记录下简单的代码

    首先是sys

    //main.c
    #include<ntddk.h>
    
    //定义宏
    #define DEVICE_NAME "\Device\MyReadDevice"
    #define SYN_LINK_NAME "\??\MyRead"
    
    //全局定义方便在多个函数中使用
    
    PDEVICE_OBJECT pDevice;
    UNICODE_STRING DeviceName;
    
    
    NTSTATUS Unload(PDRIVER_OBJECT driver)
    {
    
        DbgPrint("this driver is loading
    ");
        return STATUS_SUCCESS;
        
    
    
    }
    
    //读取函数操作
    NTSTATUS MyDriverRead(PDEVICE_OBJECT driver, PIRP pirp)
    {
        NTSTATUS status;
        PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(pirp);
        ULONG ulReadlength = stack->Parameters.Read.Length;
        pirp->IoStatus.Status= STATUS_SUCCESS;
        pirp->IoStatus.Information = ulReadlength;
        //格式化读取内容
        memset(pirp->AssociatedIrp.SystemBuffer, 0xaa, ulReadlength);
        IoCompleteRequest(pirp, IO_NO_INCREMENT);
        //读取完成标志
        DbgPrint("over
    ");
        return STATUS_SUCCESS;
    }
    
    
    
    
    NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
    {
        //PVOID test = NULL;
        //PUCHAR test1 = "aaaaaaaa";
        //memcpy(test, test1, 4);
        RtlInitUnicodeString(&DeviceName, DEVICE_NAME);
        NTSTATUS status = IoCreateDevice(driver, 0, &DeviceName, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevice);
        
        //判断是否加载成功
        if (!NT_SUCCESS(status))
        {
            DbgPrint("Create Device failed");
            return STATUS_SUCCESS;
    
        }
        UNICODE_STRING SymLinkName = RTL_CONSTANT_STRING(SYN_LINK_NAME);
        status = IoCreateSymbolicLink(&SymLinkName, &DeviceName);
        if (!NT_SUCCESS(status))
        {
         
            DbgPrint("Create SymLINKname faile
    ");
            return STATUS_SUCCESS;
        
        }
        driver->DriverUnload = Unload;
        //DbgPrint("this is my first Driver
    ");
        //设置符号读取
        driver->MajorFunction[IRP_MJ_READ] = MyDriverRead;
    
        pDevice->Flags |= DO_BUFFERED_IO;
    
    
    
    
        return STATUS_SUCCESS;
    
    
    }
    然后是应用层开发

    我们选择的是windows控制台程序,这里不做赘述

    贴代码

    //read.cpp
    #include "stdafx.h"
    #include<windows.h>
    
    int a;
    int _tmain(int argc, _TCHAR* argv[])
    {
    
            //变量的初始化定义
        HANDLE hDevice =
        CreateFile(L"\\.\MyRead",
        GENERIC_READ | GENERIC_WRITE,
        0, //分享
        NULL,  //安全检测
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
        if (hDevice == INVALID_HANDLE_VALUE)
        {
            printf("failed to obtain file handle device:"
                "%s with win32 error code:%d
    ",
                "MYWDMDRIVER",GetLastError);
            return 1;
    
        }
        UCHAR buffer[10];
        ULONG ulRead;
        BOOL bRet = ReadFile(hDevice, buffer, 10, &ulRead, NULL);
        if (bRet)
        {
        
            printf("Read %d bytes:", ulRead);
            for (int i = 0; i < (int)ulRead; i++)
            {
                printf("%02X",buffer[1]);
            }
            printf("
    ");
        }       
        CloseHandle(hDevice);
        scanf("%d", &a);
        return 0;
    }

    驱动之路漫漫,何处是归处?

    以上资料来自https://www.bilibili.com/video/av26193169/?p=2

    如有侵权,请联系本人删除

  • 相关阅读:
    Python 三级菜单
    linux 下按文件类型删除
    linux 做内网端口映射
    ss
    fio
    libXtst.so.6 is needed by teamviewer-12.0.76279-0.i686
    copy 浅复制 与深复制
    Git 使用方法
    关于 爬虫使用 urllib.urlopen 提交默认 User-Agent值
    Python 官方模块文档
  • 原文地址:https://www.cnblogs.com/kk328/p/10159516.html
Copyright © 2011-2022 走看看