Problem with Automation Typelibaray for WIA

G

Guest

Hi!

I want to scan a multipage-document into
a TIFF-file from C#. The scanner has WIA-support
and a feeder.

The following code works:

IDeviceManager mng=new DeviceManagerClass();
foreach(IDeviceInfo i in mng.DeviceInfos)
{
MessageBox.Show(i.Type.ToString()+" "+i.DeviceID);
IDevice dev=i.Connect();
foreach(IItem item in dev.Items)
{
MessageBox.Show(item.ItemID);
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"/*WIA
..FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");
}
}

But I had to use foreach and will scan from every scanner
around which makes no sense.

The code should have been:
IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos[0].Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

However this won't compile:

d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(91,4): error CS1546: Die
Eigenschaft oder der Indexer "this" wird von der Sprache nicht unterstützt
wird. Versuchen Sie direkt die Accessor-Methode
"WIA.IDeviceInfos.get_Item(ref object)" aufzurufen.
d:\usr\prj\WIABrotherTest\WIABrotherTest\bin\Debug\Interop.WIA.dll:
(Verknüpfte Datei)

If I change it into:

IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos.get_Item(0).Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

I will get this compiler error:

d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,16): error CS1502: Die
beste Übereinstimmung für die überladene Methode
'WIA.IDeviceInfos.get_Item(ref object)' hat einige ungültige Argumente
d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,41): error CS1503:
Argument '1': kann nicht von 'int' zu 'ref object' konvertiert werden

It says it can't convert int into ref object

How comes this?

How can I use the WIA-automation library easily from .NET/C#?

BTW:
I went to MS-Downloadcenter, searched for "WIA automation" and downloaded
the sdk.
I used regsvr32 to register the dll and then added a reference to "Wia
automation V2"
in my C#-project. Then the above code can be compiled and works under my XP
SP1^

Many thanks in advance
 
M

Mattias Sjögren

If I change it into:

IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos.get_Item(0).Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

I will get this compiler error:

d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,16): error CS1502: Die
beste Übereinstimmung für die überladene Methode
'WIA.IDeviceInfos.get_Item(ref object)' hat einige ungültige Argumente
d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,41): error CS1503:
Argument '1': kann nicht von 'int' zu 'ref object' konvertiert werden

It says it can't convert int into ref object


Try it like this

object index = 0;
IDevice dev=mng.DeviceInfos.get_Item(ref index).Connect();



Mattias
 
T

Tian Min Huang

Hi,

I'd like to follow up with you and see if there is any further I can help.
In the meantime, please kindly note that Windows Image Acquisition
Automation Library v2.0 is only supported on Windows XP with Service Pack 1
installed. Please refer to the following page:

Windows Image Acquisition Automation Library v2.0 Tool: Image acquisition
and manipulation component for VB and scripting
http://www.microsoft.com/downloads/details.aspx?FamilyID=a332a77a-01b8-4de6-
91c2-b7ea32537e29&DisplayLang=en

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi!

Thanks for your answer!
If I change it into:

IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos.get_Item(0).Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

I will get this compiler error:

d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,16): error CS1502: Die
beste Übereinstimmung für die überladene Methode
'WIA.IDeviceInfos.get_Item(ref object)' hat einige ungültige Argumente
d:\usr\prj\wiabrothertest\wiabrothertest\form1.cs(121,41): error CS1503:
Argument '1': kann nicht von 'int' zu 'ref object' konvertiert werden

It says it can't convert int into ref object


Try it like this

object index = 0;
IDevice dev=mng.DeviceInfos.get_Item(ref index).Connect();

Then I'll get a 'System.Runtime.InteropServices.COMException'.
The HRESULT is 0x8021006B.

How comes that the WIA library is not imported correctly? I think that
IDevice dev=mng.DeviceInfos[0].Connect(); SHOULD work.
 
G

Guest

Hi!
I'd like to follow up with you and see if there is any further I can help.

It would be nice to know why the created interop-assembly works not as
exspected
from C#.
In the meantime, please kindly note that Windows Image Acquisition
Automation Library v2.0 is only supported on Windows XP with Service Pack 1
installed.

I know that and am using and developing only for XP-SP1.
Windows Image Acquisition Automation Library v2.0 Tool: Image acquisition
and manipulation component for VB and scripting
http://www.microsoft.com/downloads/details.aspx?FamilyID=a332a77a-01b8-4de6-
91c2-b7ea32537e29&DisplayLang=en

This is exactly the package I downloaded.

Whoever reads this from the archives: Grab this package to get an easy but
powerfull API for scanners, digital cameras and webcams!
 
M

Mattias Sjögren

Then I'll get a 'System.Runtime.InteropServices.COMException'.
The HRESULT is 0x8021006B.

Can't help you with that, sorry. It seems to be a HRESULT specific to
the component you're calling.

How comes that the WIA library is not imported correctly? I think that
IDevice dev=mng.DeviceInfos[0].Connect(); SHOULD work.

Because the index parameter to the the Item method is apparently
passed by reference (which is possible and fairly common on COM), but
..NET requires indexer parameters to be passed by value. Therefore the
method can't be imported as a proper indexer, and you have to use the
get_Item syntax instead.



Mattias
 
T

