2,640
社区成员
发帖
与我相关
我的任务
分享
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;
}