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.

647 lines
17 KiB

  1. // DeviceCmdDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "DeviceCmdDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. // #define _REED // added for debugload of all commands
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CDeviceCmdDlg dialog
  14. /**************************************************************************\
  15. * CDeviceCmdDlg::CDeviceCmdDlg()
  16. *
  17. * Constructor for the Device Command Dialog
  18. *
  19. *
  20. * Arguments:
  21. *
  22. * pParent - Parent Window
  23. *
  24. *
  25. * Return Value:
  26. *
  27. * none
  28. *
  29. * History:
  30. *
  31. * 2/14/1999 Original Version
  32. *
  33. \**************************************************************************/
  34. CDeviceCmdDlg::CDeviceCmdDlg(CWnd* pParent /*=NULL*/)
  35. : CDialog(CDeviceCmdDlg::IDD, pParent)
  36. {
  37. //{{AFX_DATA_INIT(CDeviceCmdDlg)
  38. m_Flags = 0;
  39. m_FunctionCallText = _T("");
  40. //}}AFX_DATA_INIT
  41. }
  42. /**************************************************************************\
  43. * CDeviceCmdDlg::DoDataExchange()
  44. *
  45. * Handles control message maps to the correct member variables
  46. *
  47. *
  48. * Arguments:
  49. *
  50. * pDX - DataExchange object
  51. *
  52. * Return Value:
  53. *
  54. * none
  55. *
  56. * History:
  57. *
  58. * 2/14/1999 Original Version
  59. *
  60. \**************************************************************************/
  61. void CDeviceCmdDlg::DoDataExchange(CDataExchange* pDX)
  62. {
  63. CDialog::DoDataExchange(pDX);
  64. //{{AFX_DATA_MAP(CDeviceCmdDlg)
  65. DDX_Control(pDX, IDC_LIST_ITEMPROP, m_ItemPropertyListControl);
  66. DDX_Control(pDX, IDC_COMMAND_LISTBOX, m_CommandListBox);
  67. DDX_Text(pDX, IDC_FLAGS_EDITBOX, m_Flags);
  68. DDX_Text(pDX, IDC_FUNCTIONCALLTEXT, m_FunctionCallText);
  69. //}}AFX_DATA_MAP
  70. }
  71. BEGIN_MESSAGE_MAP(CDeviceCmdDlg, CDialog)
  72. //{{AFX_MSG_MAP(CDeviceCmdDlg)
  73. ON_BN_CLICKED(IDC_SEND_COMMAND, OnSendCommand)
  74. ON_EN_KILLFOCUS(IDC_FLAGS_EDITBOX, OnKillfocusFlagsEditbox)
  75. ON_LBN_SELCHANGE(IDC_COMMAND_LISTBOX, OnSelchangeCommandListbox)
  76. //}}AFX_MSG_MAP
  77. END_MESSAGE_MAP()
  78. /////////////////////////////////////////////////////////////////////////////
  79. // CDeviceCmdDlg message handlers
  80. /**************************************************************************\
  81. * CDeviceCmdDlg::Initialize()
  82. *
  83. * Sets the current item to operate commands on
  84. *
  85. *
  86. * Arguments:
  87. *
  88. * pIWiaItem - Item to use for command operations
  89. *
  90. * Return Value:
  91. *
  92. * void
  93. *
  94. * History:
  95. *
  96. * 2/14/1999 Original Version
  97. *
  98. \**************************************************************************/
  99. void CDeviceCmdDlg::Initialize(IWiaItem *pIWiaItem)
  100. {
  101. m_pIWiaItem = pIWiaItem;
  102. }
  103. /**************************************************************************\
  104. * CDeviceCmdDlg::OnInitDialog()
  105. *
  106. * Initializes the Command dialog's controls/display
  107. *
  108. *
  109. * Arguments:
  110. *
  111. * none
  112. *
  113. * Return Value:
  114. *
  115. * status
  116. *
  117. * History:
  118. *
  119. * 2/14/1999 Original Version
  120. *
  121. \**************************************************************************/
  122. BOOL CDeviceCmdDlg::OnInitDialog()
  123. {
  124. CDialog::OnInitDialog();
  125. HFONT hFixedFont = (HFONT)GetStockObject(ANSI_FIXED_FONT);
  126. if(hFixedFont != NULL)
  127. m_CommandListBox.SendMessage(WM_SETFONT,(WPARAM)hFixedFont,0);
  128. //
  129. // initialize headers for Property list control
  130. //
  131. m_ItemPropertyListControl.InitHeaders();
  132. m_ItemPropertyListControl.DisplayItemPropData(m_pIWiaItem);
  133. m_Flags = 0;
  134. m_pOptionalItem = NULL;
  135. EnumerateDeviceCapsToListBox();
  136. m_CommandListBox.SetCurSel(0);
  137. OnSelchangeCommandListbox();
  138. return TRUE; // return TRUE unless you set the focus to a control
  139. // EXCEPTION: OCX Property Pages should return FALSE
  140. }
  141. /**************************************************************************\
  142. * CDeviceCmdDlg::FormatFunctionCallText()
  143. *
  144. * Formats the Command call into a readable/displayed CString
  145. *
  146. *
  147. * Arguments:
  148. *
  149. * none
  150. *
  151. * Return Value:
  152. *
  153. * void
  154. *
  155. * History:
  156. *
  157. * 2/14/1999 Original Version
  158. *
  159. \**************************************************************************/
  160. void CDeviceCmdDlg::FormatFunctionCallText()
  161. {
  162. // format flags param
  163. CString strFlag;
  164. strFlag.Format("%d",m_Flags);
  165. // format GUID param
  166. CString strGUID;
  167. strGUID = ConvertGUIDToKnownCString(GetCommandFromListBox());
  168. // format pIWiaItem param
  169. CString strOptionalItem;
  170. strOptionalItem.Format("%p",m_pOptionalItem);
  171. m_FunctionCallText = "Flags = "+strFlag+", Command = "+strGUID+", pIWiaItem = "+strOptionalItem+"\nhResult = " + m_strhResult;
  172. UpdateData(FALSE);
  173. }
  174. /**************************************************************************\
  175. * CDeviceCmdDlg::EnumerateDeviceCapsToListBox()
  176. *
  177. * Enumerates all supported device commands, and events to the command selection
  178. * listbox
  179. *
  180. *
  181. * Arguments:
  182. *
  183. * none
  184. *
  185. * Return Value:
  186. *
  187. * status
  188. *
  189. * History:
  190. *
  191. * 2/14/1999 Original Version
  192. *
  193. \**************************************************************************/
  194. BOOL CDeviceCmdDlg::EnumerateDeviceCapsToListBox()
  195. {
  196. #ifdef _REED
  197. // false loading of commands for debugging
  198. DebugLoadCommands();
  199. #else
  200. WIA_DEV_CAP* pDevCap = NULL;
  201. IEnumWIA_DEV_CAPS* pIEnumWiaDevCaps;
  202. HRESULT hResult = S_OK;
  203. hResult = m_pIWiaItem->EnumDeviceCapabilities(WIA_DEVICE_COMMANDS | WIA_DEVICE_EVENTS,&pIEnumWiaDevCaps);
  204. if(hResult != S_OK)
  205. {
  206. AfxMessageBox("m_pIWiaItem->EnumDeviceCapabilities() Failed.." + hResultToCString(hResult));
  207. return FALSE;
  208. }
  209. else
  210. {
  211. int CapIndex = 0;
  212. do {
  213. pDevCap = (WIA_DEV_CAP*) LocalAlloc(LPTR, sizeof(WIA_DEV_CAP));
  214. if (pDevCap) {
  215. hResult = pIEnumWiaDevCaps->Next(1,pDevCap,NULL);
  216. if (hResult == S_OK)
  217. {
  218. AddDevCapToListBox(CapIndex,pDevCap);
  219. CapIndex++;
  220. if(pDevCap)
  221. {
  222. if(pDevCap->bstrName)
  223. SysFreeString(pDevCap->bstrName);
  224. if(pDevCap->bstrDescription)
  225. SysFreeString(pDevCap->bstrDescription);
  226. CoTaskMemFree(pDevCap);
  227. }
  228. }
  229. } else {
  230. hResult = E_OUTOFMEMORY;
  231. AfxMessageBox("m_pIWiaItem->EnumDeviceCapabilities() Failed.." + hResultToCString(hResult));
  232. return FALSE;
  233. }
  234. }while(hResult == S_OK);
  235. pIEnumWiaDevCaps->Release();
  236. }
  237. #endif
  238. return TRUE;
  239. }
  240. /**************************************************************************\
  241. * CDeviceCmdDlg::OnSendCommand()
  242. *
  243. * Sends the selected command
  244. *
  245. *
  246. * Arguments:
  247. *
  248. * none
  249. *
  250. * Return Value:
  251. *
  252. * void
  253. *
  254. * History:
  255. *
  256. * 2/14/1999 Original Version
  257. *
  258. \**************************************************************************/
  259. void CDeviceCmdDlg::OnSendCommand()
  260. {
  261. // get command from list box
  262. GUID Command = GetCommandFromListBox();
  263. // get flags flags from edit box
  264. LONG Flags = m_Flags;
  265. // send command
  266. // set m_pOptionalItem pointer to NULL (don't release it, the app will do this later)
  267. m_pOptionalItem = NULL;
  268. HRESULT hResult = m_pIWiaItem->DeviceCommand(Flags,&Command,&m_pOptionalItem);
  269. if (hResult != S_OK)
  270. {
  271. //WIA_ERROR(("*CWIACameraPg()* m_pIWiaItem->DeviceCommand() failed hResult = 0x%lx\n",hResult));
  272. }
  273. else
  274. {
  275. if(m_pOptionalItem != NULL)
  276. m_ItemPropertyListControl.DisplayItemPropData(m_pOptionalItem);
  277. }
  278. m_strhResult = hResultToCString(hResult);
  279. UpdateData(TRUE);
  280. FormatFunctionCallText();
  281. }
  282. /**************************************************************************\
  283. * CDeviceCmdDlg::GetCommandFromListBox()
  284. *
  285. * Returns the selected command GUID from the command list box
  286. *
  287. *
  288. * Arguments:
  289. *
  290. * none
  291. *
  292. * Return Value:
  293. *
  294. * GUID - selected command
  295. *
  296. * History:
  297. *
  298. * 2/14/1999 Original Version
  299. *
  300. \**************************************************************************/
  301. GUID CDeviceCmdDlg::GetCommandFromListBox()
  302. {
  303. // get current listbox selection
  304. int CurSel = m_CommandListBox.GetCurSel();
  305. GUID* pGUID = NULL;
  306. if(CurSel != -1)
  307. {
  308. // get GUID from current selection
  309. pGUID = (GUID*)m_CommandListBox.GetItemDataPtr(CurSel);
  310. if(pGUID != NULL)
  311. return *pGUID;
  312. else
  313. {
  314. AfxMessageBox("GUID is NULL");
  315. return WIA_CMD_SYNCHRONIZE;
  316. }
  317. }
  318. else
  319. {
  320. // just send back synchronize for fun.. ?/ DEBUG
  321. return WIA_CMD_SYNCHRONIZE;
  322. }
  323. }
  324. /**************************************************************************\
  325. * CDeviceCmdDlg::GUIDToCString()
  326. *
  327. * Formats a GUID into a CString (There is a better way to do this..I will fix
  328. * this later)
  329. *
  330. *
  331. * Arguments:
  332. *
  333. * guid - GUID to format
  334. *
  335. * Return Value:
  336. *
  337. * CString - Formatted GUID in CString format
  338. *
  339. * History:
  340. *
  341. * 2/14/1999 Original Version
  342. *
  343. \**************************************************************************/
  344. CString CDeviceCmdDlg::GUIDToCString(GUID guid)
  345. {
  346. CString strGUID;
  347. strGUID.Format("GUID = %8x-%lx-%lx-%2x%2x%2x%2x%2x%2x%2x%2x",
  348. guid.Data1,
  349. guid.Data2,
  350. guid.Data3,
  351. guid.Data4[0],
  352. guid.Data4[1],
  353. guid.Data4[2],
  354. guid.Data4[3],
  355. guid.Data4[4],
  356. guid.Data4[5],
  357. guid.Data4[6],
  358. guid.Data4[7]);
  359. return strGUID;
  360. }
  361. /**************************************************************************\
  362. * CDeviceCmdDlg::AddDevCapToListBox()
  363. *
  364. * Adds a Device capability to the command listbox
  365. *
  366. *
  367. * Arguments:
  368. *
  369. * CapIndex - Position for the command to be placed into the command list box
  370. * pDevCapStruct - Device capability structure containing the supported device command info.
  371. *
  372. * Return Value:
  373. *
  374. * status
  375. *
  376. * History:
  377. *
  378. * 2/14/1999 Original Version
  379. *
  380. \**************************************************************************/
  381. BOOL CDeviceCmdDlg::AddDevCapToListBox(int CapIndex,WIA_DEV_CAP *pDevCapStruct)
  382. {
  383. m_CommandListBox.InsertString(CapIndex,GUIDToCString(pDevCapStruct->guid)+" "+(CString)pDevCapStruct->bstrDescription);
  384. // alloc data pointer for list box.
  385. // the list box will free this memory on destruction..
  386. GUID* pGUID = (GUID*)LocalAlloc(LPTR,sizeof(GUID));
  387. memcpy(pGUID,&pDevCapStruct->guid,sizeof(GUID));
  388. m_CommandListBox.SetItemDataPtr(CapIndex,(LPVOID)pGUID);
  389. return TRUE;
  390. }
  391. /**************************************************************************\
  392. * CDeviceCmdDlg::OnKillfocusFlagsbox()
  393. *
  394. * Handles the window's message when the focus has left the flags edit box
  395. * When focus has left the control, it updates the formatted function call text.
  396. *
  397. *
  398. * Arguments:
  399. *
  400. * none
  401. *
  402. * Return Value:
  403. *
  404. * void
  405. *
  406. * History:
  407. *
  408. * 2/14/1999 Original Version
  409. *
  410. \**************************************************************************/
  411. void CDeviceCmdDlg::OnKillfocusFlagsEditbox()
  412. {
  413. UpdateData(TRUE);
  414. FormatFunctionCallText();
  415. }
  416. /**************************************************************************\
  417. * CDeviceCmdDlg::ConvertGUIDToKnownCString()
  418. *
  419. * Converts a command GUID into a readable CString for display only
  420. *
  421. *
  422. * Arguments:
  423. *
  424. * guid - Command GUID to convert
  425. *
  426. * Return Value:
  427. *
  428. * CString - converted GUID
  429. *
  430. * History:
  431. *
  432. * 2/14/1999 Original Version
  433. *
  434. \**************************************************************************/
  435. CString CDeviceCmdDlg::ConvertGUIDToKnownCString(GUID guid)
  436. {
  437. // big nasty way to convert a known command into a string..
  438. // no points for speed. :)
  439. if(guid == WIA_CMD_SYNCHRONIZE)
  440. return "WIA_CMD_SYNCHRONIZE";
  441. else if(guid == WIA_CMD_TAKE_PICTURE)
  442. return "WIA_CMD_TAKE_PICTURE";
  443. else if(guid == WIA_CMD_DELETE_ALL_ITEMS)
  444. return "WIA_CMD_DELETE_ALL_ITEMS";
  445. else if(guid == WIA_CMD_CHANGE_DOCUMENT)
  446. return "WIA_CMD_CHANGE_DOCUMENT";
  447. else if(guid == WIA_CMD_UNLOAD_DOCUMENT)
  448. return "WIA_CMD_UNLOAD_DOCUMENT";
  449. else if(guid == WIA_EVENT_DEVICE_DISCONNECTED)
  450. return "WIA_EVENT_DEVICE_DISCONNECTED";
  451. else if(guid == WIA_EVENT_DEVICE_CONNECTED)
  452. return "WIA_EVENT_DEVICE_CONNECTED";
  453. else if(guid == WIA_CMD_DELETE_DEVICE_TREE)
  454. return "WIA_CMD_DELETE_DEVICE_TREE";
  455. else if(guid == WIA_CMD_BUILD_DEVICE_TREE)
  456. return "WIA_CMD_BUILD_DEVICE_TREE";
  457. else
  458. return "WIA_CMD_USERDEFINED";
  459. }
  460. /**************************************************************************\
  461. * CDeviceCmdDlg::DebugLoadCommands()
  462. *
  463. * Loads a set of pre-loaded commands for debugging only
  464. *
  465. *
  466. * Arguments:
  467. *
  468. * none
  469. *
  470. * Return Value:
  471. *
  472. * void
  473. *
  474. * History:
  475. *
  476. * 2/14/1999 Original Version
  477. *
  478. \**************************************************************************/
  479. void CDeviceCmdDlg::DebugLoadCommands()
  480. {
  481. BSTR bstrCapName;
  482. BSTR bstrCapFriendlyName;
  483. WIA_DEV_CAP pDevCap[9];
  484. // load WIA_CMD_SYNCHRONIZE
  485. bstrCapName = ::SysAllocString(L"Syncronize");
  486. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_SYNCHRONIZE");
  487. pDevCap[0].guid = WIA_CMD_SYNCHRONIZE;
  488. pDevCap[0].bstrName = bstrCapName;
  489. pDevCap[0].bstrDescription = bstrCapFriendlyName;
  490. AddDevCapToListBox(0,pDevCap);
  491. // load WIA_CMD_TAKE_PICTURE
  492. bstrCapName = ::SysAllocString(L"Take Picture");
  493. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_TAKE_PICTURE");
  494. pDevCap[1].guid = WIA_CMD_TAKE_PICTURE;
  495. pDevCap[1].bstrName = bstrCapName;
  496. pDevCap[1].bstrDescription = bstrCapFriendlyName;
  497. AddDevCapToListBox(1,&pDevCap[1]);
  498. // load WIA_CMD_DELETE_ALL_ITEMS
  499. bstrCapName = ::SysAllocString(L"Delete all items");
  500. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_DELETE_ALL_ITEMS");
  501. pDevCap[2].guid = WIA_CMD_DELETE_ALL_ITEMS;
  502. pDevCap[2].bstrName = bstrCapName;
  503. pDevCap[2].bstrDescription = bstrCapFriendlyName;
  504. AddDevCapToListBox(2,&pDevCap[2]);
  505. // load WIA_CMD_CHANGE_DOCUMENT
  506. bstrCapName = ::SysAllocString(L"Change Document");
  507. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_CHANGE_DOCUMENT");
  508. pDevCap[3].guid = WIA_CMD_CHANGE_DOCUMENT;
  509. pDevCap[3].bstrName = bstrCapName;
  510. pDevCap[3].bstrDescription = bstrCapFriendlyName;
  511. AddDevCapToListBox(3,&pDevCap[3]);
  512. // load WIA_CMD_UNLOAD_DOCUMENT
  513. bstrCapName = ::SysAllocString(L"Unload Document");
  514. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_UNLOAD_DOCUMENT");
  515. pDevCap[4].guid = WIA_CMD_UNLOAD_DOCUMENT;
  516. pDevCap[4].bstrName = bstrCapName;
  517. pDevCap[4].bstrDescription = bstrCapFriendlyName;
  518. AddDevCapToListBox(4,&pDevCap[4]);
  519. // load WIA_EVENT_DEVICE_DISCONNECTED
  520. bstrCapName = ::SysAllocString(L"Disconnect Event");
  521. bstrCapFriendlyName = ::SysAllocString(L"WIA_EVENT_DEVICE_DISCONNECTED");
  522. pDevCap[5].guid = WIA_EVENT_DEVICE_DISCONNECTED;
  523. pDevCap[5].bstrName = bstrCapName;
  524. pDevCap[5].bstrDescription = bstrCapFriendlyName;
  525. AddDevCapToListBox(5,&pDevCap[5]);
  526. // load WIA_EVENT_DEVICE_CONNECTED
  527. bstrCapName = ::SysAllocString(L"Connect Event");
  528. bstrCapFriendlyName = ::SysAllocString(L"WIA_EVENT_DEVICE_CONNECTED");
  529. pDevCap[6].guid = WIA_EVENT_DEVICE_CONNECTED;
  530. pDevCap[6].bstrName = bstrCapName;
  531. pDevCap[6].bstrDescription = bstrCapFriendlyName;
  532. AddDevCapToListBox(6,&pDevCap[6]);
  533. // load WIA_CMD_DELETE_DEVICE_TREE
  534. bstrCapName = ::SysAllocString(L"Delete Device Tree");
  535. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_DELETE_DEVICE_TREE");
  536. pDevCap[7].guid = WIA_CMD_DELETE_DEVICE_TREE;
  537. pDevCap[7].bstrName = bstrCapName;
  538. pDevCap[7].bstrDescription = bstrCapFriendlyName;
  539. AddDevCapToListBox(7,&pDevCap[7]);
  540. // load WIA_CMD_BUILD_DEVICE_TREE
  541. bstrCapName = ::SysAllocString(L"Build Device Tree");
  542. bstrCapFriendlyName = ::SysAllocString(L"WIA_CMD_BUILD_DEVICE_TREE");
  543. pDevCap[8].guid = WIA_CMD_BUILD_DEVICE_TREE;
  544. pDevCap[8].bstrName = bstrCapName;
  545. pDevCap[8].bstrDescription = bstrCapFriendlyName;
  546. AddDevCapToListBox(8,&pDevCap[8]);
  547. }
  548. /**************************************************************************\
  549. * CDeviceCmdDlg::OnSelchangeCommandListbox()
  550. *
  551. * Handles the window's message when a user changes the selection in the
  552. * command listbox
  553. *
  554. *
  555. * Arguments:
  556. *
  557. * none
  558. *
  559. * Return Value:
  560. *
  561. * void
  562. *
  563. * History:
  564. *
  565. * 2/14/1999 Original Version
  566. *
  567. \**************************************************************************/
  568. void CDeviceCmdDlg::OnSelchangeCommandListbox()
  569. {
  570. UpdateData(TRUE);
  571. FormatFunctionCallText();
  572. }
  573. /**************************************************************************\
  574. * CDeviceCmdDlg::hResultToCString()
  575. *
  576. * Converts a hResult value into a readable CString for display only
  577. *
  578. *
  579. * Arguments:
  580. *
  581. * hResult - some HRESULT
  582. *
  583. * Return Value:
  584. *
  585. * CString - readable error return
  586. *
  587. * History:
  588. *
  589. * 2/14/1999 Original Version
  590. *
  591. \**************************************************************************/
  592. CString CDeviceCmdDlg::hResultToCString(HRESULT hResult)
  593. {
  594. CString strhResult = "";
  595. ULONG ulLen = 0;
  596. LPTSTR pMsgBuf;
  597. ulLen = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  598. NULL, hResult, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  599. (LPTSTR)&pMsgBuf, 0, NULL);
  600. if (ulLen)
  601. {
  602. strhResult = pMsgBuf;
  603. strhResult.TrimRight();
  604. LocalFree(pMsgBuf);
  605. }
  606. else
  607. {
  608. // use sprintf to write to buffer instead of .Format member of
  609. // CString. This conversion works better for HEX
  610. char buffer[255];
  611. sprintf(buffer," 0x%08X",hResult);
  612. strhResult = buffer;
  613. }
  614. return strhResult;
  615. }