none
How to set event handling for pushing scanner button on Windows 7 with WIA 2.0 C#

    Question

  • Hello,

    I would like to ask you, how to catch scanner button event by WIA 2.0 on Windows 7.

    This code I found on MSDN, but not working:
    Device device;
    DeviceManagerClass dManager = new DeviceManagerClass();
    CommonDialogClass cdlg = new CommonDialogClass();
    device = cdlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false);
    dManager.RegisterEvent(EventID.wiaEventScanImage, device.DeviceID);
    dManager.OnEvent += new _IDeviceManagerEvents_OnEventEventHandler(device_OnEvent);

    Maybe, there is problem with scanner and Win 7. I use HP DeskJet F2180. And if I dont have install HP softwares (All-in-one), pushing scanner button do nothing - windows 7 dont know that button was pressed. I have been looking for settings of scanner in windows registry and found SCANbutton with GUID, I tried this GUID in RegisterEvent, but again, not working.

    With installed HP softwares scanner button execute some HP scaner software, but I want catch this event in my aplication. WIA 2.0 work perfectly, I can scan and set properties of scanner, but I cant use button on front panel of scanner for scanning.


    I had older HP scanner and when I pushed scanner button, on WinXP was showed box with programs (these programs can be changed by windows registry) or my program handle event (actually I think that there was WIA 1.0 maybe). But Window 7 do/show nothing.

    Thanks for any reply.

    Pav.

    Wednesday, February 09, 2011 10:18 AM

All replies

  • Limitations of the Scripting Model

    I suggest you to use the WIA COM interface to access the scanner.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Wednesday, February 09, 2011 4:04 PM
  • Thanks for reply.

    I already use WIA COM interface (Windows Image Acquisition Automation Layer 2.0), without it I couldnt operate with scanner from my C# program (setting of properties). Top presented code use WIA library and some same codes can be seen here in VB.NET:

    http://msdn.microsoft.com/en-us/library/ms630826%28v=VS.85%29.aspx

    EventIDs are here:

    http://msdn.microsoft.com/en-us/library/ms630808%28v=VS.85%29.aspx

    But for me not working (i mean on windows 7 and with HP scanner), I cant handle (catch) event that scanner button on front panel was pressed.

    Here is my whole code for scanner in C# and with WIA AL 2.0:

     

    using WIA;
    Device device;
    CommonDialogClass cdlg;
    
    private void Form1_Load(object sender, EventArgs e)
    {
     DeviceManager dm = new
     DeviceManager();
     dm.RegisterEvent(EventID.wiaEventDeviceConnected, Miscellaneous.wiaAnyDeviceID); //work 
     dm.RegisterEvent(EventID.wiaEventDeviceDisconnected, Miscellaneous.wiaAnyDeviceID); //work
     dm.RegisterEvent(EventID.wiaEventScanImage, Miscellaneous.wiaAnyDeviceID); //dont work
     dm.OnEvent+=new _IDeviceManagerEvents_OnEventEventHandler(dm_OnEvent);
    
     cdlg = new CommonDialogClass();
     device = cdlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false);
    }
    
    private void dm_OnEvent(string EventID, string DeviceID, string ItemID)
    {
     MessageBox.Show(EventID + " " + DeviceID + " " + ItemID);
    }
    
    private void buttonStartScan_Click(object sender, EventArgs e)
    {
    //setting of scanner properties
     Object Object1 = null;
     Object Object2 = null;
     Int32 DPI = 300;
     Object1 = (Object)"6146";
     Object2 = (Object)2; 
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6147";
     Object2 = (Object)DPI;
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6148";
     Object2 = (Object)DPI;
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6149";
     Object2 = (Object)100;
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6150";
     Object2 = (Object)150;
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6151";
     Object2 = (Object)(DPI * 8.5 - (100+100));
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
     Object1 = (Object)"6152";
     Object2 = (Object)(DPI * 11.69 - (100+200));
     device.Items[1].Properties.get_Item(ref Object1).set_Value(ref Object2);
    
    //scanning
     ImageFile img = (ImageFile)cdlg.ShowTransfer(device.Items[1], FormatID.wiaFormatBMP, false);
    
     string file = "d:\\test1.bmp";
     img.SaveFile(file);
    }
    

    First two events work - both start dm_OnEvent; event for button was pressed dont work (EventID.wiaEventScanImage).

    Last method is for start scanning from aplication - work. So, why wiaEventScanImage dont work?

    Or is this WIA Scripting? Im little bit lost:(

    Thursday, February 10, 2011 9:48 AM
  • You are using the WIA COM interface generaled from a type library that is for scripting. I suggest you to translate the interface from the SDK C++ headers and create the objects using their program ids.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Friday, February 11, 2011 10:22 PM
  • Thanks. Your suggestion is very helpfull. I didnt know that this is scripting (actully I didnt know that there is some resctrictions for WIAAL). I spent much time with google, tried find some sloution.

    Ok, I will try it.

    Pav.

    Tuesday, February 15, 2011 2:15 PM
  • I suggest you to translate the interface from the SDK C++ headers and create the objects using their program ids.

    Can you provide a short example on how to do this (i.e only creating a DeviceManager) from within C#?

    Regards,

    Benjamin

    Tuesday, February 15, 2011 4:00 PM
  • //replace IWiaDevMgr with IWiaDevMgr2 for Vista or higher

    IWiaDevMgr manager = (IWiaDevMgr)Activator.CreateInstance(Type.GetTypeFromProgID("WIA.DeviceManager"));

    definition of IWiaDevMgr can be found at pinvoke.net.

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Tuesday, February 15, 2011 9:47 PM
  • Hi,

    I tried some samples in SDKs, concretely Microsoft SDKs\Windows\v7.0\Samples\multimedia\wia\getimage\, which is in C++ and I changed in EventCallback.cpp this:

        hr = pWiaDevMgr->RegisterEventCallbackInterface(
            0,
            NULL,
            &WIA_EVENT_SCAN_IMAGE,                                //here was &WIA_EVENT_DEVICE_CONNECTED
            this,
            &m_pConnectEventObject
        );

    I think that after pushing button on any device (button for scanning) will trigger m_pConnectEventObject, which will connect specific device. If Iam right (maybe not :)), it doesnt work with HP DeskJet F2180...

    Interesting thing, when I tried older scanner HP ScanJet 6300C with code written on top of topic, it worked. Simply I pushed button on front panel of scanner and my aplication was handling event. So, WIA Automation layer 2.0 work fine ... I thing maybe new HP devices have new special feature, for me, bad feature:) and button on front panel work only with their software (contains in All-In-Once packet), which is useless. Or there is problem with combined device (printer+scanner) or is problem in me :).

    Pav

    Thursday, February 17, 2011 8:06 AM
  • Yes the support of WIA is really depending on how well the driver developer write the WIA layer. I suggest you to contact HP for that matter.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, February 17, 2011 3:42 PM