#pragma once
#include "wia_lh.h"
#include "Wia.h"
#include "combaseapi.h"
#include <iostream>
#include <QDebug>
#include "shlwapi.h"
#include "atlbase.h"
#include "wiadef.h"
WIAScan::WIAScan(IWiaDevMgr2 ** deviceManager) : wiaItem(new IWiaItem2 *)
{
CoInitialize(NULL);
if (SUCCEEDED(CreateWiaDeviceManager(deviceManager)))
if (SUCCEEDED(EnumerateWiaDevices(*deviceManager)))
if (SUCCEEDED(CreateWiaDevice(*deviceManager, devId, wiaItem)))
if (SUCCEEDED(EnumerateItems(*wiaItem)))
TransferWiaItem(*wiaItem);
else
std::cout << "Enumerate items error";
else
std::cout << "Creation error";
else
std::cout << "Enumerate wia error";
else
std::cout << "Manager error";
}
HRESULT WIAScan::CreateWiaDeviceManager(IWiaDevMgr2 **ppWiaDevMgr)
{
*ppWiaDevMgr = NULL;
HRESULT hr = CoCreateInstance(CLSID_WiaDevMgr2, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)ppWiaDevMgr);
return hr;
}
HRESULT WIAScan::EnumerateWiaDevices(IWiaDevMgr2 *ppWiaDevMgr)
{
IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
HRESULT hr = ppWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo);
if (SUCCEEDED(hr))
{
while (S_OK == hr)
{
IWiaPropertyStorage *pWiaPropertyStorage = NULL;
hr = pWiaEnumDevInfo->Next(1, &pWiaPropertyStorage, NULL);
if (hr == S_OK)
{
ReadSomeWiaProperties(pWiaPropertyStorage);
pWiaPropertyStorage->Release();
pWiaPropertyStorage = NULL;
}
}
if (S_FALSE == hr)
return hr;
pWiaEnumDevInfo->Release();
pWiaEnumDevInfo = NULL;
}
return hr;
}
HRESULT WIAScan::ReadSomeWiaProperties(IWiaPropertyStorage *pWiaPropertyStorage)
{
if (NULL == pWiaPropertyStorage)
return E_INVALIDARG;
PROPSPEC PropSpec[3] = { 0 };
PROPVARIANT PropVar[3] = { 0 };
const ULONG c_nPropertyCount = sizeof(PropSpec) / sizeof(PropSpec[0]);
PropSpec[0].ulKind = PRSPEC_PROPID;
PropSpec[0].propid = WIA_DIP_DEV_ID;
PropSpec[1].ulKind = PRSPEC_PROPID;
PropSpec[1].propid = WIA_DIP_DEV_NAME;
PropSpec[2].ulKind = PRSPEC_PROPID;
PropSpec[2].propid = WIA_DIP_DEV_DESC;
HRESULT hr = pWiaPropertyStorage->ReadMultiple(c_nPropertyCount, PropSpec, PropVar);
if (SUCCEEDED(hr))
{
if (VT_BSTR == PropVar[0].vt)
devId = SysAllocString(PropVar[0].bstrVal);
if (VT_BSTR == PropVar[1].vt)
devName
= QString::fromWCharArray(PropVar
[1].
bstrVal);
FreePropVariantArray(c_nPropertyCount, PropVar);
}
return hr;
}
HRESULT WIAScan::CreateWiaDevice(IWiaDevMgr2 *pWiaDevMgr, BSTR bstrDeviceID, IWiaItem2 **ppWiaDevice)
{
if (NULL == pWiaDevMgr || NULL == bstrDeviceID || NULL == ppWiaDevice)
return E_INVALIDARG;
*ppWiaDevice = NULL;
HRESULT hr = pWiaDevMgr->CreateDevice(NULL, bstrDeviceID, ppWiaDevice);
return hr;
}
HRESULT WIAScan::EnumerateItems(IWiaItem2 *pWiaItem)
{
if (NULL == pWiaItem)
return E_INVALIDARG;
LONG lItemType = 0;
HRESULT hr = pWiaItem->GetItemType(&lItemType);
if (SUCCEEDED(hr))
{
if (lItemType & WiaItemTypeFolder || lItemType & WiaItemTypeHasAttachments)
{
IEnumWiaItem2 *pEnumWiaItem = NULL;
hr = pWiaItem->EnumChildItems(NULL, &pEnumWiaItem);
if (SUCCEEDED(hr))
{
while (S_OK == hr)
{
IWiaItem2 *pChildWiaItem = NULL;
hr = pEnumWiaItem->Next(1, &pChildWiaItem, NULL);
if (S_OK == hr)
{
LONG lItemType = 0;
hr = pChildWiaItem->GetItemType(&lItemType);
if (lItemType & WiaItemTypeFolder || lItemType & WiaItemTypeHasAttachments)
{
hr = EnumerateItems(pChildWiaItem);
PrintCommands(pChildWiaItem);
}
pChildWiaItem->Release();
pChildWiaItem = NULL;
}
}
if (S_FALSE == hr)
hr = S_OK;
pEnumWiaItem->Release();
pEnumWiaItem = NULL;
}
}
}
return hr;
}
HRESULT WIAScan::TransferWiaItem(IWiaItem2 *pIWiaItem2)
{
if (NULL == pIWiaItem2)
return E_INVALIDARG;
IWiaTransfer * pWiaTransfer = NULL;
HRESULT hr = pIWiaItem2->QueryInterface(IID_IWiaTransfer, (void**)&pWiaTransfer);
if (SUCCEEDED(hr))
{
CWiaDataCallback * pWiaClassCallback = new CWiaDataCallback;
IWiaItem2 ** ppWiaItemChild = new IWiaItem2 *;
IWiaItem2 * pWiaItemChild = NULL;
CComBSTR bzUploadFileName (L"ImageItem");
hr = pIWiaItem2->CreateChildItem(WiaItemTypeTransfer, 0, bzUploadFileName, ppWiaItemChild);
if (pWiaClassCallback)
{
LONG lItemType = 0;
hr = pIWiaItem2->GetItemType(&lItemType);
if (lItemType & WiaItemTypeTransfer)
{
if ((lItemType & WiaItemTypeFolder))
{
std::cout << "\nI am a folder item";
hr = pWiaTransfer->Download(NULL, pWiaClassCallback);
if (S_OK == hr)
std::cout << "\npWiaTransfer->Download() on folder item SUCCEEDED";
else if (S_FALSE == hr)
std::cout << "\npWiaTransfer->Download() on folder item returned S_FALSE. Folder may not be having child items" << hr;
else if (FAILED(hr))
std::cout << "\npWiaTransfer->Download() on folder item failed" << hr;
}
else if (lItemType & WiaItemTypeFile)
{
hr = pWiaTransfer->Download(0, pWiaClassCallback);
if (S_OK == hr)
std::cout << "\npWiaTransfer->Download() on file item SUCCEEDED";
else if (S_FALSE == hr)
std::cout << "\npWiaTransfer->Download() on file item returned S_FALSE. File may be empty" << hr;
else if (FAILED(hr))
std::cout << "\npWiaTransfer->Download() on file item failed" << hr;
}
}
pWiaClassCallback->Release();
pWiaClassCallback = NULL;
}
else
std::cout << "\nUnable to create CWiaTransferCallback class instance";
pWiaTransfer->Release();
pWiaTransfer = NULL;
}
else
std::cout << "\npIWiaItem2->QueryInterface failed on IID_IWiaTransfer" << hr;
return hr;
}
void WIAScan::PrintCommands(IWiaItem2* item)
{
IEnumWIA_DEV_CAPS* caps = 0;
HRESULT h = item->EnumDeviceCapabilities(WIA_DEVICE_COMMANDS, &caps);
if (SUCCEEDED(h))
{
ULONG count = 0;
caps->GetCount(&count);
if (count > 0)
{
WIA_DEV_CAP* cap = new WIA_DEV_CAP[count];
ULONG fetched;
caps->Next(count, cap, &fetched);
for (int i = 0; i < fetched; i++)
std::cout << cap[i].bstrName << "\n";
}
caps->Release();
}
}
Bookmarks