Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
7.1 KiB

  1. // devctrl.cpp : Implementation of CDeviceControl
  2. #include "pch.h"
  3. #include "wiafb.h"
  4. #include "devctrl.h"
  5. #define IOCTL_EPP_WRITE 0x85 // remove at a later data.. Visioneer specific ( this is for
  6. // proof of concept.)
  7. #define IOCTL_EPP_READ 0x84
  8. /////////////////////////////////////////////////////////////////////////////
  9. // CDeviceControl
  10. STDMETHODIMP CDeviceControl::RawWrite(LONG lPipeNum,VARIANT *pvbuffer,LONG lbuffersize,LONG lTimeout)
  11. {
  12. HRESULT hr = E_FAIL;
  13. /*
  14. UINT uiBufferLen = 0;
  15. CHAR *pData = NULL;
  16. DWORD dwBytesWritten = 0;
  17. switch (pvbuffer->vt) {
  18. case VT_BSTR:
  19. {
  20. if(NULL != pvbuffer->bstrVal){
  21. uiBufferLen = WideCharToMultiByte(CP_ACP, 0, pvbuffer->bstrVal, -1, NULL, NULL, 0, 0);
  22. if (!uiBufferLen) {
  23. // SetLastErrorCode
  24. }
  25. pData = new CHAR[uiBufferLen+1];
  26. if (!pData) {
  27. // SetLastErrorCode
  28. return E_OUTOFMEMORY;
  29. }
  30. WideCharToMultiByte(CP_ACP, 0, pvbuffer->bstrVal, -1, pData, uiBufferLen, 0, 0);
  31. //
  32. // send data to device
  33. //
  34. // DeviceIOControl(....)
  35. if(!WriteFile(m_pScannerSettings->DeviceIOHandles[lPipeNum],
  36. pData,
  37. lbuffersize,
  38. &dwBytesWritten,NULL)){
  39. // SetLastErrorCode
  40. }
  41. //
  42. // delete any allocated memory
  43. //
  44. delete pData;
  45. } else {
  46. // SetLastErrorCode
  47. }
  48. }
  49. break;
  50. default:
  51. hr = E_FAIL;
  52. break;
  53. }
  54. */
  55. return hr;
  56. }
  57. STDMETHODIMP CDeviceControl::RawRead(LONG lPipeNum,VARIANT *pvbuffer,LONG lbuffersize,LONG *plbytesread,LONG lTimeout)
  58. {
  59. HRESULT hr = S_OK;
  60. WCHAR wszBuffer[255];
  61. CHAR *pBuffer = NULL;
  62. DWORD dwBytesRead = 0;
  63. VariantClear(pvbuffer);
  64. //
  65. // clean out buffer
  66. //
  67. memset(wszBuffer,0,sizeof(wszBuffer));
  68. //
  69. // alloc/clean in buffer
  70. //
  71. pBuffer = new CHAR[(lbuffersize+1)];
  72. if(NULL == pBuffer){
  73. return E_OUTOFMEMORY;
  74. }
  75. memset(pBuffer,0,lbuffersize+1);
  76. //
  77. // read from device
  78. //
  79. if(!ReadFile(m_pScannerSettings->DeviceIOHandles[lPipeNum],pBuffer,lbuffersize,&dwBytesRead,NULL)) {
  80. return E_FAIL;
  81. }
  82. pBuffer[dwBytesRead] = '\0';
  83. //
  84. // set number of bytes read
  85. //
  86. *plbytesread = dwBytesRead;
  87. //
  88. // construct VARIANT properly, for out buffer
  89. //
  90. MultiByteToWideChar(CP_ACP,
  91. MB_PRECOMPOSED,
  92. pBuffer,
  93. lstrlenA(pBuffer)+1,
  94. wszBuffer,
  95. (sizeof(wszBuffer)/sizeof(WCHAR)));
  96. pvbuffer->vt = VT_BSTR;
  97. pvbuffer->bstrVal = SysAllocString(wszBuffer);
  98. delete pBuffer;
  99. return hr;
  100. }
  101. STDMETHODIMP CDeviceControl::ScanRead(LONG lPipeNum,LONG lBytesToRead, LONG *plBytesRead, LONG lTimeout)
  102. {
  103. if(!ReadFile(m_pScannerSettings->DeviceIOHandles[lPipeNum],
  104. m_pBuffer,
  105. m_lBufferSize,
  106. &m_dwBytesRead,NULL)) {
  107. //
  108. // SetLastErrorCode
  109. //
  110. }
  111. return S_OK;
  112. }
  113. STDMETHODIMP CDeviceControl::RegisterWrite(LONG lPipeNum,VARIANT *pvbuffer,LONG lTimeout)
  114. {
  115. HRESULT hr = S_OK;
  116. PBYTE pData = NULL;
  117. DWORD dwBytesWritten = 0;
  118. IO_BLOCK IoBlock;
  119. memset(&IoBlock,0,sizeof(IO_BLOCK));
  120. VARIANT *pVariant = NULL;
  121. VARIANTARG *pVariantArg = pvbuffer->pvarVal;
  122. LONG lUBound = 0;
  123. LONG lNumItems = 0;
  124. if(SafeArrayGetDim(pVariantArg->parray)!=1){
  125. return E_INVALIDARG;
  126. }
  127. //
  128. // get upper bounds
  129. //
  130. hr = SafeArrayGetUBound(pVariantArg->parray,1,(LONG*)&lUBound);
  131. if (SUCCEEDED(hr)) {
  132. hr = SafeArrayAccessData(pVariantArg->parray,(void**)&pVariant);
  133. if (SUCCEEDED(hr)) {
  134. lNumItems = (lUBound + 1);
  135. pData = (PBYTE)LocalAlloc(LPTR,sizeof(BYTE) * lNumItems);
  136. if(NULL != pData){
  137. //
  138. // copy contents of VARIANT into BYTE array, for writing to
  139. // the device.
  140. //
  141. for(INT index = 0;index <lUBound;index++){
  142. pData[index] = pVariant[index].bVal;
  143. }
  144. IoBlock.uOffset = (BYTE)IOCTL_EPP_WRITE;
  145. IoBlock.uLength = (BYTE)(sizeof(BYTE) * lNumItems);
  146. IoBlock.pbyData = pData;
  147. DeviceIoControl(m_pScannerSettings->DeviceIOHandles[lPipeNum],
  148. (DWORD) IOCTL_WRITE_REGISTERS,
  149. &IoBlock,
  150. sizeof(IO_BLOCK),
  151. NULL,
  152. 0,
  153. &dwBytesWritten,
  154. NULL);
  155. //
  156. // free array block, after operation is complete
  157. //
  158. LocalFree(pData);
  159. pData = NULL;
  160. }
  161. }
  162. }
  163. return hr;
  164. }
  165. STDMETHODIMP CDeviceControl::RegisterRead(LONG lPipeNum,LONG lRegNumber, VARIANT *pvbuffer,LONG lTimeout)
  166. {
  167. HRESULT hr = S_OK;
  168. DWORD dwBytesRead = 0;
  169. pvbuffer->vt = VT_UI1;
  170. //
  171. // read from device
  172. //
  173. IO_BLOCK IoBlock;
  174. IoBlock.uOffset = MAKEWORD(IOCTL_EPP_READ, (BYTE)lRegNumber);
  175. IoBlock.uLength = 1;
  176. IoBlock.pbyData = &pvbuffer->bVal;
  177. if (!DeviceIoControl(m_pScannerSettings->DeviceIOHandles[lPipeNum],
  178. (DWORD) IOCTL_READ_REGISTERS,
  179. (PVOID)&IoBlock,
  180. (DWORD)sizeof(IO_BLOCK),
  181. (PVOID)&pvbuffer->bVal,
  182. (DWORD)sizeof(BYTE),
  183. &dwBytesRead,
  184. NULL)){
  185. return E_FAIL;
  186. };
  187. return hr;
  188. }
  189. STDMETHODIMP CDeviceControl::SetBitsInByte(BYTE bMask, BYTE bValue, BYTE *pbyte)
  190. {
  191. LONG lBitIndex = 0;
  192. if(((BITS*)&bMask)->b0 == 1)
  193. lBitIndex = 0;
  194. else if(((BITS*)&bMask)->b1 == 1)
  195. lBitIndex = 1;
  196. else if(((BITS*)&bMask)->b2 == 1)
  197. lBitIndex = 2;
  198. else if(((BITS*)&bMask)->b3 == 1)
  199. lBitIndex = 3;
  200. else if(((BITS*)&bMask)->b4 == 1)
  201. lBitIndex = 4;
  202. else if(((BITS*)&bMask)->b5 == 1)
  203. lBitIndex = 5;
  204. else if(((BITS*)&bMask)->b6 == 1)
  205. lBitIndex = 6;
  206. else if(((BITS*)&bMask)->b7 == 1)
  207. lBitIndex = 7;
  208. *pbyte = (*pbyte & ~bMask) | ((bValue << lBitIndex) & bMask);
  209. return S_OK;
  210. }