Hello everyone. I need to scan the image without any dialog boxes. Windows 10, two scanners, both canon, for checking did everything just like here
Maybe QT has another way to get image from scanner instead of wia?

Qt Code:
  1. #pragma once
  2. #include "wia_lh.h"
  3. #include "Wia.h"
  4. #include "combaseapi.h"
  5. #include <iostream>
  6. #include <QDebug>
  7. #include "shlwapi.h"
  8. #include "atlbase.h"
  9. #include "wiadef.h"
  10.  
  11. WIAScan::WIAScan(IWiaDevMgr2 ** deviceManager) : wiaItem(new IWiaItem2 *)
  12. {
  13. CoInitialize(NULL);
  14. if (SUCCEEDED(CreateWiaDeviceManager(deviceManager)))
  15. if (SUCCEEDED(EnumerateWiaDevices(*deviceManager)))
  16. if (SUCCEEDED(CreateWiaDevice(*deviceManager, devId, wiaItem)))
  17. if (SUCCEEDED(EnumerateItems(*wiaItem)))
  18. TransferWiaItem(*wiaItem);
  19. else
  20. std::cout << "Enumerate items error";
  21. else
  22. std::cout << "Creation error";
  23. else
  24. std::cout << "Enumerate wia error";
  25. else
  26. std::cout << "Manager error";
  27. }
  28.  
  29. HRESULT WIAScan::CreateWiaDeviceManager(IWiaDevMgr2 **ppWiaDevMgr)
  30. {
  31. *ppWiaDevMgr = NULL;
  32. HRESULT hr = CoCreateInstance(CLSID_WiaDevMgr2, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr2, (void**)ppWiaDevMgr);
  33. return hr;
  34. }
  35.  
  36. HRESULT WIAScan::EnumerateWiaDevices(IWiaDevMgr2 *ppWiaDevMgr)
  37. {
  38. IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
  39. HRESULT hr = ppWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL, &pWiaEnumDevInfo);
  40. if (SUCCEEDED(hr))
  41. {
  42. while (S_OK == hr)
  43. {
  44. IWiaPropertyStorage *pWiaPropertyStorage = NULL;
  45. hr = pWiaEnumDevInfo->Next(1, &pWiaPropertyStorage, NULL);
  46. if (hr == S_OK)
  47. {
  48. ReadSomeWiaProperties(pWiaPropertyStorage);
  49. pWiaPropertyStorage->Release();
  50. pWiaPropertyStorage = NULL;
  51. }
  52. }
  53. if (S_FALSE == hr)
  54. return hr;
  55. pWiaEnumDevInfo->Release();
  56. pWiaEnumDevInfo = NULL;
  57. }
  58. return hr;
  59. }
  60.  
  61. HRESULT WIAScan::ReadSomeWiaProperties(IWiaPropertyStorage *pWiaPropertyStorage)
  62. {
  63. if (NULL == pWiaPropertyStorage)
  64. return E_INVALIDARG;
  65. PROPSPEC PropSpec[3] = { 0 };
  66. PROPVARIANT PropVar[3] = { 0 };
  67. const ULONG c_nPropertyCount = sizeof(PropSpec) / sizeof(PropSpec[0]);
  68. PropSpec[0].ulKind = PRSPEC_PROPID;
  69. PropSpec[0].propid = WIA_DIP_DEV_ID;
  70. PropSpec[1].ulKind = PRSPEC_PROPID;
  71. PropSpec[1].propid = WIA_DIP_DEV_NAME;
  72. PropSpec[2].ulKind = PRSPEC_PROPID;
  73. PropSpec[2].propid = WIA_DIP_DEV_DESC;
  74. HRESULT hr = pWiaPropertyStorage->ReadMultiple(c_nPropertyCount, PropSpec, PropVar);
  75. if (SUCCEEDED(hr))
  76. {
  77. if (VT_BSTR == PropVar[0].vt)
  78. devId = SysAllocString(PropVar[0].bstrVal);
  79. if (VT_BSTR == PropVar[1].vt)
  80. devName = QString::fromWCharArray(PropVar[1].bstrVal);
  81. FreePropVariantArray(c_nPropertyCount, PropVar);
  82. }
  83. return hr;
  84. }
  85.  
  86. HRESULT WIAScan::CreateWiaDevice(IWiaDevMgr2 *pWiaDevMgr, BSTR bstrDeviceID, IWiaItem2 **ppWiaDevice)
  87. {
  88. if (NULL == pWiaDevMgr || NULL == bstrDeviceID || NULL == ppWiaDevice)
  89. return E_INVALIDARG;
  90. *ppWiaDevice = NULL;
  91. HRESULT hr = pWiaDevMgr->CreateDevice(NULL, bstrDeviceID, ppWiaDevice);
  92. return hr;
  93. }
  94.  
  95. HRESULT WIAScan::EnumerateItems(IWiaItem2 *pWiaItem)
  96. {
  97. if (NULL == pWiaItem)
  98. return E_INVALIDARG;
  99. LONG lItemType = 0;
  100. HRESULT hr = pWiaItem->GetItemType(&lItemType);
  101. if (SUCCEEDED(hr))
  102. {
  103. if (lItemType & WiaItemTypeFolder || lItemType & WiaItemTypeHasAttachments)
  104. {
  105. IEnumWiaItem2 *pEnumWiaItem = NULL;
  106. hr = pWiaItem->EnumChildItems(NULL, &pEnumWiaItem);
  107. if (SUCCEEDED(hr))
  108. {
  109. while (S_OK == hr)
  110. {
  111. IWiaItem2 *pChildWiaItem = NULL;
  112. hr = pEnumWiaItem->Next(1, &pChildWiaItem, NULL);
  113. if (S_OK == hr)
  114. {
  115. LONG lItemType = 0;
  116. hr = pChildWiaItem->GetItemType(&lItemType);
  117. if (lItemType & WiaItemTypeFolder || lItemType & WiaItemTypeHasAttachments)
  118. {
  119. hr = EnumerateItems(pChildWiaItem);
  120. PrintCommands(pChildWiaItem);
  121. }
  122. pChildWiaItem->Release();
  123. pChildWiaItem = NULL;
  124. }
  125. }
  126. if (S_FALSE == hr)
  127. hr = S_OK;
  128. pEnumWiaItem->Release();
  129. pEnumWiaItem = NULL;
  130. }
  131. }
  132. }
  133. return hr;
  134. }
  135.  
  136. HRESULT WIAScan::TransferWiaItem(IWiaItem2 *pIWiaItem2)
  137. {
  138. if (NULL == pIWiaItem2)
  139. return E_INVALIDARG;
  140. IWiaTransfer * pWiaTransfer = NULL;
  141. HRESULT hr = pIWiaItem2->QueryInterface(IID_IWiaTransfer, (void**)&pWiaTransfer);
  142. if (SUCCEEDED(hr))
  143. {
  144. CWiaDataCallback * pWiaClassCallback = new CWiaDataCallback;
  145. IWiaItem2 ** ppWiaItemChild = new IWiaItem2 *;
  146. IWiaItem2 * pWiaItemChild = NULL;
  147. CComBSTR bzUploadFileName (L"ImageItem");
  148. hr = pIWiaItem2->CreateChildItem(WiaItemTypeTransfer, 0, bzUploadFileName, ppWiaItemChild);
  149. if (pWiaClassCallback)
  150. {
  151. LONG lItemType = 0;
  152. hr = pIWiaItem2->GetItemType(&lItemType);
  153. if (lItemType & WiaItemTypeTransfer)
  154. {
  155. if ((lItemType & WiaItemTypeFolder))
  156. {
  157. std::cout << "\nI am a folder item";
  158. hr = pWiaTransfer->Download(NULL, pWiaClassCallback);
  159. if (S_OK == hr)
  160. std::cout << "\npWiaTransfer->Download() on folder item SUCCEEDED";
  161. else if (S_FALSE == hr)
  162. std::cout << "\npWiaTransfer->Download() on folder item returned S_FALSE. Folder may not be having child items" << hr;
  163. else if (FAILED(hr))
  164. std::cout << "\npWiaTransfer->Download() on folder item failed" << hr;
  165. }
  166. else if (lItemType & WiaItemTypeFile)
  167. {
  168. hr = pWiaTransfer->Download(0, pWiaClassCallback);
  169. if (S_OK == hr)
  170. std::cout << "\npWiaTransfer->Download() on file item SUCCEEDED";
  171. else if (S_FALSE == hr)
  172. std::cout << "\npWiaTransfer->Download() on file item returned S_FALSE. File may be empty" << hr;
  173. else if (FAILED(hr))
  174. std::cout << "\npWiaTransfer->Download() on file item failed" << hr;
  175. }
  176. }
  177. pWiaClassCallback->Release();
  178. pWiaClassCallback = NULL;
  179. }
  180. else
  181. std::cout << "\nUnable to create CWiaTransferCallback class instance";
  182. pWiaTransfer->Release();
  183. pWiaTransfer = NULL;
  184. }
  185. else
  186. std::cout << "\npIWiaItem2->QueryInterface failed on IID_IWiaTransfer" << hr;
  187. return hr;
  188. }
  189.  
  190. void WIAScan::PrintCommands(IWiaItem2* item)
  191. {
  192. IEnumWIA_DEV_CAPS* caps = 0;
  193. HRESULT h = item->EnumDeviceCapabilities(WIA_DEVICE_COMMANDS, &caps);
  194. if (SUCCEEDED(h))
  195. {
  196. ULONG count = 0;
  197. caps->GetCount(&count);
  198. if (count > 0)
  199. {
  200. WIA_DEV_CAP* cap = new WIA_DEV_CAP[count];
  201. ULONG fetched;
  202. caps->Next(count, cap, &fetched);
  203. for (int i = 0; i < fetched; i++)
  204. std::cout << cap[i].bstrName << "\n";
  205. }
  206. caps->Release();
  207. }
  208. }
To copy to clipboard, switch view to plain text mode 

maybe there are some of my mistakes in the text, basically everything was copied by reference

the problem in connecting to the device, the command output procedure, there are only 4 items in it Syncronize / Delete all items / Delete device tree / Build device tree. I think should be including a takepicture or something like that.

Qt Code:
  1. item-> DeviceCommand (NULL, & WIA_CMD_BUILD_DEVICE_TREE, & item);
To copy to clipboard, switch view to plain text mode 
- it works, but does not change anything in the tree

through
Qt Code:
  1. item-> DeviceDialog (........)
To copy to clipboard, switch view to plain text mode 
the standard wia window is launched, scanning works through it, but I need to do it without it

Qt Code:
  1. item-> CreateChildItem (WiaItemTypeImage, NULL, imgName, wiaImageItem);
To copy to clipboard, switch view to plain text mode 
- E_NOTIMPL

Qt Code:
  1. item-> DeviceCommand (NULL, & WIA_CMD_TAKE_PICTURE, wiaImageItem);
To copy to clipboard, switch view to plain text mode 
- respectively, from the previous command, also does not work, maybe some kind of Com port initialization is missing, the device name, its id and driver description are read