windows驱动入门-1

1.搭建编译环境

    (1) 安装编译器:vc6.0或vc2003、vc2005等。

    (2) 安装驱动开发工具包DDK ( driver development kits )

        下载:ftp://202.113.29.4/ISO/M$/WinDDK/winxp_ddk.rar

    这个是winxp的,也就是说由他编译的驱动是在XP下运行。如果你想下载其他版本,可以到ftp://202.113.29.4/ISO/M$/WinDDK/下找到你要的目标平台。

    注意:这里的版本指的是将要运行你编译的驱动的机器操作系统版本,与你自己现在运行操作系统版本无关。

 

2.编写第一个驱动

 

  1. /* hello.c */
  2. #include <wdm.h>
  3. VOID HelloUnload( IN  PDRIVER_OBJECT DriverObject )
  4. {
  5.        DbgPrint( "Bye driver" );
  6. }
  7. NTSTATUS DriverEntry( IN PDRIVER_OBJECT pDriverObject,
  8. IN PUNICODE_STRING pRegistryPath )
  9. {
  10.        DbgPrint( " hello driver world! " ) ;
  11.        pDriverObject->DriverUnload = HelloUnload;
  12.        return STATUS_SUCCESS;
  13. } 

DriverEntry 驱动的入口函数,相当c中main函数。

DbgPrint 输出函数,相当c中printf函数。

DriverUnload:函数指针,指向驱动卸载函数,当驱动动态卸载的时候调用它释放资源,有点像析构函数。

 

makefile文件

  1. !INCLUDE $(NTMAKEENV)/makefile.def

 

sources文件

  1. TARGETNAME=hello
  2. TARGETTYPE=DRIVER
  3. TARGETPATH=obj
  4. INCLUDES=
  5. TARGETLIBS=
  6. SOURCES=hello.c
  7.  

编译

 

  (1) 开始/所以程序/Develompent kits/ Windows DDK 2600 / Build Environments /Win 2K Checked Build Environment

   (2) 输入build进行编译。

  (3)没有任何问题的话,将在objckd/i386/下面生成一个hello.sys,这就是驱动文件。

 

 

3.驱动动态加载程序

  1. #include <windows.h>
  2. #include <stdio.h>
  3. int _cdecl main(void)
  4. {
  5.     HANDLE hSCManager;
  6.     HANDLE hService;
  7.     SERVICE_STATUS ss;
  8.     hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
  9.     
  10.     printf("Load Driver/n");
  11.     if(hSCManager)
  12.     {
  13.         printf("Create Service/n");
  14.         hService = CreateService(hSCManager, "Example""Example Driver", SERVICE_START | DELETE | SERVICE_STOP, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, "C://hello.sys", NULL, NULL, NULL, NULL, NULL);
  15.         if(!hService)
  16.         {
  17.             hService = OpenService(hSCManager, "Example", SERVICE_START | DELETE | SERVICE_STOP);
  18.         }
  19.         if(hService)
  20.         {
  21.             printf("Start Service/n");
  22.             StartService(hService, 0, NULL);
  23.             printf("Press Enter to close service/r/n");
  24.             getchar();
  25.             ControlService(hService, SERVICE_CONTROL_STOP, &ss);
  26.             CloseServiceHandle(hService);
  27.             DeleteService(hService);
  28.         }
  29.         CloseServiceHandle(hSCManager);
  30.     }
  31.     
  32.     return 0;
  33. }

4.运行驱动

 

 (1).hello.sys复制到c:/hello.sys

 (2).运行查看消息工具Dbgview.exe(debug view)。

    下载地址:http://download.sysinternals.com/Files/DebugView.zip

 (3).运行驱动加载程序,加载hello.sys

     你将会在Dbgview.exe程序中看到第一条输出。

 (4).在驱动加载程序上按任意键,卸载hello.sys驱动。

  你将会在Dbgview.exe程序中看到第二条输出。

如下图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值