Saturday, August 22, 2009

How ActivateDeviceEx or ActivateDevice works? [WinCE]

Basic difference between these two API is that 1st load and register a device driver under Active key in registry while 2nd only load the driver.
Here I will details about only ActivateDeviceEx. It takes four parameter
1. Registry path to determine all sub key information like prefix, order and dll information.
2. REGINI pointer: every structure variable determine the value to be added to active key. Before going further, I would like to clear what is a active key. If using tools in platform builder, you can see in the registry, there is a node called "active" . Basically it contains all driver and their key which are active at the paticular time. To determine whether your driver is active or not, you can verify it by looking the registry entry.
3. cRegEnts - count the number of structure regini pointer pointer
4. lpvParam - Bus specific variable, we know that ActivateDevice calls xxx_Init, so this is second parameter during that course. So you can fill the variable and later you can access the same in init function.

Let begin with how everything goes fine:)

It will start with reading the registry entry in a structure.
Check for operation flag, like for load, unload or none.
Check for boot phase like phase 1 or 2. Usually there is two boot phase, and depending on boot phase setting in flag subkey, it determine to load in particular boot phase.
Once all this done, it will create a entry for the driver in the active key.It work like if there is already existing entry then it will format then will add new structure which can contain fresh subkey values.
Now it initializes the subkey variable using the registry entry made by us with RegSetValue API.
Next it will read the parent BUS driver specific registry entry by check bus parent and bus name using the active key.
It will check the index value if exist, if not then it will create unique Index for it. Else will create the same except we will assign 0 if index value mention is 10. It work in three steps by verifying bus name and device name and then if not duplicate then it will create new structure for the device and keep with itself in a link list sort of structure.
There is bug here in code - I tested and verified. If you have a device with index not mention with same prefix as of three device then one of them will not get loaded. I will debug and send it to MS.

Now check for naked device name and also allocate the total space needed.
Most important is that this step will store the address of each standard exported function. Then check for init and deinit exist and remove entry which are not present in exported function list.
In the last and final step, add device handle and device name.
This this point everything was related with setting registry variable and registry related stuff,. Now we will launch the device by calling xxx_Init function.
Now there will security check on the launch parameter or un-launch. There will OEMCetifyTrust in this process and then there will check on registry entry again. Then there will be call to Init or DeInit depending on flags entry.

Everything is Microsoft copyright. The way explained is mine. That's it for now. See you at next article.