There is a lesser known feature of stream interface drivers, the post init IOCTL. This is probably lesser known because there isn’t a good way to put this in the help index and it doesn’t appear to be mentioned in the stream interface section of the help contents.
The post init IOCTL is an IOCTL that is called by the device manager after calling XXX_Init(). The purpose is that this allows your driver to do some initialization after XXX_Init() runs. Huh, initialization after XXX_Init() runs? Yes, after XXX_Init() runs. I have struggled with this because I could never think of a reason to do some initialization after XXX_Init(), but I recently found a reason, which I document in Windows CE: Advertising a Power Manageable Device Driver.
Here is how the post init IOCLT works:
1.       Add an IOCTL, that you define, in the XXX_IOControl() function
2.       Add the IOCTL value to the registry settings for the driver
3.       When the Device Manager loads the driver:
a.       It reads the registry settings
b.      It loads the driver DLL
c.       It calls XXX_Open()
d.      If the IOCTL value was in the registry, it calls XXX_Open() then DeviceIoControl() passing in the IOCTL value and a POST_INIT_BUF struct
The POST_INIT_BUF struct contains a device handle that can be used DevMgr API calls and the driver’s registry path as passed into ActivateDeviceEx().
The code:
#define IOCTL_DRIVERSHELL_POSTINIT 0xB0
 
                case IOCTL_DRIVERSHELL_POSTINIT:
                                {
                                                POST_INIT_BUF *pBuf = (POST_INIT_BUF *)pBufIn;
                                                GUID PMStreadGuid = DEVCLASS_POWER_MANAGER_GUID;
                                                if( ERROR_SUCCESS != DmAdvertiseInterface( pBuf->p_hDevice, &PMStreadGuid, TEXT("XXX1:"), TRUE) )
                                                {
                                                                RETAILMSG( 1, (TEXT("DmAdvertiseInterface failed\r\n")));
                                                                bRet = FALSE;
                                                }
                                                else
                                                                bRet = TRUE;
                                }
                                break;
 
The registry change:
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\DriverShell]
                "IOCTL"=dword:B0
Copyright © 2009 – Bruce Eitman
All Rights Reserved