Tian Min Huang

Hi,

I believe the following MSDN articles are helpful for troublshooting .NET
Interop. Though some samples are given in Visual Basic .NET, they can be
converted to C# easily.

Troubleshooting .NET Interoperability
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchTroubleshootingNETInteroperability.asp

Exposing COM Components to the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

For the further investigation, could you post a simple project and tell me
the detailed steps to reproduce the problem? I will be glad to check it on
my side.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi,

I believe the following MSDN articles are helpful for troublshooting .NET
Interop. Though some samples are given in Visual Basic .NET, they can be
converted to C# easily.

Troubleshooting .NET Interoperability
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechar
t/html/vbtchTroubleshootingNETInteroperability.asp

Exposing COM Components to the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconconsumingunmanageddllfunctions.asp

For the further investigation, could you post a simple project and tell me
the detailed steps to reproduce the problem? I will be glad to check it on
my side.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi!
I believe the following MSDN articles are helpful for troublshooting .NET
Interop. Though some samples are given in Visual Basic .NET, they can be
converted to C# easily.

I will look at these articles at the end of the week because I only have
limited internet access on a fair this week. I wanted to let you know how
to create a project:
For the further investigation, could you post a simple project and tell me
the detailed steps to reproduce the problem? I will be glad to check it on
my side.

I already posted a snippet code in the first post.

But creating the project is quite easy so I hope it is okay that I don't
post a zip-file at this moment:

-Create a Winforms-C#-project
-Place a button on the form
-doubleclick it
-paste the following code:

IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos[0].Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

-Add "using WIA;" to the top
-Use "Add reference" and choose the WIA-Automation Library Version 2
(Before that you have to download the SDK und type regsvr32.exe wiaaut.dll)

This should reproduce the problem.

Many thanks for your help!
 
G

Guest

Hi!
I believe the following MSDN articles are helpful for troublshooting .NET
Interop. Though some samples are given in Visual Basic .NET, they can be
converted to C# easily.

I will look at these articles at the end of the week because I only have
limited internet access on a fair this week. I wanted to let you know how
to create a project:
For the further investigation, could you post a simple project and tell me
the detailed steps to reproduce the problem? I will be glad to check it on
my side.

I already posted a snippet code in the first post.

But creating the project is quite easy so I hope it is okay that I don't
post a zip-file at this moment:

-Create a Winforms-C#-project
-Place a button on the form
-doubleclick it
-paste the following code:

IDeviceManager mng=new DeviceManagerClass();
IDevice dev=mng.DeviceInfos[0].Connect();
IItem item=dev.Items[0];
IImageFile
file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
/*WIA.FormatID. .wiaFormatTIFF*/);
file.SaveFile("C:\\file.tif");

-Add "using WIA;" to the top
-Use "Add reference" and choose the WIA-Automation Library Version 2
(Before that you have to download the SDK und type regsvr32.exe wiaaut.dll)

This should reproduce the problem.

Many thanks for your help!
 
T

Tian Min Huang

Hi,

Thanks for your response. I am checking this issue and will update you with
my information.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tian Min Huang

Hi,

I reproduced the problem on my side, and now I'd like to share the
following infomration with you after further research:

We are not able to call "mng.DeviceInfos[0]", because DeviceInfos does not
accept to retrieves the specified item in the collection. According to the
documentation on "DeviceInfos.Item(Index)", the parameter Index is of
Variant value instead of integer. It's the same if you check the ildasm of
it:

method public hidebysig newslot specialname abstract virtual
instance class WIA.DeviceInfo
marshal( interface)
get_Item([in] object& marshal( struct) Index) runtime managed
internalcall
{
.custom instance void
[mscorlib]System.Runtime.InteropServices.DispIdAttribute::.ctor(int32) = (
01 00 00 00 00 00 00 00 )
} // end of method IDeviceInfos::get_Item

So we have to declare an object variable with which to call get_Item().

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Joined
Aug 10, 2006
Messages
1
Reaction score
0
> >
> > IDeviceManager mng=new DeviceManagerClass();
> > IDevice dev=mng.DeviceInfos.get_Item(0).Connect();
> > IItem item=dev.Items[0];
> > IImageFile
> >file=(IImageFile)item.Transfer("{00000000-0000-0000-0000-000000000000}"
> > /*WIA.FormatID. .wiaFormatTIFF*/);
> > file.SaveFile("C:\\file.tif");
> >
> >It says it can't convert int into ref object

> Try it like this
>
> object index = 0;
> IDevice dev=mng.DeviceInfos.get_Item(ref index).Connect();
>


Then I'll get a 'System.Runtime.InteropServices.COMException'.
The HRESULT is 0x8021006B.

How comes that the WIA library is not imported correctly? I think that
IDevice dev=mng.DeviceInfos[0].Connect(); SHOULD work.

3 years too late probably isn't much help for the OP, but in case anyone else comes here, I figured this out. I was getting "HRESULT is 0x8021006B" myself, when trying to access the Items collection from my WIA Device object.

I banged my head against the wall for several hours, then I remembered that VB arrays are 1-based. Then I didn't get "HRESULT is 0x8021006B" anymore. :)

WIA.Item item = dev.Items[1] as WIA.Item;
 
Top