Книга: Microsoft Windows Embedded CE 6.0 Exam Preparation Kit
Dynamically Loading a Driver
Dynamically Loading a Driver
As mentioned earlier in this lesson, an application can also communicate with a stream device driver after calling the ActivateDevice or ActivateDeviceEx function. ActivateDeviceEx offers more flexibility than ActivateDevice, yet both functions cause Device Manager to load the stream driver and call the driver's XXX_Init function. In fact, ActivateDevice calls ActivateDeviceEx. Note, however, that ActivateDeviceEx does not provide access to an already loaded driver. The primary purpose of the ActivateDeviceEx function is to read a driver-specific registry key specified in the function call to determine the DLL name, device prefix, index, and other values, add the relevant values to the active device list, and then load the device driver into the Device Manager process space. The function call returns a handle that the application can later use to unload the driver in a call to the DeactivateDevice function.
ActivateDeviceEx replaces the older RegisterDevice function as a method to load a driver on demand, as illustrated in the following code sample:
// Ask Device Manager to load the driver for which the definition
// is located at HKLMDriversSample in the registry.
hActiveDriver = ActivateDeviceEx(L"DriversSample", NULL, 0, NULL);
if (hActiveDriver == INVALID_HANDLE_VALUE) {
ERRORMSG(1, (L"Unable to load driver"));
return -1;
}
// Once the driver is lodaded, applications can open the driver
hDriver = CreateFile (L"SMP1:",
GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDriver == INVALID_HANDLE_VALUE) {
ERRORMSG(1, (TEXT("Unable to open Sample (SMP) driver")));
return 0; //Insert code that uses the driver here
}
// Close the driver when access is no longer needed
if (hDriver != INVALID_HANDLE_VALUE) {
bRet = CloseHandle(hDriver);
if (bRet == FALSE) {
ERRORMSG(1, (TEXT("Unable to close SMP driver")));
}
}
// Manually unload the driver from the system using Device Manager
if (hActiveDriver != INVALID_HANDLE_VALUE) {
bRet = DeactivateDevice(hActiveDriver);
if (bRet == FALSE) {
ERRORMSG(1, (TEXT("Unable to unload SMP driver ")));
}
}
NOTE
Automatic vs. dynamic loading of drivers
Calling ActivateDeviceEx to load a driver has the same result as loading the driver automatically during the boot process through parameters defined in the HKEY_LOCAL_MACHINEDriversBuiltIn key. The BuiltIn registry key is covered in more detail in Lesson 3 later in this chapter.
- Lesson 2: Implementing a Stream Interface Driver
- Opening and Closing a Stream Driver by Using the File API
- JDBC Туре 4 DRIVER
- Initial loading of extra modules
- Problems loading modules
- Test Driver Code
- Installing Proprietary Video Drivers
- Beginning the Boot Loading Process
- Loading the Linux Kernel
- Chapter 8. Device Driver Basics
- 8.1. Device Driver Concepts
- 8.1.2. Device Driver Architecture