How to load our device drivers 如何装载我们的设备驱动

原创 2006年05月18日 10:12:00

安装驱动有很多种方法 INF/ 直接写注册表,也可以直接自己编写装载程序来完成

这里有一个装载驱动的类,使用非常方便。

Introduction

When you develop device drivers you always need to make two projects: first, the device driver and second the application to load and test it. This wrapper class, TDriver, can help you in the second one. Furthermore I developed an application example that you can use to load any driver you indicate.

Background

Windows allows loading drivers at runtime using the Service Control Manager. Yes, the Service Control Manager in Windows not only can be used to load and manage services. You can use it with device drivers as you use it to load/unload/start/stop windows services.

If you want to send/receive data to the device driver loaded, to allow this, you have to get a handle to the driver. For this purpose you can use the function createFile, and pass the driver name as the file name. You can see Windows is so abstract in this aspect.

Using the code

Using the TDriver Class is so easy. Here is the class definition and the explanation follows:

 

class TDriver

{

public:

    TDriver(void);    //constructor

    ~TDriver(void);      //destructor

 

    //functions to initialized the driver variables

    DWORD InitDriver(LPCTSTR name, LPCTSTR path, LPCTSTR dosName=NULL);

    DWORD InitDriver(LPCTSTR path);

 

 

    //functions to load and unload drivers.

    //If start = TRUE, the driver will be started.

    DWORD LoadDriver(BOOL start = TRUE);

    DWORD LoadDriver(LPCTSTR name, LPCTSTR path,

    LPCTSTR dosName=NULL, BOOL start=TRUE);

    DWORD LoadDriver(LPCTSTR path, BOOL start=TRUE);

 

    //if forceClearData == TRUE, will remove

    //variables although we cant remove driver "service"

    DWORD UnloadDriver(BOOL forceClearData = FALSE);

   

    //functions to start and stop, driver "service"

    DWORD StartDriver(void);

    DWORD StopDriver(void);

 

    //if true, the driver havent been removed at finish

    void SetRemovable(BOOL value);

 

 

    //funtion to return class status

    BOOL IsInitialized();

    BOOL IsStarted();

    BOOL IsLoaded();

 

 

    //function to get driver handle

    HANDLE GetDriverHandle(void);

 

    //funtions to make IO operation with driver

    DWORD WriteIo(DWORD code, PVOID buffer, DWORD count);

    DWORD ReadIo(DWORD code, PVOID buffer, DWORD count);

    DWORD RawIo(DWORD code, PVOID inBuffer,

        DWORD inCount, PVOID outBuffer, DWORD outCount);

   

private:

    HANDLE driverHandle;    //driver handle

   

    LPTSTR driverName;        //driver name

    LPTSTR driverPath;        //driver disk path

    LPTSTR driverDosName; //driver's dos name, to link with it

 

    BOOL initialized; //variables to store the status of this class

    BOOL started;

    BOOL loaded;

    BOOL removable;

 

    //get a handle to the driver

    DWORD OpenDevice(void);   

};

The methods you can use in this class are:
InitDriver(...)

This function is used to initialize the parameters of the driver such as: driver name, driver path and driver dos name. If you only know the driver path, the funtion tries to get the rest of the parameters.

LoadDriver(..)

You can use this function after you call InitDriver, when you use only one parameter. If start is FALSE, we need to call StartDriver function after, to start it. The other two functions, first initialize the driver and then load it as the first function.

UnloadDriver(BOOL forceClearData = FALSE)

This function stops the driver and deregister it in Windows Service Manager. If a problem occurred (while stopping or removing the driver), the class data associated with it(driver name, driver path...) will not be free unless you pass the TRUE value in forceClearData.

StartDriver()

You can use this function to start the driver "service".

StopDriver()

Use this function to stop the driver "service"

SetRemovable(BOOL value)

If you set the parameter removable to FALSE, in the unload process, the class will not deregister the driver in Windows Service Manager. This parameter is set automatically to FALSE if the driver was loaded by another application.

IsInitialized();
BOOL IsLoaded();
BOOL IsStarted()

The above are functions to ask the driver about its status.

GetDriverHandle()

You must use this function if you can access to the driver handle directly.

WriteIo(DWORD code, PVOID buffer, DWORD count)

Function to send data to the driver. You must indicate the IOCTL code.

