Listing WIA devices in C++/CLI fails with “cannot convert from 'int' to 'System::Object ^%'”

StackOverflow https://stackoverflow.com/questions/12068528

  •  27-06-2021
  •  | 
  •  

Question

I'm trying to list all WIA devices with C++/CLI. I'm fairly new to C++/CLI (although I consider myself an intermediate C++ programmer), but I keep getting this error:

error C2664: 'WIA::IDeviceInfos::default::get' : cannot convert parameter 1 from 'int' to 'System::Object ^%'

when using the following code snippet:

DeviceManager^ dm = (gcnew WIA::DeviceManager());
for (int i = 1; i <= dm->DeviceInfos->Count; i++)
{
String^ deviceName = dm->DeviceInfos[i].Properties("Name")->get_Value()->ToString();
this->devices->Items->Add(deviceName);
}

Why should I treat that int as an Object? In Managed C++ there was the concept of boxing, but it doesn't work here and anyway I thought C++/CLI was introduced in order to get rid of it?

Was it helpful?

Solution

The Value property needs some non-obvious code to get it out. Try this:

WIA::DeviceInfo ^ info = dm->DeviceInfos[gcnew System::Int32(i)];
WIA::Property ^ propName = info->Properties[gcnew System::String(L"Name")];
String ^ strName = propName->default->ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top