我想很多WinCE的开发人员,尤其是刚入门并且做驱动开发的工程师,都曾碰到这样一个问题,要编写一个外围设备的驱动,拿最简单的GPIO驱动来说,编写驱动本身可能只花了一会儿功夫,可要把编译生成的DLL打包到先前做好的操作系统映像当中,最简单也得MakeImg一下,还要修改BIB文件、注册表文件,以让系统启动的时候就加载该驱动,所有工作都做完了,还得花几分钟下载整个操作系统到内存去运行,这也得要个好几分钟。能力强的人一次成功,不走回头路也就算了。如果驱动编写得有问题,那又得改代码,重新编译,把刚刚所做的事情再做一遍。说出来不怕大家笑话,我刚开始做驱动时就这样,反反复复,半天下来,才调试好一个简单的驱动。而事实上很大一部分时间都浪费在重复操作上。这种调试驱动的方法实在效率太低了。想到Linux下面的驱动调试,多方便!直接INSMOD一下,应用程序就可以调用,出现问题就RMMOD,根本无须来回倒腾操作系统的映像文件。那么,WinCE下难道就没有这么简便的方法嘛?答案是肯定的。
         闲话少说,进入正题。查找EVC的帮助,发现函数ActivateDevice()可用来加载驱动程序。而这个函数的使用是相当简单的。我就不多说了,贴上一段帮助最能说明问题。当然,你也可以用ActivateDeviceEx()。
HANDLE ActivateDevice (
LPCWSTR lpszDevKey,
DWORD dwClientInfo
);
Parameters
lpszDevKey
[in] Pointer to the registry path string of the device's driver key under HKEY_LOCAL_MACHINE. A driver key contains the DLL name, the device prefix, friendly name, and other device information.
dwClientInfo
[in] Data that will be stored in the device's Active key in the ClientInfo value. The registry path to the driver's Active key is passed in as the context parameter to the device's XXX_Init function. The value in dwClientInfo is stored in the registry before XXX_Init is called.
Return Values
On success, ActivateDevice returns a handle to the device that can be used in subsequent calls to DeactivateDevice.
Remarks
This function is superseded by ActivateDeviceEx.
ActivateDevice loads a device driver. ActivateDevice reads the registry key specified in lpszDevKey to get the DLL name, device prefix, index, and other values. Next it adds the device to the active device list in the registry branch HKEY_LOCAL_MACHINE\Drivers\Active, and stores the relevant values there. If no device index was specified in the key named in lpszDevKey, then it assigns a free index. Then it loads the device driver DLL in the process space of the Device Manager. Then it broadcasts a WM_DEVICECHANGE message for the new device and triggers a NOTIFICATION_EVENT_DEVICE_CHANGE event in the application notification system to notify applications of the presence of the new device.
从上面的描述中可以看到,在使用该函数时,我们只要给出第一个参数就可以,而这个参数是注册表中的一个路径字符串。所以,要想很方便的动态加载任意一个驱动,我们还要了解一下有关注册表的内容。但其中最核心的就是一条,把你驱动的相关注册表信息放到HKEY_LOCAL_MACHINE下,主要内容包括Prefix、DLL、Index、Order等信息。这里就不展开说明了。
        自己起初费了那么多冤枉时间,实在很郁闷。原理摸清后就做了一个小工具,实现动态加载流驱动,以提高开发效率,也能方便后来人。下面就详细介绍这个小工具的使用过程,让大家体验一下动态加载流驱动是多么畅快的一件事情。
阅读更多下文请点击:http://www.hzlitai.com.cn/article/ARM11/SYSTEM/1807.html