Source code of Windows XP (NT5)
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.

662 lines
16 KiB

  1. // WIATest.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "WIATest.h"
  5. #include "MainFrm.h"
  6. #include "WIATestDoc.h"
  7. #include "WIATestView.h"
  8. //
  9. // enable/disable MessageBox Error reporting
  10. //
  11. #define _MESSAGEBOX_DEBUG
  12. #define MIN_PROPID 2
  13. #ifdef _DEBUG
  14. #define new DEBUG_NEW
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. class CWiaTestCmdLineInfo : public CCommandLineInfo {
  19. public:
  20. virtual void ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast);
  21. };
  22. void CWiaTestCmdLineInfo::ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
  23. {
  24. TRACE("CWiaTestCmdLineInfo::ParseParam(%s)\n",pszParam);
  25. }
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CWIATestApp
  28. BEGIN_MESSAGE_MAP(CWIATestApp, CWinApp)
  29. //{{AFX_MSG_MAP(CWIATestApp)
  30. ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  31. // NOTE - the ClassWizard will add and remove mapping macros here.
  32. // DO NOT EDIT what you see in these blocks of generated code!
  33. //}}AFX_MSG_MAP
  34. // Standard file based document commands
  35. ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
  36. ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  37. END_MESSAGE_MAP()
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CWIATestApp construction
  40. CWIATestApp::CWIATestApp()
  41. {
  42. // TODO: add construction code here,
  43. // Place all significant initialization in InitInstance
  44. }
  45. /////////////////////////////////////////////////////////////////////////////
  46. // The one and only CWIATestApp object
  47. CWIATestApp theApp;
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CWIATestApp initialization
  50. /**************************************************************************\
  51. * CWIATestApp::InitInstance()
  52. *
  53. * Initializes Instance of the WIATEST application
  54. *
  55. *
  56. * Arguments:
  57. *
  58. * none
  59. *
  60. * Return Value:
  61. *
  62. * status
  63. *
  64. * History:
  65. *
  66. * 2/14/1999 Original Version
  67. *
  68. \**************************************************************************/
  69. BOOL CWIATestApp::InitInstance()
  70. {
  71. AfxEnableControlContainer();
  72. // Standard initialization
  73. #ifdef _AFXDLL
  74. Enable3dControls(); // Call this when using MFC in a shared DLL
  75. #else
  76. Enable3dControlsStatic(); // Call this when linking to MFC statically
  77. #endif
  78. //SetRegistryKey(_T("WIATest"));
  79. //LoadStdProfileSettings(0); // Load standard INI file options (including MRU)
  80. // Register the application's document templates. Document templates
  81. // serve as the connection between documents, frame windows and views.
  82. //
  83. // Save Command line
  84. //
  85. m_CmdLine = m_lpCmdLine;
  86. CSingleDocTemplate* pDocTemplate;
  87. pDocTemplate = new CSingleDocTemplate(
  88. IDR_MAINFRAME,
  89. RUNTIME_CLASS(CWIATestDoc),
  90. RUNTIME_CLASS(CMainFrame), // main SDI frame window
  91. RUNTIME_CLASS(CWIATestView));
  92. AddDocTemplate(pDocTemplate);
  93. // Parse command line for standard shell commands, DDE, file open
  94. CWiaTestCmdLineInfo cmdInfo;
  95. ParseCommandLine(cmdInfo);
  96. // Dispatch commands specified on the command line
  97. if (!ProcessShellCommand(cmdInfo))
  98. return FALSE;
  99. // The one and only window has been initialized, so show and update it.
  100. m_pMainWnd->ShowWindow(SW_SHOW);
  101. m_pMainWnd->UpdateWindow();
  102. return TRUE;
  103. }
  104. /**************************************************************************\
  105. * CWIATestApp::ExitInstance()
  106. *
  107. * Exit routine for cleanup on WIATEST application
  108. *
  109. *
  110. * Arguments:
  111. *
  112. * none
  113. *
  114. * Return Value:
  115. *
  116. * status
  117. *
  118. * History:
  119. *
  120. * 2/14/1999 Original Version
  121. *
  122. \**************************************************************************/
  123. int CWIATestApp::ExitInstance()
  124. {
  125. return CWinApp::ExitInstance();
  126. }
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CWIATestApp message handlers
  129. /**************************************************************************\
  130. * CWIATestApp::GetDeviceIDCommandLine()
  131. *
  132. * Retrieves Command line
  133. *
  134. *
  135. * Arguments:
  136. *
  137. * none
  138. *
  139. * Return Value:
  140. *
  141. * CString - Command line
  142. *
  143. * History:
  144. *
  145. * 2/14/1999 Original Version
  146. *
  147. \**************************************************************************/
  148. CString CWIATestApp::GetDeviceIDCommandLine()
  149. {
  150. return m_CmdLine;
  151. }
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CAboutDlg dialog used for App About
  154. class CAboutDlg : public CDialog {
  155. public:
  156. CAboutDlg();
  157. // Dialog Data
  158. //{{AFX_DATA(CAboutDlg)
  159. enum {IDD = IDD_ABOUTBOX};
  160. //}}AFX_DATA
  161. // ClassWizard generated virtual function overrides
  162. //{{AFX_VIRTUAL(CAboutDlg)
  163. protected:
  164. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  165. //}}AFX_VIRTUAL
  166. // Implementation
  167. protected:
  168. //{{AFX_MSG(CAboutDlg)
  169. // No message handlers
  170. //}}AFX_MSG
  171. DECLARE_MESSAGE_MAP()
  172. };
  173. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  174. {
  175. //{{AFX_DATA_INIT(CAboutDlg)
  176. //}}AFX_DATA_INIT
  177. }
  178. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  179. {
  180. CDialog::DoDataExchange(pDX);
  181. //{{AFX_DATA_MAP(CAboutDlg)
  182. //}}AFX_DATA_MAP
  183. }
  184. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  185. //{{AFX_MSG_MAP(CAboutDlg)
  186. // No message handlers
  187. //}}AFX_MSG_MAP
  188. END_MESSAGE_MAP()
  189. /**************************************************************************\
  190. * CWIATestApp::OnAppAbout()
  191. *
  192. * Activates the About Dialog!
  193. *
  194. *
  195. * Arguments:
  196. *
  197. * none
  198. *
  199. * Return Value:
  200. *
  201. * none
  202. *
  203. * History:
  204. *
  205. * 2/14/1999 Original Version
  206. *
  207. \**************************************************************************/
  208. void CWIATestApp::OnAppAbout()
  209. {
  210. CAboutDlg aboutDlg;
  211. aboutDlg.DoModal();
  212. }
  213. //////////////////////////////////////////////////////////////////////////////////////////
  214. /////////////////////////////////// UTILS SECTION ////////////////////////////////////////
  215. //////////////////////////////////////////////////////////////////////////////////////////
  216. /**************************************************************************\
  217. * ::ReadPropStr
  218. *
  219. * Reads a BSTR value of a target property
  220. *
  221. *
  222. * Arguments:
  223. *
  224. * propid - property ID
  225. * pIWiaPropStg - property storage
  226. * pbstr - returned BSTR read from property
  227. *
  228. * Return Value:
  229. *
  230. * status
  231. *
  232. * History:
  233. *
  234. * 2/14/1999 Original Version
  235. *
  236. \**************************************************************************/
  237. HRESULT ReadPropStr(PROPID propid,IWiaPropertyStorage *pIWiaPropStg,BSTR *pbstr)
  238. {
  239. HRESULT hResult = S_OK;
  240. PROPSPEC PropSpec[1];
  241. PROPVARIANT PropVar[1];
  242. UINT cbSize = 0;
  243. *pbstr = NULL;
  244. memset(PropVar, 0, sizeof(PropVar));
  245. PropSpec[0].ulKind = PRSPEC_PROPID;
  246. PropSpec[0].propid = propid;
  247. hResult = pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  248. if (SUCCEEDED(hResult)) {
  249. if (PropVar[0].pwszVal) {
  250. *pbstr = SysAllocString(PropVar[0].pwszVal);
  251. } else {
  252. *pbstr = SysAllocString(L"");
  253. }
  254. if (*pbstr == NULL) {
  255. //StressStatus("* ReadPropStr, SysAllocString failed");
  256. hResult = E_OUTOFMEMORY;
  257. }
  258. PropVariantClear(PropVar);
  259. } else {
  260. //CString msg;
  261. //msg.Format("* ReadPropStr, ReadMultiple of propid: %d, Failed", propid);
  262. //StressStatus(msg);
  263. }
  264. return hResult;
  265. }
  266. /**************************************************************************\
  267. * ::WritePropStr
  268. *
  269. * Writes a BSTR value to a target property
  270. *
  271. *
  272. * Arguments:
  273. *
  274. * propid - property ID
  275. * pIWiaPropStg - property storage
  276. * pbstr - BSTR to write to target property
  277. *
  278. * Return Value:
  279. *
  280. * status
  281. *
  282. * History:
  283. *
  284. * 2/14/1999 Original Version
  285. *
  286. \**************************************************************************/
  287. HRESULT WritePropStr(PROPID propid, IWiaPropertyStorage *pIWiaPropStg, BSTR bstr)
  288. {
  289. HRESULT hResult = S_OK;
  290. PROPSPEC propspec[1];
  291. PROPVARIANT propvar[1];
  292. propspec[0].ulKind = PRSPEC_PROPID;
  293. propspec[0].propid = propid;
  294. propvar[0].vt = VT_BSTR;
  295. propvar[0].pwszVal = bstr;
  296. hResult = pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  297. return hResult;
  298. }
  299. /**************************************************************************\
  300. * ::WritePropLong
  301. *
  302. * Writes a LONG value of a target property
  303. *
  304. *
  305. * Arguments:
  306. *
  307. * propid - property ID
  308. * pIWiaPropStg - property storage
  309. * lVal - LONG to be written to target property
  310. *
  311. * Return Value:
  312. *
  313. * status
  314. *
  315. * History:
  316. *
  317. * 2/14/1999 Original Version
  318. *
  319. \**************************************************************************/
  320. HRESULT WritePropLong(PROPID propid, IWiaPropertyStorage *pIWiaPropStg, LONG lVal)
  321. {
  322. HRESULT hResult;
  323. PROPSPEC propspec[1];
  324. PROPVARIANT propvar[1];
  325. propspec[0].ulKind = PRSPEC_PROPID;
  326. propspec[0].propid = propid;
  327. propvar[0].vt = VT_I4;
  328. propvar[0].lVal = lVal;
  329. hResult = pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  330. return hResult;
  331. }
  332. /**************************************************************************\
  333. * ::WritePropGUID
  334. *
  335. * Writes a GUID value of a target property
  336. *
  337. *
  338. * Arguments:
  339. *
  340. * propid - property ID
  341. * pIWiaPropStg - property storage
  342. * guidVal - GUID to be written to target property
  343. *
  344. * Return Value:
  345. *
  346. * status
  347. *
  348. * History:
  349. *
  350. * 2/14/1999 Original Version
  351. *
  352. \**************************************************************************/
  353. HRESULT WritePropGUID(PROPID propid, IWiaPropertyStorage *pIWiaPropStg, GUID guidVal)
  354. {
  355. HRESULT hResult;
  356. PROPSPEC propspec[1];
  357. PROPVARIANT propvar[1];
  358. propspec[0].ulKind = PRSPEC_PROPID;
  359. propspec[0].propid = propid;
  360. propvar[0].vt = VT_CLSID;
  361. propvar[0].puuid = &guidVal;
  362. hResult = pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  363. return hResult;
  364. }
  365. /**************************************************************************\
  366. * ::WriteProp
  367. *
  368. * Writes a value of a target property
  369. *
  370. *
  371. * Arguments:
  372. *
  373. * VarType - Varient Type
  374. * propid - property ID
  375. * pIWiaPropStg - property storage
  376. * pVal - value to be written to target property (in string form)
  377. *
  378. * Return Value:
  379. *
  380. * status
  381. *
  382. * History:
  383. *
  384. * 2/14/1999 Original Version
  385. *
  386. \**************************************************************************/
  387. HRESULT WriteProp(unsigned int VarType,PROPID propid, IWiaPropertyStorage *pIWiaPropStg, LPCTSTR pVal)
  388. {
  389. HRESULT hResult;
  390. WCHAR wsbuffer[MAX_PATH];
  391. PROPSPEC propspec[1];
  392. PROPVARIANT propvar[1];
  393. propspec[0].ulKind = PRSPEC_PROPID;
  394. propspec[0].propid = propid;
  395. propvar[0].vt = (VARTYPE)VarType;
  396. propvar[0].puuid = (GUID *)& GUID_NULL;
  397. switch (VarType) {
  398. case VT_I1:
  399. sscanf(pVal,"%li",&propvar[0].cVal);
  400. break;
  401. case VT_I2:
  402. sscanf(pVal,"%li",&propvar[0].bVal);
  403. break;
  404. case VT_I4:
  405. sscanf(pVal,"%li",&propvar[0].lVal);
  406. break;
  407. case VT_I8:
  408. sscanf(pVal,"%li",&propvar[0].hVal);
  409. break;
  410. case VT_UI1:
  411. sscanf(pVal,"%li",&propvar[0].bVal);
  412. break;
  413. case VT_UI2:
  414. sscanf(pVal,"%li",&propvar[0].uiVal);
  415. break;
  416. case VT_UI4:
  417. sscanf(pVal,"%li",&propvar[0].ulVal);
  418. break;
  419. case VT_UI8:
  420. sscanf(pVal,"%li",&propvar[0].lVal);
  421. break;
  422. case VT_INT:
  423. sscanf(pVal,"%li",&propvar[0].intVal);
  424. break;
  425. case VT_R4:
  426. sscanf(pVal,"%f",&propvar[0].fltVal);
  427. break;
  428. case VT_R8:
  429. sscanf(pVal,"%f",&propvar[0].fltVal);
  430. break;
  431. case VT_BSTR:
  432. MultiByteToWideChar(CP_ACP, 0,pVal,-1,wsbuffer,MAX_PATH);
  433. propvar[0].bstrVal = SysAllocString(wsbuffer);
  434. break;
  435. case VT_CLSID:
  436. UuidFromString((UCHAR*)pVal,propvar[0].puuid);
  437. break;
  438. case VT_UINT:
  439. sscanf(pVal,"%li",&propvar[0].uintVal);
  440. break;
  441. default:
  442. sscanf(pVal,"%li",&propvar[0].lVal);
  443. break;
  444. }
  445. hResult = pIWiaPropStg->WriteMultiple(1, propspec, propvar, MIN_PROPID);
  446. return hResult;
  447. }
  448. /**************************************************************************\
  449. * ::ReadPropLong
  450. *
  451. * Reads a long value from a target property
  452. *
  453. *
  454. * Arguments:
  455. *
  456. * propid - property ID
  457. * pIWiaPropStg - property storage
  458. * plval - returned long read from property
  459. *
  460. * Return Value:
  461. *
  462. * status
  463. *
  464. * History:
  465. *
  466. * 2/14/1999 Original Version
  467. *
  468. \**************************************************************************/
  469. HRESULT ReadPropLong(PROPID propid, IWiaPropertyStorage *pIWiaPropStg, LONG *plval)
  470. {
  471. HRESULT hResult = S_OK;
  472. PROPSPEC PropSpec[1];
  473. PROPVARIANT PropVar[1];
  474. UINT cbSize = 0;
  475. memset(PropVar, 0, sizeof(PropVar));
  476. PropSpec[0].ulKind = PRSPEC_PROPID;
  477. PropSpec[0].propid = propid;
  478. hResult = pIWiaPropStg->ReadMultiple(1, PropSpec, PropVar);
  479. if (SUCCEEDED(hResult)) {
  480. *plval = PropVar[0].lVal;
  481. }
  482. return hResult;
  483. }
  484. /**************************************************************************\
  485. * ::StressStatus
  486. *
  487. * Reports status to user via status list box
  488. *
  489. * Arguments:
  490. *
  491. * status - CString value to be displayed in the list box
  492. *
  493. * Return Value:
  494. *
  495. * void
  496. *
  497. * History:
  498. *
  499. * 2/14/1999 Original Version
  500. *
  501. \**************************************************************************/
  502. void StressStatus(CString status)
  503. {
  504. /*
  505. int iLine = m_StressStatusListBox.AddString(status);
  506. m_StressStatusListBox.SetTopIndex(iLine);
  507. if(m_bLoggingEnabled)
  508. {
  509. if(m_LogFile)
  510. {
  511. m_LogFile.Write(status.GetBuffer(256),status.GetLength());
  512. m_LogFile.Write("\r\n",2);
  513. }
  514. }
  515. */
  516. OutputDebugString(status + "\n");
  517. #ifdef _DEBUG
  518. //OutputDebugString(status + "\n");
  519. #endif
  520. }
  521. /**************************************************************************\
  522. * ::StressStatus
  523. *
  524. * Reports status, and hResult to user via status list box
  525. *
  526. * Arguments:
  527. *
  528. * status - CString value to be displayed in the list box
  529. * hResult - hResult to be translated
  530. *
  531. * Return Value:
  532. *
  533. * void
  534. *
  535. * History:
  536. *
  537. * 2/14/1999 Original Version
  538. *
  539. \**************************************************************************/
  540. void StressStatus(CString status, HRESULT hResult)
  541. {
  542. CString msg;
  543. ULONG ulLen = MAX_PATH;
  544. LPTSTR pMsgBuf = (char*)LocalAlloc(LPTR,MAX_PATH);
  545. //
  546. // attempt to handle WIA custom errors first
  547. //
  548. switch (hResult) {
  549. case WIA_ERROR_GENERAL_ERROR:
  550. sprintf(pMsgBuf,"There was a general device failure.");
  551. break;
  552. case WIA_ERROR_PAPER_JAM:
  553. sprintf(pMsgBuf,"The paper path is jammed.");
  554. break;
  555. case WIA_ERROR_PAPER_EMPTY:
  556. sprintf(pMsgBuf,"There are no documents in the input tray to scan.");
  557. break;
  558. case WIA_ERROR_PAPER_PROBLEM:
  559. sprintf(pMsgBuf,"There is a general problem with an input document.");
  560. break;
  561. case WIA_ERROR_OFFLINE:
  562. sprintf(pMsgBuf,"The device is offline.");
  563. break;
  564. case WIA_ERROR_BUSY:
  565. sprintf(pMsgBuf,"The device is busy.");
  566. break;
  567. case WIA_ERROR_WARMING_UP:
  568. sprintf(pMsgBuf,"The device is warming up.");
  569. break;
  570. case WIA_ERROR_USER_INTERVENTION:
  571. sprintf(pMsgBuf,"The user has paused or stopped the device.");
  572. break;
  573. default:
  574. //
  575. // free temp buffer, because FormatMessage() will allocate it for me
  576. //
  577. LocalFree(pMsgBuf);
  578. ulLen = 0;
  579. ulLen = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  580. NULL, hResult, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  581. (LPTSTR)&pMsgBuf, 0, NULL);
  582. break;
  583. }
  584. if (ulLen) {
  585. msg = pMsgBuf;
  586. msg.TrimRight();
  587. LocalFree(pMsgBuf);
  588. } else {
  589. // use sprintf to write to buffer instead of .Format member of
  590. // CString. This conversion works better for HEX
  591. char buffer[255];
  592. sprintf(buffer,"hResult = 0x%08X",hResult);
  593. msg = buffer;
  594. }
  595. StressStatus(status + ", " + msg);
  596. #ifdef _MESSAGEBOX_DEBUG
  597. MessageBox(NULL,status+ ", " + msg,"WIATest Debug Report",MB_OK|MB_ICONERROR);
  598. #endif
  599. }