WIA has a Device Manager object that provides an interface that allows for programs to register for event notifications.
Contacting the Device Manager
You use the IWiaDevMgr
interface to interact with the device manager. You get a pointer to that interface with a call to CoCreateInstance()
:
IWiaDevMgr *pWiaDevMgr;
HRESULT hr;
hr = CoCreateInstance(CLSID_WiaDevMgr,
NULL,
CLSCTX_LOCAL_SERVER,
IID_IWiaDevMgr,
(void*)&pWiaDevMgr);
Registering a program for an event
Then, registering a program to be run when the event fires is as simple as:
pWiaDevMgr->RegisterEventCallbackProgram(
WIA_REGISTER_EVENT_CALLBACK,
NULL,
&WIA_EVENT_DEVICE_CONNECTED,
bstrCommandline,
bstrName,
bstrDescription,
bstrIcon);
The command line, name, description, and icon are all BSTR
s because they are passing through a COM interface. You can either use SysAllocString()
and its friends to create them, or use the classes supplied by Visual C extensions or ATL to create and manage them.
Releasing the Device Manager
If you aren't using a COM-aware smart pointer for the interface, then don't forget to release the reference taken by CoCreateInstance()
:
pWiaDevMgr->Release();
If you don't release it, the COM system will find a way to punish you, but it might not be immediately obvious...
Unregistration
Casual testing shows that deleting a registered event works when all four parameters used to register the event are passed exactly. The call is:
pWiaDevMgr->RegisterEventCallbackProgram(
WIA_UNREGISTER_EVENT_CALLBACK,
NULL,
&WIA_EVENT_DEVICE_CONNECTED,
bstrCommandline,
bstrName,
bstrDescription,
bstrIcon);
This is a potential annoyance because there seems to be no documented API to list the registered events. This implies that if your installer registers a program, then it should also keep a record of the arguments used so that your uninstaller can unregister the event.
The event parameters
The command line can contain the strings %1
and %2
which will be replaced by the port name and the GUID of the event that fired, respectively, before the command line is parsed.
The icon , name and description are displayed in the list presented to the user when the camera is plugged in. The name should be shorter than the description.
The icon is a combination of a file name and a resource identifier. A good default value is "sti.dll,0"
which will be a generic image of a camera and scanner. If you supply your own icon, the string almost certainly must include a fully-qualified path to the DLL. Going out on a limb, I'd imagine that deliberately including a comma anywhere in the path other than as the separator before the resource id will cause trouble.
Behind the scenes
The actual location where WIA/STI stores the list of events is not documented. However, with a little searching in regedit, I located the event catalog on my XP SP3 system. One might imagine that it would be found in a similar location in other systems...
The registry key HKLM\SYSTEM\CurrrentControlSet\Control\SillImage\Events
contains a subkey for each event known to the system. Each key has a value named GUID containing the GUID that identifies that event.
Device Connected event handlers are listed in the Connect
subkey, for example.
Keys for individual WIA/STI devices can be found in the HKLM\SYSTEM\CurrentControlSet\Control\Class\{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}
key among other places.
Remember that these locations are not documented. Touch them at your own risk, your mileage will vary, ...