ReadIo(DWORD code, PVOID buffer, DWORD count)

Function to receive data from the driver. You must indicate the IOCTL code. The data will be received in buffer with a maximum length of count.

RawIo(DWORD code, PVOID inBuffer, 
    DWORD inCount, PVOID outBuffer, DWORD outCount)

You decide what to do, read, write or both.

They are all functions exported by the class to applications. However to make a simple application that manage drivers we need use only two functions. For example, I developed a program to show the functionality of the TDriver class, that will load/unload any driver you introduce (see screenshot article). The code for this purpose is the next:

if(driver.LoadDriver(m_driverPath, true) == DRV_SUCCESS)
{
    //.....
}

//.....

if(driver.UnloadDriver() == DRV_SUCCESS)
{
    //.....
}

As you can see that with two lines you can load and unload a driver, making life easy for all :P.

设备管理器中 停用启用设备后设备上有YB 错误代码Code 31

闲暇时写个WDM驱动:CharSample。加载驱动后,在设备管理器中显示工作正常,甚是欣喜;然而这种欣喜没有持续几分钟就被冲散了:停用/再启用设备后,设备图标上有yellow bang,显示设备状态...
  • lixiangminghate
  • lixiangminghate
  • 2016年12月03日 16:42
  • 726

Linux device drivers学习笔记(4)——Outline

Linux设备驱动学习 学习新技术需要抓住要点,然后从全局把握技术的流程,再进而细化技术的细节,最后从本质上掌握技术。看了这么长时间的Linux Device Drivers,也配合看了其他从网上找...
  • u012914709
  • u012914709
  • 2014年05月11日 15:49
  • 507

分享《Essential Linux Device Drivers》中文版高清电子版

新浪微博 @宋宝华Barry 在@微盘 分享了 Prentice.Hall出版社《Essential.Linux.Device.Drivers》中文版高清电子版 "宋宝华_精通LINUX设备驱动开发....
  • 21cnbao
  • 21cnbao
  • 2013年02月28日 22:45
  • 19623

How to write DSP device drivers

By Nick Lethaby and David FriedlandEmbedded Systems Design (12/15/03, 05:00:00 H EST)As digital sign...
  • jackle_zheng
  • jackle_zheng
  • 2009年04月17日 21:32
  • 530

Essential Linux Device Drivers(source code)

http://elinuxdd.com/~elinuxdd/elinuxdd.docs/sources.html
  • u011028408
  • u011028408
  • 2014年03月14日 17:27
  • 744

linux device drivers 读书笔记(第一章)

前言自己记录的读书笔记,按章节和小标题提取需要记忆的内容设备驱动的角色作为一个在软件和硬件之间的连接,将具体一个设备怎样提供功能所抽象,而使外部应用可以专注于一个设备提供了哪些功能.将内核分类进程管理...
  • qq_29343201
  • qq_29343201
  • 2016年09月21日 21:47
  • 219

Linux 设备驱动篇之-------I2c设备驱动(待续)

Linux 设备驱动篇之-------I2c设备驱动   虽然I2C硬件体系结构和协议都很容易理解,但是Linux I2C驱动体系结构却有相当的复杂度,它主要由3部分组成,即I2C设备驱动、I2C...
  • sonbai
  • sonbai
  • 2013年05月31日 22:12
  • 4222

Linux Device Drivers 3rd edition 设备驱动 源代码

Linux Device Drivers 3rd edition is a very good book by Jonathan Corbet, Alessandro Rubini and Greg ...
  • YAJUN0601
  • YAJUN0601
  • 2011年06月22日 00:55
  • 1315

读书笔记《Linux Device Drivers》:第一章,设备驱动介绍

  那个GMA500的倒霉驱动搞了很久,因此找到了这本书,发现是本好书,决定读一下。网上查了一下,这本的中文名字是《Linux设备驱动程序》,最新是第三版本,2005年出版的。这本书比较贵『相当地贵』...
  • flowingflying
  • flowingflying
  • 2009年07月23日 17:13
  • 1949

Hive装载数据命令

必须在表定义时创建partition a、单分区建表语句:create table day_table (id int, content string) partitioned by (dt str...
  • macyang
  • macyang
  • 2012年02月14日 21:28
  • 3873
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to load our device drivers 如何装载我们的设备驱动
举报原因:
原因补充:

(最多只允许输入30个字)