驱动开发:通过ReadFile与内核层通信( 三 )


我们首先从内核中读出前五个字节并放入缓冲区内,输出该缓冲区内的数据,然后在调用写入,将hello lyshark写回到内核里里面,这段代码可以这样来写 。
#include <iostream>#include <Windows.h>#include <winioctl.h>int main(int argc, char *argv[]){  HANDLE hDevice = CreateFileA("\\\\.\\LySharkDriver", GENERIC_READ | GENERIC_WRITE, 0,    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  if (hDevice == INVALID_HANDLE_VALUE)  {    CloseHandle(hDevice);    return 0;  }  // 从内核读取数据到本地  char buffer[128] = { 0 };  ULONG length;  // 读入到buffer长度为5  // By:lyshark.com  ReadFile(hDevice, buffer, 5, &length, 0);  for (int i = 0; i < (int)length; i++)  {    printf("读取字节: %c", buffer[i]);  }  // 写入数据到内核  char write_buffer[128] = "hello lyshark";  ULONG write_length;  WriteFile(hDevice, write_buffer, strlen(write_buffer), &write_length, 0);  system("pause");  CloseHandle(hDevice);  return 0;}使用驱动工具安装我们的驱动,然后运行该应用层程序,实现通信,效果如下所示:

驱动开发:通过ReadFile与内核层通信

文章插图

经验总结扩展阅读