1

I have HP Scanner Scanjet 5590 attached to my system. I want to know the status of the scanner via c#.net. How to do it? The status are :

on/off, idle, scanning, etc..

Update: Also I have tried using WMI but it is not detecting it. In the control panel of Win-7 the scanner is shown under the head DEVICES and not under the head PRINTERS.

Kindly help.

HotTester
  • 5,170
  • 15
  • 57
  • 93
  • 1
    I guess you already saw http://stackoverflow.com/questions/296182/how-to-get-printer-info-in-net. Doesn't that answer your question? – Ando Jun 18 '12 at 06:02
  • The problem is the WMI is not showing the HP Scanner as a device. It is showing in the Win-7 control panel as in Devices and Scanner option under the Devices. When i query via WMI it is not showing it. – HotTester Jun 19 '12 at 05:29

3 Answers3

4

u can use WMI to get information like this. See this post How to get Printer Info in .NET? as a reference.

Update: Try to search for PnP entities:

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_PnPEntity");
            ManagementObjectCollection coll = searcher.Get();

            foreach (ManagementObject any in coll)
            {
                   // Check for device name
            }

This should give you a list of all devices.

Community
  • 1
  • 1
Stefan Keller
  • 352
  • 1
  • 7
  • The problem is the WMI is not showing the HP Scanner as a device. It is showing in the Win-7 control panel as in Devices and Scanner option under the Devices. When i query via WMI it is not showing it. – HotTester Jun 19 '12 at 05:29
2

You could try to use the Windows Image Acquisition (WIA) API. WIA 2.0 was released with Windows Vista and is mainly targeted towards scanners. It is still supported for Windows 7. I tested it with a HP Scanjet 4670 scanner a while back. Your scanner should be compatible with the WIA API.

To access WIA 2.0, you’ll need to add a reference to the COM library “Microsoft Windows Image Acquisition Library v2.0″.

enter image description here

Once you have added the reference you can enumerate the WIA compatible devices.

var deviceManager = new DeviceManager();    
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
    var deviceName = 
        deviceManager.DeviceInfos[i].Properties["Name"].get_Value().ToString();

    // Is the device a scanner?
    if (deviceManager.DeviceInfos[i].Type == WiaDeviceType.ScannerDeviceType)
    {
        //...etc.
    }
}

Remark: Be sure to treat the DeviceInfos array as a 1-baed array instead of a zero-based array! You'll get COM exceptions if you don't.

When you find your scanner in the DeviceInfos[...] array you can connect to it.

DeviceInfo deviceInfo = deviceManager.DeviceInfos[1];
deviceInfo.Connect();

Once connected you can operate it. Let's scan an image.

// Start the scan   
var item = deviceInfo.Items[1];
var imageFile = (ImageFile) item.Transfer(FormatID.wiaFormatJPEG);

You can find more information on the above here:

Windows Image Acquisition (WIA)

Using the WIA API you are at least able to detect if the scanner is attached to your system and is powered on. That deals with the on/off issue.

You can also use WIA to query the device properties.

Scanner Device Property Constants

The following device property might interest you:

WIA_DPS_DOCUMENT_HANDLING_STATUS: Contains current state of the scanner's installed flatbed, document feeder, or duplexer (ready, paper jam, lamp error...etc.).

Query the WIA_DPS_DOCUMENT_HANDLING_STATUS to check the current status of the scanner.

For example:

class WIA_PROPERTIES
{
    public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const uint WIA_DIP_FIRST = 2;
    public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;

    // Scanner only device properties
    public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
}

Property documentHandlingStatus = null;
foreach (Property property in device.Properties)
{
    string propertyName = property.Name;
    string propertyValue = property.get_Value().ToString();

    if (property.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
    {
       // ...
    }
}

Checkout Microsoft's WiaDef.h header file for the values of these device property constants.

WiaDef.h

Christophe Geers
  • 7,468
  • 3
  • 31
  • 47
0
string printerName = "HP Scanner Scanjet 5590"; 
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName); 
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); 
ManagementObjectCollection coll = searcher.Get();  
foreach (ManagementObject printer in coll)
{     
    foreach (PropertyData property in printer.Properties)     
    {         
        Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));     
    } 
}
Kishore Kumar
  • 11,843
  • 26
  • 86
  • 149