Sunday, August 17, 2008

WinCE 6.0 : Loading device drivers programmatically

Windows CE drivers typically are built into the OS image itself. They are packaged into the OS through platform.bib and loaded by device manager during system boot up. Configuration of these drivers are kept in the registry through platform.reg.

However there can be instances when we want to load a driver programmatically. And we can do so using ActivateDeviceEx. In order to use ActivateDeviceEx, we would need to populate the registry with the appropriate device driver settings, example below.

ActivateDeviceEx
----------------

// prepare registry entries for loading device driver
DWORD ret;
HKEY hkResult;
DWORD dwDisposition;

ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("Drivers\\BuiltIn\\BTH"),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkResult,
&dwDisposition);

TCHAR prefix[] = TEXT("BTH");
TCHAR dll[] = TEXT("\\Nand Disk\\Bluetooth\\Bth_Drv.dll");
DWORD index = 1;

ret = RegSetValueEx(hkResult,
TEXT("Index"),
0,
REG_DWORD,
(BYTE*)&index,
sizeof(index));
ret = RegSetValueEx(hkResult,
TEXT("Dll"),
0,
REG_SZ,
(BYTE*)dll,
sizeof(dll));
ret = RegSetValueEx(hkResult,
TEXT("Prefix"),
0,
REG_SZ,
(BYTE*)prefix,
sizeof(prefix));

RegCloseKey(hkResult);

// load device driver DLL using ActivateDeviceEx
HANDLE hBthDevice = INVALID_HANDLE_VALUE;

hBthDevice = ActivateDeviceEx(TEXT("Drivers\\BuiltIn\\BTH"), NULL, 0, NULL);

No comments: