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.

1153 lines
36 KiB

  1. #include "pch.h"
  2. CComModule _Module;
  3. #include <initguid.h>
  4. DEFINE_GUID(CLSID_VBScript, 0xb54f3741, 0x5b07, 0x11cf, 0xa4, 0xb0, 0x0, 0xaa, 0x0, 0x4a, 0x55, 0xe8);
  5. CIOBlock::CIOBlock()
  6. {
  7. memset(m_szFileName,0,sizeof(m_szFileName));
  8. }
  9. CIOBlock::~CIOBlock()
  10. {
  11. StopScriptEngine();
  12. }
  13. void CIOBlock::Initialize(PGSD_INFO pGSDInfo)
  14. {
  15. // SBB - RAID 370299 - orenr - 2001/04/18 - Security fix -
  16. // potential buffer overrun. Changed lstrcpy to use
  17. // _tcsncpy instead.
  18. //
  19. ZeroMemory(m_szFileName, sizeof(m_szFileName));
  20. _tcsncpy(m_szFileName,
  21. pGSDInfo->szProductFileName,
  22. (sizeof(m_szFileName) / sizeof(TCHAR)) - 1);
  23. }
  24. HRESULT CIOBlock::StartScriptEngine()
  25. {
  26. HRESULT hr = S_OK;
  27. //
  28. // load scriptlets
  29. //
  30. hr = LoadScript();
  31. if(FAILED(hr)){
  32. return hr;
  33. }
  34. m_pDeviceScriptSite = new CDeviceScriptSite;
  35. if(m_pDeviceScriptSite){
  36. m_pDeviceProperty = new CComObject<CDeviceProperty>;
  37. if(m_pDeviceProperty){
  38. m_pDeviceAction = new CComObject<CDeviceAction>;
  39. if(m_pDeviceAction){
  40. m_pDeviceControl = new CComObject<CDeviceControl>;
  41. if(m_pDeviceControl){
  42. m_pLastError = new CComObject<CLastError>;
  43. if(!m_pLastError){
  44. hr = E_OUTOFMEMORY;
  45. }
  46. } else {
  47. hr = E_OUTOFMEMORY;
  48. }
  49. } else {
  50. hr = E_OUTOFMEMORY;
  51. }
  52. } else {
  53. hr = E_OUTOFMEMORY;
  54. }
  55. } else {
  56. hr = E_OUTOFMEMORY;
  57. }
  58. if(FAILED(hr)){
  59. //
  60. // Delete site & objects
  61. //
  62. if (m_pDeviceScriptSite) {
  63. delete m_pDeviceScriptSite;
  64. m_pDeviceScriptSite = NULL;
  65. }
  66. if (m_pDeviceProperty) {
  67. delete m_pDeviceProperty;
  68. m_pDeviceProperty = NULL;
  69. }
  70. if (m_pDeviceAction) {
  71. delete m_pDeviceAction;
  72. m_pDeviceAction = NULL;
  73. }
  74. if (m_pLastError) {
  75. m_pLastError = NULL;
  76. }
  77. }
  78. //
  79. // Initialize objects
  80. //
  81. m_pDeviceProperty->m_pScannerSettings = &m_ScannerSettings;
  82. m_pDeviceControl->m_pScannerSettings = &m_ScannerSettings;
  83. m_pDeviceAction->m_pScannerSettings = &m_ScannerSettings;
  84. //
  85. // get type library information
  86. //
  87. ITypeLib *ptLib = 0;
  88. // hr = LoadTypeLib(L"wiafb.tlb", &ptLib); // type library as a separate file
  89. hr = LoadTypeLib(OLESTR("wiafbdrv.dll\\2"), &ptLib); // type library as a resource
  90. if (SUCCEEDED(hr)) {
  91. Trace(TEXT("Type library loaded"));
  92. } else {
  93. Trace(TEXT("Type library failed to load"));
  94. }
  95. ptLib->GetTypeInfoOfGuid(CLSID_DeviceProperty, &m_pDeviceScriptSite->m_pTypeInfo);
  96. ptLib->GetTypeInfoOfGuid(CLSID_DeviceAction, &m_pDeviceScriptSite->m_pTypeInfoDeviceAction);
  97. ptLib->GetTypeInfoOfGuid(CLSID_DeviceControl, &m_pDeviceScriptSite->m_pTypeInfoDeviceControl);
  98. ptLib->GetTypeInfoOfGuid(CLSID_LastError, &m_pDeviceScriptSite->m_pTypeInfoLastError);
  99. ptLib->Release();
  100. //
  101. // Intialize DeviceScriptSite with IUnknowns of scripting objects
  102. //
  103. hr = m_pDeviceProperty->QueryInterface(IID_IUnknown,(void **)&m_pDeviceScriptSite->m_pUnkScriptObject);
  104. if (SUCCEEDED(hr)) {
  105. Trace(TEXT("QI on DeviceProperty success"));
  106. } else {
  107. Trace(TEXT("QI on DeviceProperty FAILED"));
  108. }
  109. hr = m_pDeviceAction->QueryInterface(IID_IUnknown,(void **)&m_pDeviceScriptSite->m_pUnkScriptObjectDeviceAction);
  110. if (SUCCEEDED(hr)) {
  111. Trace(TEXT("QI on DeviceAction success"));
  112. } else {
  113. Trace(TEXT("QI on DeviceAction FAILED"));
  114. }
  115. hr = m_pDeviceControl->QueryInterface(IID_IUnknown,(void **)&m_pDeviceScriptSite->m_pUnkScriptObjectDeviceControl);
  116. if (SUCCEEDED(hr)) {
  117. Trace(TEXT("QI on DeviceControl success"));
  118. } else {
  119. Trace(TEXT("QI on DeviceControl FAILED"));
  120. }
  121. hr = m_pLastError->QueryInterface(IID_IUnknown,(void **)&m_pDeviceScriptSite->m_pUnkScriptObjectLastError);
  122. if (SUCCEEDED(hr)) {
  123. Trace(TEXT("QI on LastError success"));
  124. } else {
  125. Trace(TEXT("QI on LastError FAILED"));
  126. }
  127. //
  128. // Start inproc script engine, VBSCRIPT.DLL
  129. //
  130. hr = CoCreateInstance(CLSID_VBScript,
  131. NULL,
  132. CLSCTX_INPROC_SERVER,
  133. IID_IActiveScript, (void **)&m_pActiveScript);
  134. if (SUCCEEDED(hr)) {
  135. Trace(TEXT("CoCreateInstance, VBScript"));
  136. } else {
  137. Trace(TEXT("CoCreateInstance, VBScript FAILED"));
  138. Trace(TEXT("hr = %x"),hr);
  139. }
  140. //
  141. // Get engine's IActiveScriptParse interface.
  142. //
  143. hr = m_pActiveScript->QueryInterface(IID_IActiveScriptParse,
  144. (void **)&m_pActiveScriptParser);
  145. if (SUCCEEDED(hr)) {
  146. Trace(TEXT("QI pm ActiveParse success"));
  147. } else {
  148. Trace(TEXT("QI pm ActiveParse FAILED"));
  149. Trace(TEXT("hr = %x"),hr);
  150. }
  151. //
  152. // Give engine our DeviceScriptSite interface...
  153. //
  154. hr = m_pActiveScript->SetScriptSite((IActiveScriptSite *)m_pDeviceScriptSite);
  155. if (SUCCEEDED(hr)) {
  156. Trace(TEXT("SetScriptSite "));
  157. } else {
  158. Trace(TEXT("SetScriptSite FAILED"));
  159. Trace(TEXT("hr = %x"),hr);
  160. }
  161. //
  162. // Intialize Engine
  163. //
  164. hr = m_pActiveScriptParser->InitNew();
  165. if (SUCCEEDED(hr)) {
  166. Trace(TEXT("InitNew"));
  167. } else {
  168. Trace(TEXT("InitNew FAILED"));
  169. Trace(TEXT("hr = %x"),hr);
  170. }
  171. //
  172. // Add object names to ActiveScript's known named item list
  173. //
  174. hr = m_pActiveScript->AddNamedItem(L"DeviceProperty", SCRIPTITEM_ISVISIBLE);
  175. if (SUCCEEDED(hr)) {
  176. Trace(TEXT("added item"));
  177. } else {
  178. Trace(TEXT("added item FAILED"));
  179. Trace(TEXT("hr = %x"),hr);
  180. }
  181. hr = m_pActiveScript->AddNamedItem(L"DeviceAction", SCRIPTITEM_ISVISIBLE | SCRIPTITEM_ISSOURCE);
  182. if (SUCCEEDED(hr)) {
  183. Trace(TEXT("added item"));
  184. } else {
  185. Trace(TEXT("added item FAILED"));
  186. Trace(TEXT("hr = %x"),hr);
  187. }
  188. hr = m_pActiveScript->AddNamedItem(L"DeviceControl", SCRIPTITEM_ISVISIBLE);
  189. if (SUCCEEDED(hr)) {
  190. Trace(TEXT("added item"));
  191. } else {
  192. Trace(TEXT("added item FAILED"));
  193. Trace(TEXT("hr = %x"),hr);
  194. }
  195. hr = m_pActiveScript->AddNamedItem(L"LastError", SCRIPTITEM_ISVISIBLE);
  196. if (SUCCEEDED(hr)) {
  197. Trace(TEXT("added item"));
  198. } else {
  199. Trace(TEXT("added item FAILED"));
  200. Trace(TEXT("hr = %x"),hr);
  201. }
  202. return ProcessScript();
  203. }
  204. HRESULT CIOBlock::StopScriptEngine()
  205. {
  206. HRESULT hr = S_OK;
  207. //
  208. // Release interfaces
  209. //
  210. if(m_pActiveScriptParser){
  211. m_pActiveScriptParser->Release();
  212. m_pActiveScriptParser = NULL;
  213. }
  214. if (m_pActiveScript) {
  215. m_pActiveScript->Release();
  216. m_pActiveScript = NULL;
  217. }
  218. //
  219. // Delete site & objects
  220. //
  221. if (m_pDeviceScriptSite) {
  222. delete m_pDeviceScriptSite;
  223. m_pDeviceScriptSite = NULL;
  224. }
  225. if (m_pDeviceProperty) {
  226. delete m_pDeviceProperty;
  227. m_pDeviceProperty = NULL;
  228. }
  229. if (m_pDeviceAction) {
  230. delete m_pDeviceAction;
  231. m_pDeviceAction = NULL;
  232. }
  233. if (m_pLastError) {
  234. m_pLastError = NULL;
  235. }
  236. //
  237. // free scriptlets
  238. //
  239. if(m_wszScriptText){
  240. LocalFree(m_wszScriptText);
  241. }
  242. return hr;
  243. }
  244. HRESULT CIOBlock::LoadScript()
  245. {
  246. HRESULT hr = S_OK;
  247. DWORD dwFileSize = 0;
  248. DWORD dwBytesRead = 0;
  249. CHAR *pBytes = NULL;
  250. HANDLE hGSDProductLineFile = NULL;
  251. BY_HANDLE_FILE_INFORMATION FileInfo;
  252. //
  253. // construct Window's system path
  254. //
  255. TCHAR szFullFilePath[255];
  256. GetSystemDirectory(szFullFilePath,(sizeof(szFullFilePath)/sizeof(TCHAR)));
  257. lstrcat(szFullFilePath,TEXT("\\"));
  258. lstrcat(szFullFilePath,m_szFileName);
  259. //
  260. // open script file
  261. //
  262. Trace(TEXT("Opening this script file: %ws"),szFullFilePath);
  263. hGSDProductLineFile = CreateFile(szFullFilePath,
  264. GENERIC_READ,
  265. FILE_SHARE_READ,
  266. NULL,
  267. OPEN_EXISTING,
  268. FILE_ATTRIBUTE_NORMAL,
  269. NULL);
  270. if (NULL == hGSDProductLineFile || INVALID_HANDLE_VALUE == hGSDProductLineFile) {
  271. DWORD dwError = 0;
  272. dwError = GetLastError();
  273. Trace(TEXT("CreateFile() LastError reports %d "),dwError);
  274. return E_FAIL;
  275. }
  276. //
  277. // calculate needed space, for script text buffer
  278. //
  279. GetFileInformationByHandle(hGSDProductLineFile,&FileInfo);
  280. dwFileSize = FileInfo.nFileSizeLow;
  281. //
  282. // alloc temp memory, for reading script file, read file, and close file
  283. //
  284. pBytes = (CHAR*)LocalAlloc(LPTR,(dwFileSize + 1024));
  285. if (NULL != pBytes) {
  286. ReadFile(hGSDProductLineFile,pBytes,dwFileSize,&dwBytesRead,NULL);
  287. CloseHandle(hGSDProductLineFile);
  288. if ((dwBytesRead == dwFileSize)) {
  289. //
  290. // allocate text buffer
  291. //
  292. m_wszScriptText = (WCHAR*)LocalAlloc(LPTR,(dwFileSize * 2) + 1024);
  293. if(NULL != m_wszScriptText){
  294. memset(m_wszScriptText,0,(dwFileSize * 2) + 1024);
  295. MultiByteToWideChar(CP_ACP, 0, pBytes, dwBytesRead, m_wszScriptText, (dwFileSize*2) + 1024);
  296. }
  297. } else {
  298. hr = E_FAIL;
  299. }
  300. //
  301. // free temp buffer
  302. //
  303. LocalFree(pBytes);
  304. }
  305. return hr;
  306. }
  307. HRESULT CIOBlock::ProcessScript()
  308. {
  309. HRESULT hr = S_OK;
  310. EXCEPINFO ei;
  311. //
  312. // parse scriptlet
  313. // note: we are alloc a copy here... should we keep the original
  314. // around for extra processing...manually??
  315. //
  316. BSTR pParseText = ::SysAllocString(m_wszScriptText);
  317. hr = m_pActiveScriptParser->ParseScriptText(pParseText, NULL,
  318. NULL, NULL, 0, 0, 0L, NULL, &ei);
  319. if (SUCCEEDED(hr)) {
  320. Trace(TEXT("script parsed"));
  321. } else {
  322. Trace(TEXT("script parse FAILED"));
  323. Trace(TEXT("hr = %x"),hr);
  324. }
  325. ::SysFreeString(pParseText);
  326. //
  327. // Execute the scriptlet
  328. //
  329. hr = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  330. if (SUCCEEDED(hr)) {
  331. Trace(TEXT("Scripted connected"));
  332. } else {
  333. Trace(TEXT("Scripted connection FAILED"));
  334. Trace(TEXT("hr = %x"),hr);
  335. }
  336. return hr;
  337. }
  338. HRESULT CIOBlock::DebugDumpScannerSettings()
  339. {
  340. HRESULT hr = S_OK;
  341. // #define _USE_DUMMY_VALUES
  342. #ifdef _USE_DUMMY_VALUES
  343. lstrcpy(m_ScannerSettings.Version,TEXT("1.0"));
  344. lstrcpy(m_ScannerSettings.DeviceName,TEXT("HP 5P Driver"));
  345. lstrcpy(m_ScannerSettings.FirmwareVersion,TEXT("1.01"));
  346. m_ScannerSettings.BUSType = BUS_TYPE_SCSI;
  347. m_ScannerSettings.bNegative = TRUE;
  348. m_ScannerSettings.CurrentXResolution = 300;
  349. m_ScannerSettings.CurrentYResolution = 300;
  350. m_ScannerSettings.BedWidth = 8500;
  351. m_ScannerSettings.BedHeight = 11693;
  352. m_ScannerSettings.XOpticalResolution = 300;
  353. m_ScannerSettings.YOpticalResolution = 300;
  354. m_ScannerSettings.CurrentBrightness = 32;
  355. m_ScannerSettings.CurrentContrast = 12;
  356. m_ScannerSettings.ADFSupport = FALSE;
  357. m_ScannerSettings.TPASupport = FALSE;
  358. m_ScannerSettings.RawPixelPackingOrder = WIA_PACKED_PIXEL;
  359. m_ScannerSettings.RawPixelFormat = WIA_ORDER_BGR;
  360. m_ScannerSettings.RawDataAlignment = DWORD_ALIGN;
  361. m_ScannerSettings.FeederWidth = m_ScannerSettings.BedWidth;
  362. m_ScannerSettings.FeederHeight = m_ScannerSettings.BedHeight;
  363. m_ScannerSettings.VFeederJustification = LEFT_JUSTIFIED;
  364. m_ScannerSettings.HFeederJustification = TOP_JUSTIFIED;
  365. m_ScannerSettings.MaxADFPageCapacity = 30;
  366. m_ScannerSettings.CurrentDataType = WIA_DATA_GRAYSCALE;
  367. m_ScannerSettings.CurrentBitDepth = 8;
  368. m_ScannerSettings.XSupportedResolutionsRange.lMax = 1200;
  369. m_ScannerSettings.XSupportedResolutionsRange.lMin = 12;
  370. m_ScannerSettings.XSupportedResolutionsRange.lStep = 1;
  371. m_ScannerSettings.YSupportedResolutionsRange.lMax = 1200;
  372. m_ScannerSettings.YSupportedResolutionsRange.lMin = 12;
  373. m_ScannerSettings.YSupportedResolutionsRange.lStep = 1;
  374. m_ScannerSettings.XExtentsRange.lMax = 2550;
  375. m_ScannerSettings.XExtentsRange.lMin = 1;
  376. m_ScannerSettings.XExtentsRange.lStep = 1;
  377. m_ScannerSettings.YExtentsRange.lMax = 3507;
  378. m_ScannerSettings.YExtentsRange.lMin = 1;
  379. m_ScannerSettings.YExtentsRange.lStep = 1;
  380. m_ScannerSettings.XPosRange.lMax = 2549;
  381. m_ScannerSettings.XPosRange.lMin = 0;
  382. m_ScannerSettings.XPosRange.lStep = 1;
  383. m_ScannerSettings.YPosRange.lMax = 3506;
  384. m_ScannerSettings.YPosRange.lMin = 0;
  385. m_ScannerSettings.YPosRange.lStep = 1;
  386. m_ScannerSettings.CurrentXPos = 0;
  387. m_ScannerSettings.CurrentYPos = 0;
  388. m_ScannerSettings.CurrentXExtent = m_ScannerSettings.XExtentsRange.lMax;
  389. m_ScannerSettings.CurrentYExtent = m_ScannerSettings.YExtentsRange.lMax;
  390. m_ScannerSettings.BrightnessRange.lMax = 127;
  391. m_ScannerSettings.BrightnessRange.lMin = -127;
  392. m_ScannerSettings.BrightnessRange.lStep = 1;
  393. m_ScannerSettings.ContrastRange.lMax = 127;
  394. m_ScannerSettings.ContrastRange.lMin = -127;
  395. m_ScannerSettings.ContrastRange.lStep = 1;
  396. m_ScannerSettings.XSupportedResolutionsList = NULL;
  397. m_ScannerSettings.YSupportedResolutionsList = NULL;
  398. INT iNumValues = 4; // add 1 extra for header node
  399. m_ScannerSettings.SupportedDataTypesList = (PLONG)LocalAlloc(LPTR,(sizeof(LONG) * iNumValues));
  400. if(m_ScannerSettings.SupportedDataTypesList){
  401. m_ScannerSettings.SupportedDataTypesList[0] = (iNumValues - 1);
  402. m_ScannerSettings.SupportedDataTypesList[1] = WIA_DATA_THRESHOLD;
  403. m_ScannerSettings.SupportedDataTypesList[2] = WIA_DATA_GRAYSCALE;
  404. m_ScannerSettings.SupportedDataTypesList[3] = WIA_DATA_COLOR;
  405. } else {
  406. m_ScannerSettings.SupportedDataTypesList = NULL;
  407. }
  408. #endif
  409. Trace(TEXT(" -- m_ScannerSettings structure dump --"));
  410. Trace(TEXT("BUSType = %d"),m_ScannerSettings.BUSType);
  411. Trace(TEXT("bNegative = %d"),m_ScannerSettings.bNegative);
  412. Trace(TEXT("CurrentXResolution = %d"),m_ScannerSettings.CurrentXResolution);
  413. Trace(TEXT("CurrentYResolution = %d"),m_ScannerSettings.CurrentYResolution);
  414. Trace(TEXT("BedWidth = %d"),m_ScannerSettings.BedWidth);
  415. Trace(TEXT("BedHeight = %d"),m_ScannerSettings.BedHeight);
  416. Trace(TEXT("XOpticalResolution = %d"),m_ScannerSettings.XOpticalResolution);
  417. Trace(TEXT("YOpticalResolution = %d"),m_ScannerSettings.YOpticalResolution);
  418. Trace(TEXT("CurrentBrightness = %d"),m_ScannerSettings.CurrentBrightness);
  419. Trace(TEXT("CurrentContrast = %d"),m_ScannerSettings.CurrentContrast);
  420. Trace(TEXT("ADFSupport = %d"),m_ScannerSettings.ADFSupport);
  421. Trace(TEXT("TPASupport = %d"),m_ScannerSettings.TPASupport);
  422. Trace(TEXT("RawPixelPackingOrder = %d"),m_ScannerSettings.RawPixelPackingOrder);
  423. Trace(TEXT("RawPixelFormat = %d"),m_ScannerSettings.RawPixelFormat);
  424. Trace(TEXT("RawDataAlignment = %d"),m_ScannerSettings.RawDataAlignment);
  425. Trace(TEXT("FeederWidth = %d"),m_ScannerSettings.FeederWidth);
  426. Trace(TEXT("FeederHeight = %d"),m_ScannerSettings.FeederHeight);
  427. Trace(TEXT("VFeederJustification = %d"),m_ScannerSettings.VFeederJustification);
  428. Trace(TEXT("HFeederJustification = %d"),m_ScannerSettings.HFeederJustification);
  429. Trace(TEXT("MaxADFPageCapacity = %d"),m_ScannerSettings.MaxADFPageCapacity);
  430. Trace(TEXT("CurrentDataType = %d"),m_ScannerSettings.CurrentDataType );
  431. Trace(TEXT("CurrentBitDepth = %d"),m_ScannerSettings.CurrentBitDepth);
  432. Trace(TEXT("XSupportedResolutionsRange.lMax = %d"),m_ScannerSettings.XSupportedResolutionsRange.lMax);
  433. Trace(TEXT("XSupportedResolutionsRange.lMin = %d"),m_ScannerSettings.XSupportedResolutionsRange.lMin);
  434. Trace(TEXT("XSupportedResolutionsRange.lNom = %d"),m_ScannerSettings.XSupportedResolutionsRange.lNom);
  435. Trace(TEXT("XSupportedResolutionsRange.lStep = %d"),m_ScannerSettings.XSupportedResolutionsRange.lStep);
  436. Trace(TEXT("YSupportedResolutionsRange.lMax = %d"),m_ScannerSettings.YSupportedResolutionsRange.lMax);
  437. Trace(TEXT("YSupportedResolutionsRange.lMin = %d"),m_ScannerSettings.YSupportedResolutionsRange.lMin);
  438. Trace(TEXT("YSupportedResolutionsRange.lNom = %d"),m_ScannerSettings.YSupportedResolutionsRange.lNom);
  439. Trace(TEXT("YSupportedResolutionsRange.lStep = %d"),m_ScannerSettings.YSupportedResolutionsRange.lStep);
  440. Trace(TEXT("XExtentsRange.lMax = %d"),m_ScannerSettings.XExtentsRange.lMax);
  441. Trace(TEXT("XExtentsRange.lMin = %d"),m_ScannerSettings.XExtentsRange.lMin);
  442. Trace(TEXT("XExtentsRange.lNom = %d"),m_ScannerSettings.XExtentsRange.lNom);
  443. Trace(TEXT("XExtentsRange.lStep = %d"),m_ScannerSettings.XExtentsRange.lStep);
  444. Trace(TEXT("YExtentsRange.lMax = %d"),m_ScannerSettings.YExtentsRange.lMax);
  445. Trace(TEXT("YExtentsRange.lMin = %d"),m_ScannerSettings.YExtentsRange.lMin);
  446. Trace(TEXT("YExtentsRange.lNom = %d"),m_ScannerSettings.YExtentsRange.lNom);
  447. Trace(TEXT("YExtentsRange.lStep = %d"),m_ScannerSettings.YExtentsRange.lStep);
  448. Trace(TEXT("XPosRange.lMax = %d"),m_ScannerSettings.XPosRange.lMax);
  449. Trace(TEXT("XPosRange.lMin = %d"),m_ScannerSettings.XPosRange.lMin);
  450. Trace(TEXT("XPosRange.lNom = %d"),m_ScannerSettings.XPosRange.lNom);
  451. Trace(TEXT("XPosRange.lStep = %d"),m_ScannerSettings.XPosRange.lStep);
  452. Trace(TEXT("YPosRange.lMax = %d"),m_ScannerSettings.YPosRange.lMax);
  453. Trace(TEXT("YPosRange.lMin = %d"),m_ScannerSettings.YPosRange.lMin);
  454. Trace(TEXT("YPosRange.lNom = %d"),m_ScannerSettings.YPosRange.lNom);
  455. Trace(TEXT("YPosRange.lStep = %d"),m_ScannerSettings.YPosRange.lStep);
  456. Trace(TEXT("CurrentXPos = %d"),m_ScannerSettings.CurrentXPos);
  457. Trace(TEXT("CurrentYPos = %d"),m_ScannerSettings.CurrentYPos);
  458. Trace(TEXT("CurrentXExtent = %d"),m_ScannerSettings.CurrentXExtent);
  459. Trace(TEXT("CurrentYExtent = %d"),m_ScannerSettings.CurrentYExtent);
  460. Trace(TEXT("BrightnessRange.lMax = %d"),m_ScannerSettings.BrightnessRange.lMax);
  461. Trace(TEXT("BrightnessRange.lMin = %d"),m_ScannerSettings.BrightnessRange.lMin);
  462. Trace(TEXT("BrightnessRange.lNom = %d"),m_ScannerSettings.BrightnessRange.lNom);
  463. Trace(TEXT("BrightnessRange.lStep = %d"),m_ScannerSettings.BrightnessRange.lStep);
  464. Trace(TEXT("ContrastRange.lMax = %d"),m_ScannerSettings.ContrastRange.lMax);
  465. Trace(TEXT("ContrastRange.lMin = %d"),m_ScannerSettings.ContrastRange.lMin);
  466. Trace(TEXT("ContrastRange.lNom = %d"),m_ScannerSettings.ContrastRange.lNom);
  467. Trace(TEXT("ContrastRange.lStep = %d"),m_ScannerSettings.ContrastRange.lStep);
  468. Trace(TEXT("XSupportedResolutionsList = %x"),m_ScannerSettings.XSupportedResolutionsList);
  469. Trace(TEXT("YSupportedResolutionsList = %x"),m_ScannerSettings.YSupportedResolutionsList);
  470. if(m_ScannerSettings.XSupportedResolutionsList) {
  471. LONG lNumResolutions = m_ScannerSettings.XSupportedResolutionsList[0];
  472. Trace(TEXT("Number of Supported X Resolutions = %d"),lNumResolutions);
  473. for(LONG i = 1;i<=lNumResolutions;i++){
  474. Trace(TEXT("Supported Resolution #%d = %d"),i,m_ScannerSettings.XSupportedResolutionsList[i]);
  475. }
  476. }
  477. if(m_ScannerSettings.YSupportedResolutionsList) {
  478. LONG lNumResolutions = m_ScannerSettings.YSupportedResolutionsList[0];
  479. Trace(TEXT("Number of Supported Y Resolutions = %d"),lNumResolutions);
  480. for(LONG i = 1;i<=lNumResolutions;i++){
  481. Trace(TEXT("Supported Resolution #%d = %d"),i,m_ScannerSettings.YSupportedResolutionsList[i]);
  482. }
  483. }
  484. return hr;
  485. }
  486. HRESULT CIOBlock::ReadValue(LONG ValueID, PLONG plValue)
  487. {
  488. HRESULT hr = S_OK;
  489. if(NULL == plValue){
  490. return E_INVALIDARG;
  491. }
  492. //
  493. // set returned long value to 0
  494. //
  495. *plValue = 0;
  496. //
  497. // initialize LastError Object to SUCCESS
  498. //
  499. m_pLastError->m_hr = S_OK;
  500. //
  501. // set action ID
  502. //
  503. m_pDeviceAction->m_DeviceActionID = 102; // make #define
  504. //
  505. // set value ID
  506. //
  507. m_pDeviceAction->m_DeviceValueID = ValueID;
  508. // //
  509. // ****************************************** //
  510. // //
  511. //
  512. // Give engine our DeviceScriptSite interface...
  513. //
  514. IActiveScript *pActiveScript = NULL;
  515. hr = m_pActiveScript->Clone(&pActiveScript);
  516. if (SUCCEEDED(hr)) {
  517. Trace(TEXT("cloning script success"));
  518. } else {
  519. Trace(TEXT("cloning script FAILED"));
  520. Trace(TEXT("hr = %x"),hr);
  521. }
  522. hr = pActiveScript->SetScriptSite((IActiveScriptSite *)m_pDeviceScriptSite);
  523. if (SUCCEEDED(hr)) {
  524. Trace(TEXT("SetScriptSite on cloned script"));
  525. } else {
  526. Trace(TEXT("SetScriptSite on cloned script FAILED"));
  527. Trace(TEXT("hr = %x"),hr);
  528. }
  529. //
  530. // Execute the scriptlet
  531. //
  532. hr = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  533. if (SUCCEEDED(hr)) {
  534. Trace(TEXT("Scripted connected"));
  535. } else {
  536. Trace(TEXT("Scripted connection FAILED"));
  537. Trace(TEXT("hr = %x"),hr);
  538. }
  539. // //
  540. // ****************************************** //
  541. // //
  542. //
  543. // signal script event (DeviceActionEvent)
  544. //
  545. hr = m_pDeviceAction->Fire_DeviceActionEvent();
  546. if(SUCCEEDED(hr)){
  547. //
  548. // check for any script-returned errors
  549. //
  550. hr = m_pLastError->m_hr;
  551. if(SUCCEEDED(hr)){
  552. *plValue = m_pDeviceAction->m_lValue;
  553. }
  554. }
  555. return hr;
  556. }
  557. HRESULT CIOBlock::WriteValue(LONG ValueID, LONG lValue)
  558. {
  559. HRESULT hr = S_OK;
  560. //
  561. // initialize LastError Object to SUCCESS
  562. //
  563. m_pLastError->m_hr = S_OK;
  564. //
  565. // set action ID
  566. //
  567. m_pDeviceAction->m_DeviceActionID = 101; // make #define
  568. //
  569. // set value ID
  570. //
  571. m_pDeviceAction->m_DeviceValueID = ValueID;
  572. //
  573. // set value
  574. //
  575. m_pDeviceAction->m_lValue = lValue;
  576. // //
  577. // ****************************************** //
  578. // //
  579. //
  580. // Give engine our DeviceScriptSite interface...
  581. //
  582. IActiveScript *pActiveScript = NULL;
  583. hr = m_pActiveScript->Clone(&pActiveScript);
  584. if (SUCCEEDED(hr)) {
  585. Trace(TEXT("cloning script success"));
  586. } else {
  587. Trace(TEXT("cloning script FAILED"));
  588. Trace(TEXT("hr = %x"),hr);
  589. }
  590. hr = pActiveScript->SetScriptSite((IActiveScriptSite *)m_pDeviceScriptSite);
  591. if (SUCCEEDED(hr)) {
  592. Trace(TEXT("SetScriptSite on cloned script"));
  593. } else {
  594. Trace(TEXT("SetScriptSite on cloned script FAILED"));
  595. Trace(TEXT("hr = %x"),hr);
  596. }
  597. //
  598. // Execute the scriptlet
  599. //
  600. hr = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  601. if (SUCCEEDED(hr)) {
  602. Trace(TEXT("Scripted connected"));
  603. } else {
  604. Trace(TEXT("Scripted connection FAILED"));
  605. Trace(TEXT("hr = %x"),hr);
  606. }
  607. // //
  608. // ****************************************** //
  609. // //
  610. //
  611. // signal script event (DeviceActionEvent)
  612. //
  613. hr = m_pDeviceAction->Fire_DeviceActionEvent();
  614. if(SUCCEEDED(hr)){
  615. //
  616. // check for any script-returned errors
  617. //
  618. hr = m_pLastError->m_hr;
  619. }
  620. pActiveScript->Release();
  621. return hr;
  622. }
  623. HRESULT CIOBlock::InitializeProperties()
  624. {
  625. HRESULT hr = S_OK;
  626. //
  627. // initialize LastError Object to SUCCESS
  628. //
  629. m_pLastError->m_hr = S_OK;
  630. //
  631. // set action ID
  632. //
  633. m_pDeviceAction->m_DeviceActionID = 100; // make #define
  634. // //
  635. // ****************************************** //
  636. // //
  637. //
  638. // Give engine our DeviceScriptSite interface...
  639. //
  640. IActiveScript *pActiveScript = NULL;
  641. hr = m_pActiveScript->Clone(&pActiveScript);
  642. if (SUCCEEDED(hr)) {
  643. Trace(TEXT("cloning script success"));
  644. } else {
  645. Trace(TEXT("cloning script FAILED"));
  646. Trace(TEXT("hr = %x"),hr);
  647. }
  648. hr = pActiveScript->SetScriptSite((IActiveScriptSite *)m_pDeviceScriptSite);
  649. if (SUCCEEDED(hr)) {
  650. Trace(TEXT("SetScriptSite on cloned script"));
  651. } else {
  652. Trace(TEXT("SetScriptSite on cloned script FAILED"));
  653. Trace(TEXT("hr = %x"),hr);
  654. }
  655. //
  656. // Execute the scriptlet
  657. //
  658. hr = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  659. if (SUCCEEDED(hr)) {
  660. Trace(TEXT("Scripted connected"));
  661. } else {
  662. Trace(TEXT("Scripted connection FAILED"));
  663. Trace(TEXT("hr = %x"),hr);
  664. }
  665. // //
  666. // ****************************************** //
  667. // //
  668. //
  669. // signal script event (DeviceActionEvent)
  670. //
  671. hr = m_pDeviceAction->Fire_DeviceActionEvent();
  672. if(SUCCEEDED(hr)){
  673. //
  674. // check for any script-returned errors
  675. //
  676. hr = m_pLastError->m_hr;
  677. }
  678. pActiveScript->Release();
  679. return hr;
  680. }
  681. HRESULT CIOBlock::Scan(LONG lPhase, PBYTE pBuffer, LONG lLength, LONG *plReceived)
  682. {
  683. m_pDeviceControl->m_pBuffer = pBuffer;
  684. m_pDeviceControl->m_lBufferSize = lLength;
  685. m_pDeviceControl->m_dwBytesRead = 0;
  686. HRESULT hr = S_OK;
  687. //
  688. // initialize LastError Object to SUCCESS
  689. //
  690. m_pLastError->m_hr = S_OK;
  691. m_pDeviceAction->m_lValue = lLength; // set data amount requested
  692. //
  693. // set action ID
  694. //
  695. switch(lPhase){
  696. case SCAN_FIRST:
  697. m_pDeviceAction->m_DeviceActionID = 104; // make #define
  698. break;
  699. case SCAN_NEXT:
  700. m_pDeviceAction->m_DeviceActionID = 105; // make #define
  701. break;
  702. case SCAN_FINISHED:
  703. m_pDeviceAction->m_DeviceActionID = 106; // make #define
  704. break;
  705. default:
  706. break;
  707. }
  708. // //
  709. // ****************************************** //
  710. // //
  711. //
  712. // Give engine our DeviceScriptSite interface...
  713. //
  714. IActiveScript *pActiveScript = NULL;
  715. hr = m_pActiveScript->Clone(&pActiveScript);
  716. if (SUCCEEDED(hr)) {
  717. Trace(TEXT("cloning script success"));
  718. } else {
  719. Trace(TEXT("cloning script FAILED"));
  720. Trace(TEXT("hr = %x"),hr);
  721. }
  722. hr = pActiveScript->SetScriptSite((IActiveScriptSite *)m_pDeviceScriptSite);
  723. if (SUCCEEDED(hr)) {
  724. Trace(TEXT("SetScriptSite on cloned script"));
  725. } else {
  726. Trace(TEXT("SetScriptSite on cloned script FAILED"));
  727. Trace(TEXT("hr = %x"),hr);
  728. }
  729. //
  730. // Execute the scriptlet
  731. //
  732. hr = m_pActiveScript->SetScriptState(SCRIPTSTATE_CONNECTED);
  733. if (SUCCEEDED(hr)) {
  734. Trace(TEXT("Scripted connected"));
  735. } else {
  736. Trace(TEXT("Scripted connection FAILED"));
  737. Trace(TEXT("hr = %x"),hr);
  738. }
  739. // //
  740. // ****************************************** //
  741. // //
  742. //
  743. // signal script event (DeviceActionEvent)
  744. //
  745. hr = m_pDeviceAction->Fire_DeviceActionEvent();
  746. if(SUCCEEDED(hr)){
  747. //
  748. // check for any script-returned errors
  749. //
  750. hr = m_pLastError->m_hr;
  751. }
  752. pActiveScript->Release();
  753. if(NULL != plReceived){
  754. *plReceived = m_pDeviceControl->m_dwBytesRead;
  755. }
  756. return hr;
  757. }
  758. BOOL CIOBlock::GetEventStatus(PGSD_EVENT_INFO pGSDEventInfo)
  759. {
  760. //
  761. // ask script about device reporting an event...
  762. // if there is an event, fill out pGSDEventInfo structure
  763. // and return TRUE, letting WIAFBDRV know that an event has
  764. // occured....or return FALSE, that nothing has happened.
  765. //
  766. // Dispatch a GETEVENT_STATUS event action to script here.
  767. //
  768. // check returned status flag... if no event happened, return FALSE;
  769. // else..somthing did happen..so check the returned mapping key.
  770. //
  771. //
  772. // script will return a mapping key that corresponds to
  773. // the device event.
  774. //
  775. //
  776. // use key to look up correct GUID from the driver's reported supported
  777. // event list, set GUID, and continue to return TRUE
  778. //
  779. return FALSE;
  780. }
  781. BOOL CIOBlock::DeviceOnLine()
  782. {
  783. //
  784. // ask script to check that the device is ON-LINE, and
  785. // funtional. Return TRUE, if it is, and FALSE if it is not.
  786. //
  787. // Dispatch a DEVICE_ONLINE event action to script here.
  788. return TRUE;
  789. }
  790. HRESULT CIOBlock::ResetDevice()
  791. {
  792. HRESULT hr = S_OK;
  793. //
  794. // ask script to reset the device to a power-on state.
  795. // Return TRUE, if it succeeded, and FALSE if it did not.
  796. //
  797. // Dispatch a DEVICE_RESET event action to script here.
  798. return hr;
  799. }
  800. HRESULT CIOBlock::EventInterrupt(PGSD_EVENT_INFO pGSDEventInfo)
  801. {
  802. BYTE InterruptData = 0;
  803. DWORD dwIndex = 0;
  804. DWORD dwError = 0;
  805. BOOL fLooping = TRUE;
  806. BOOL bRet = TRUE;
  807. DWORD dwBytesRet = 0;
  808. OVERLAPPED Overlapped;
  809. memset(&Overlapped,0,sizeof(OVERLAPPED));
  810. //
  811. // create an event to wait on
  812. //
  813. Overlapped.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  814. HANDLE hEventArray[2] = {pGSDEventInfo->hShutDownEvent, Overlapped.hEvent};
  815. HANDLE InterruptHandle = m_ScannerSettings.DeviceIOHandles[0]; // <- SET INTERRUPT PIPE INDEX
  816. // WITH REAL INDEX VALUE!!
  817. while (fLooping) {
  818. bRet = DeviceIoControl( InterruptHandle,
  819. IOCTL_WAIT_ON_DEVICE_EVENT,
  820. NULL,
  821. 0,
  822. &InterruptData,
  823. sizeof(InterruptData),
  824. &dwError,
  825. &Overlapped );
  826. if ( bRet || ( !bRet && ( ::GetLastError() == ERROR_IO_PENDING ))) {
  827. dwIndex = WaitForMultipleObjects( 2,
  828. hEventArray,
  829. FALSE,
  830. INFINITE );
  831. switch ( dwIndex ) {
  832. case WAIT_OBJECT_0+1:
  833. bRet = GetOverlappedResult( InterruptHandle, &Overlapped, &dwBytesRet, FALSE );
  834. if (dwBytesRet) {
  835. // Change detected - signal
  836. if (*pGSDEventInfo->phSignalEvent != INVALID_HANDLE_VALUE) {
  837. //
  838. // InterruptData contains result from device
  839. // *pGSDEventInfo->pEventGUID needs to be set to
  840. // the correct EVENT. (map event to result here??)
  841. //
  842. //
  843. // ask script to report a mapping key that corresponds to
  844. // the InterruptData returned information from device event.
  845. //
  846. // Dispatch a MAP_EVENT_RESULT_TO_KEY event action to script here.
  847. //
  848. //
  849. // use key to look up correct GUID from the driver's reported supported
  850. // event list, set GUID, and continue to set
  851. // "SignalEvent" for service notification.
  852. //
  853. //
  854. // signal service about the event
  855. //
  856. SetEvent(*pGSDEventInfo->phSignalEvent);
  857. }
  858. break;
  859. }
  860. //
  861. // reset the overlapped event
  862. //
  863. ResetEvent( Overlapped.hEvent );
  864. break;
  865. case WAIT_OBJECT_0:
  866. default:
  867. fLooping = FALSE;
  868. }
  869. }
  870. else {
  871. dwError = ::GetLastError();
  872. break;
  873. }
  874. }
  875. return S_OK;
  876. }
  877. ////////////////////////////////////////////////////////////
  878. // helpers called internally, or wrapped by a script call //
  879. ////////////////////////////////////////////////////////////
  880. LONG CIOBlock::InsertINTIntoByteBuffer(PBYTE szDest, PBYTE szSrc, BYTE cPlaceHolder, INT iValueToInsert)
  881. {
  882. LONG lFinalStringSize = 0;
  883. INT iSrcIndex = 0;
  884. INT iValueIndex = 0;
  885. CHAR szValue[10];
  886. // clean value string, and convert INT to characters
  887. memset(szValue,0,sizeof(szValue));
  888. _itoa(iValueToInsert,szValue,10);
  889. while(szSrc[iSrcIndex] != '\0'){
  890. // check for place holder
  891. if (szSrc[iSrcIndex] != cPlaceHolder) {
  892. szDest[lFinalStringSize] = szSrc[iSrcIndex];
  893. iSrcIndex++;
  894. lFinalStringSize++; // increment size of buffer
  895. } else {
  896. // replace placeholder with integer value (in string format)
  897. iValueIndex = 0;
  898. while (szValue[iValueIndex] != '\0') {
  899. szDest[lFinalStringSize] = szValue[iValueIndex];
  900. iValueIndex++;
  901. lFinalStringSize++; // increment size of command buffer
  902. }
  903. iSrcIndex++;
  904. }
  905. }
  906. // terminate buffer with NULL character
  907. szDest[lFinalStringSize] = '\0';
  908. lFinalStringSize++;
  909. return lFinalStringSize;
  910. }
  911. LONG CIOBlock::ExtractINTFromByteBuffer(PINT iDest, PBYTE szSrc, BYTE cTerminatorByte, INT iOffset)
  912. {
  913. *iDest = 0;
  914. BYTE szTempBuffer[25];
  915. INT iValueIndex = 0;
  916. memset(szTempBuffer,0,sizeof(szTempBuffer));
  917. while (szSrc[iOffset] != cTerminatorByte) {
  918. szTempBuffer[iValueIndex] = szSrc[iOffset];
  919. iValueIndex++;
  920. iOffset++;
  921. }
  922. iValueIndex++;
  923. szTempBuffer[iValueIndex] = '\0';
  924. *iDest = atoi((char*)szTempBuffer);
  925. return (LONG)*iDest;
  926. }
  927. VOID Trace(LPCTSTR format,...)
  928. {
  929. #ifdef DEBUG
  930. TCHAR Buffer[1024];
  931. va_list arglist;
  932. va_start(arglist, format);
  933. wvsprintf(Buffer, format, arglist);
  934. va_end(arglist);
  935. OutputDebugString(Buffer);
  936. OutputDebugString(TEXT("\n"));
  937. #endif
  938. }