WIA.DeviceManager.DeviceInfos.Count = 0. Why?

I'm trying to identify the scanners attached to the computer. One of the possible solutions is to use WIA (Windows Image Acquisition Automation Library).

These were my actions so far:

  • Download wiaaut.dll
  • Copy it to system32
  • Register it with "regsvr32 wiaaut.dll" (successfully)
  • Add reference to my project in Visual Studio.NET
  • Check that the Windows Image Acquisition (WIA) service is running

Next, I add and debug the following code:

WIA.DeviceManager manager = new WIA.DeviceManagerClass();
string deviceName = "";
foreach (WIA.DeviceInfo info in manager.DeviceInfos)
{
    if (info.Type == WIA.WiaDeviceType.ScannerDeviceType)
    {
        foreach (WIA.Property p in info.Properties)
        {
            if (p.Name == "Name")
                {
                    deviceName = ((WIA.IProperty)p).get_Value().ToString();
                    Console.WriteLine(deviceName);
                }
        }
    }
}

However, the manager.DeviceInfos is always empty. I have 2 scanners attached, one of them shows in Control Panel->Scanners and Cameras, one doesn't, and both show under "Imaging Devices" in Device manager.

Any suggestion on why none are appearing in WIA.DeviceManager.DeviceInfos?

Running on Windows XP Service Pack 2

Answers


try it with this class:

using System;
using System.Collections.Generic;
using System.Text;
using WIA;
using WIAVIDEOLib;
namespace Scanner
{
public class ImageAcquisition
{

    private WIALib.WiaClass WiaClass;
    private WIALib.ItemClass ItemClass;
    private WIALib.CollectionClass CollectionClassDevices;
    private WIALib.CollectionClass CollectionClassPics;


    #region SelectDevice
    public bool SelectDevice()
    {
        try
        {
            object selectUsingUI;

            WiaClass = new WIALib.WiaClass();
            CollectionClassDevices = (WIALib.CollectionClass)WiaClass.Devices;

            if (WiaClass.Devices.Count == 0)
                return false;

            selectUsingUI = System.Reflection.Missing.Value;

            ItemClass = (WIALib.ItemClass)WiaClass.Create(ref selectUsingUI);

            if (ItemClass == null)
                return false;

            return true;
        }
        catch (System.Exception exp)
        {
            return false;
        }
    }
    #endregion

    #region Capture
    public System.Drawing.Image Capture()
    {
        try
        {
            CollectionClassPics = ItemClass.GetItemsFromUI(WIALib.WiaFlag.SingleImage, WIALib.WiaIntent.ImageTypeColor) as WIALib.CollectionClass;
            if (CollectionClassPics == null)
                return null;

            ItemClass = (WIALib.ItemClass)System.Runtime.InteropServices.Marshal.CreateWrapperOfType(CollectionClassPics[0], typeof(WIALib.ItemClass));
            string imageFileName = System.IO.Path.GetTempFileName();
            ItemClass.Transfer(imageFileName, false);
            System.Drawing.Image Image = System.Drawing.Image.FromFile(imageFileName);

            System.Runtime.InteropServices.Marshal.ReleaseComObject(CollectionClassPics[0]);
            return Image;
        }
        catch (System.Exception exp)
        {
            return null;
        }
    }
    #endregion
}

}


 foreach (WIA.Property p in info.Properties)
     {
         if (p.Name == "Name") <-- p is a property why cast like you doing above.
         {
             deviceName = ((WIA.IProperty)p).get_Value().ToString();
             Console.WriteLine(deviceName);
         }
     }

try this:

deviceName = p.get_Value();

this will show like a error on visual studio but when you press f5 will compile. and will run..


I work with this in java so maybe my hint isnt correct, but i use your mentioned way only for windows vista and later... so it looks like you are using wia 2.0 but for windows ME and XP you should use wia 1.0

At MSDN it is described.. maybe it will help


Try change the line :

foreach (WIA.DeviceInfo info in manager.DeviceInfos)

and replace with:

foreach (manager.DeviceInfo info in manager.DeviceInfos)

I hope i helped you.


Need Your Help

Running app on ipad asks for System username and password again and again

ios xcode osx ipad

When I am running my application on my device, it asks for system username and password again and again. Previously, I was running on iphone, it asked for first time only.

Unix system programming - unzipping a file programmatically

c unzip systems-programming

What I need to do is unzip a file, (.gz or .z), read the first line and do some stuff according to the first line read. But the C standard library doesn't seem to offer a way to do this.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.