WIA,无法枚举本机系统中的扫描仪

马少华 马少华 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先上代码,几乎是抄的msdn上的。
HRESULT CreateWiaDeviceManager(IWiaDevMgr2 **ppWiaDevMgr) //Vista or later
{
//
// Validate arguments
//
if (NULL == ppWiaDevMgr)
{
return E_INVALIDARG;
}

//
// Initialize out variables
//
*ppWiaDevMgr = NULL;

//
// Create an instance of the device manager
//

//XP or earlier:
//HRESULT hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr, (void**)ppWiaDevMgr);

//Vista or later:
HRESULT hr = CoCreateInstance(CLSID_WiaDevMgr2, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)ppWiaDevMgr);

//
// Return the result of creating the device manager
//
return hr;
}

HRESULT EnumerateWiaDevices(IWiaDevMgr2 *pWiaDevMgr) //Vista or later
{
//
// Validate arguments
//
if (NULL == pWiaDevMgr)
{
return E_INVALIDARG;
}

//
// Get a device enumerator interface
//
IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
HRESULT hr = pWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo);
if (SUCCEEDED(hr))
{
//
// Loop until you get an error or pWiaEnumDevInfo->Next returns
// S_FALSE to signal the end of the list.
//
while (S_OK == hr)
{

u_long dicount = 0;
hr = pWiaEnumDevInfo->GetCount(&dicount);
//此处hr返回s_ok,但dicount为0,但我电脑上确实连接了一个扫描仪,驱动也装好了,可以正常使用
//
// Get the next device's property storage interface pointer
//
IWiaPropertyStorage *pWiaPropertyStorage = NULL;
hr = pWiaEnumDevInfo->Next(1, &pWiaPropertyStorage, NULL);
//这里直接返回S_FAILED
//
// pWiaEnumDevInfo->Next will return S_FALSE when the list is
// exhausted, so check for S_OK before using the returned
// value.
//
if (hr == S_OK)
{
//
// Do something with the device's IWiaPropertyStorage*
//

PROPSPEC PropSpec[3] = { 0 };
PROPVARIANT PropVar[3] = { 0 };

const u_long propcount = sizeof(PROPSPEC) / sizeof(PropSpec[0]);

//
// Define which properties you want to read:
// Device ID. This is what you would use to create
// the device.
//
PropSpec[0].ulKind = PRSPEC_PROPID;
PropSpec[0].propid = WIA_DIP_DEV_ID;

//
// Device Name
//
PropSpec[1].ulKind = PRSPEC_PROPID;
PropSpec[1].propid = WIA_DIP_DEV_NAME;

//
// Device description
//
PropSpec[2].ulKind = PRSPEC_PROPID;
PropSpec[2].propid = WIA_DIP_DEV_DESC;


hr = pWiaPropertyStorage->ReadMultiple(propcount, PropSpec, PropVar);
if (SUCCEEDED(hr))
{
//
// IWiaPropertyStorage::ReadMultiple will return S_FALSE if some
// properties could not be read, so you have to check the return
// types for each requested item.
//

//
// Check the return type for the device ID
//
if (VT_BSTR == PropVar[0].vt)
{
//
// Do something with the device ID
//
_tprintf(TEXT("WIA_DIP_DEV_ID: %ws\n"), PropVar[0].bstrVal);
}

//
// Check the return type for the device name
//
if (VT_BSTR == PropVar[1].vt)
{
//
// Do something with the device name
//
_tprintf(TEXT("WIA_DIP_DEV_NAME: %ws\n"), PropVar[1].bstrVal);
}

//
// Check the return type for the device description
//
if (VT_BSTR == PropVar[2].vt)
{
//
// Do something with the device description
//
_tprintf(TEXT("WIA_DIP_DEV_DESC: %ws\n"), PropVar[2].bstrVal);
}

//
// Free the returned PROPVARIANTs
//
FreePropVariantArray(propcount, PropVar);

}

//
// Release the device's IWiaPropertyStorage*
//
pWiaPropertyStorage->Release();
pWiaPropertyStorage = NULL;
}
}

//
// If the result of the enumeration is S_FALSE (which
// is normal), change it to S_OK.
//
if (S_FALSE == hr)
{
hr = S_OK;
}

//
// Release the enumerator
//
pWiaEnumDevInfo->Release();
pWiaEnumDevInfo = NULL;
}

//
// Return the result of the enumeration
//
return hr;
}




int main()
{
CoInitialize(NULL);
IWiaDevMgr2 *pWait;
HRESULT hr = CreateWiaDeviceManager(&pWait);
if (hr != S_OK)
{
_tprintf(_T("create devicemanager error %d"), GetLastError());
CoUninitialize();
return -1;
}

EnumerateWiaDevices(pWait);
getchar();
pWait->Release();
CoUninitialize();
return 0;
}


请用过wia的大牛指点一二。
...全文

相关推荐

wiasane, 扫描仪访问现在轻松WIA驱动程序.zip

wiasane, 扫描仪访问现在轻松WIA驱动程序 WiaSaneWiaSane项目提供了 Windows 图像采集( WIA ) 驱动程序,通过扫描仪访问扫描仪设备实现图像采集现在简单的( 理智) 网络协议。这个WIA驱动程序在正常的网络协议之上实现一个健全的前端。 它被称为扫描仪访

weixin_38744270

C#结合WIA实现本地扫描仪图像获取的完整程序设计与实战

经过这么多技术细节的洗礼,我想说的是:成功的图像采集系统,不在于用了多高级的API,而在于如何优雅地应对各种不完美。操作系统会更新,驱动会出bug,用户会误操作,设备会老化。我们的代码不能追求“绝对正确”,而要追求“足够健壮”。就像一位老师傅修车,他不会指望每个零件都完美无瑕,而是知道哪个螺丝松一点没关系,哪个垫片磨平了还能凑合用几天。我们也应该成为这样的“系统医生”,懂得何时该坚持标准,何时该灵活变通。

csp1223

C# 实现.NET 调用扫描仪的源码教程

WIA,即Windows Image Acquisition,是微软提供的一种服务,允许应用程序通过扫描仪和其他影像输入设备进行图像和数据的获取。WIA服务为.NET框架中的设备交互提供了一种更为简便的方式,尤其是在处理图像相关的硬件设备时。该服务通过一组COM组件实现,使开发者能够以编程的方式访问和控制影像设备。COM,即组件对象模型(Component Object Model),是一种由微软公司开发的软件架构,用于使得各种软件组件通过二进制接口进行交互。

Compass宁

C#利用WIA获取扫描仪图片

C#利用WIA获取扫描仪图片并保存到picturebox中 两种保存方式。

wenqiiori1

c# winform wia 调用扫描仪

c# winform wia 调用扫描仪demo,c# winform wia 调用扫描仪demo

mrlaoxia
评论 7
收藏 已收藏
分享
返回顶部
马少华
硬件/系统 2,644 成员 17,229 内容