tags:

views:

759

answers:

3

When I connect my digital camera with my computer, a dialog box containing all the registered programs can be used to get images from the camera will appear. Now I want to add my own program in the list, so that when I click the item of my program, I can use my own program to get images from the digital camera.

Thank you very much!

A: 

You need to use the WIA (Windows Image Acquisition) interface. IWiaDevMgr provides three methods to do this: RegisterEventCallbackProgram, RegisterEventCallbackCLSID, and RegisterEventCallbackInterface. If you want Windows to start your program when the user clicks you in the Autoplay dialog, you can use either RegisterEventCallbackProgram or RegisterEventCallbackCLSID.

chrisd
+3  A: 

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 BSTRs 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, ...

RBerteig
A: 

Also check here:

http://icopy.sourceforge.net/?p=98