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.

173 lines
5.4 KiB

  1. // Wiaselect.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "Wiaselect.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CWiaselect dialog
  13. CWiaselect::CWiaselect(CWnd* pParent /*=NULL*/)
  14. : CDialog(CWiaselect::IDD, pParent)
  15. {
  16. m_bstrSelectedDeviceID = NULL;
  17. m_lDeviceCount = 0;
  18. //{{AFX_DATA_INIT(CWiaselect)
  19. // NOTE: the ClassWizard will add member initialization here
  20. //}}AFX_DATA_INIT
  21. }
  22. void CWiaselect::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CWiaselect)
  26. DDX_Control(pDX, IDC_WIADEVICE_LISTBOX, m_WiaDeviceListBox);
  27. //}}AFX_DATA_MAP
  28. }
  29. BEGIN_MESSAGE_MAP(CWiaselect, CDialog)
  30. //{{AFX_MSG_MAP(CWiaselect)
  31. ON_LBN_DBLCLK(IDC_WIADEVICE_LISTBOX, OnDblclkWiadeviceListbox)
  32. //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CWiaselect message handlers
  36. BOOL CWiaselect::OnInitDialog()
  37. {
  38. CDialog::OnInitDialog();
  39. // a WIA device was selected, so continue
  40. HRESULT hr = S_OK;
  41. IWiaDevMgr *pIWiaDevMgr = NULL;
  42. hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr,(void**)&pIWiaDevMgr);
  43. if(FAILED(hr)){
  44. // creation of device manager failed, so we can not continue
  45. ErrorMessageBox(TEXT("CoCreateInstance failed trying to create the WIA device manager"),hr);
  46. return FALSE;
  47. } else {
  48. // enumerate devices, and fill WIA Device listbox
  49. m_lDeviceCount = 0;
  50. ULONG ulFetched = 0;
  51. IWiaPropertyStorage *pIWiaPropStg = NULL;
  52. IEnumWIA_DEV_INFO *pWiaEnumDevInfo = NULL;
  53. hr = pIWiaDevMgr->EnumDeviceInfo(WIA_DEVINFO_ENUM_LOCAL,&pWiaEnumDevInfo);
  54. if (SUCCEEDED(hr)){
  55. hr = pWiaEnumDevInfo->Reset();
  56. if (SUCCEEDED(hr)) {
  57. do {
  58. hr = pWiaEnumDevInfo->Next(1,&pIWiaPropStg,&ulFetched);
  59. if (hr == S_OK) {
  60. PROPSPEC PropSpec[2];
  61. PROPVARIANT PropVar [2];
  62. memset(PropVar,0,sizeof(PropVar));
  63. PropSpec[0].ulKind = PRSPEC_PROPID;
  64. PropSpec[0].propid = WIA_DIP_DEV_ID;
  65. PropSpec[1].ulKind = PRSPEC_PROPID;
  66. PropSpec[1].propid = WIA_DIP_DEV_NAME;
  67. hr = pIWiaPropStg->ReadMultiple(sizeof(PropSpec)/sizeof(PROPSPEC), PropSpec, PropVar);
  68. if (hr == S_OK) {
  69. // Device ID
  70. // PropVar[0].bstrVal
  71. // Device Name
  72. // PropVar[1].bstrVal
  73. TCHAR szDeviceName[MAX_PATH];
  74. memset(szDeviceName,0,sizeof(szDeviceName));
  75. TSPRINTF(szDeviceName,TEXT("%ws"),PropVar[1].bstrVal);
  76. // add name to listbox
  77. m_WiaDeviceListBox.AddString(szDeviceName);
  78. // add device ID to array
  79. m_bstrDeviceIDArray[m_lDeviceCount] = SysAllocString(PropVar[0].bstrVal);
  80. FreePropVariantArray(sizeof(PropSpec)/sizeof(PROPSPEC),PropVar);
  81. }
  82. // release property storage interface
  83. pIWiaPropStg->Release();
  84. // increment device counter
  85. m_lDeviceCount++;
  86. }
  87. } while (hr == S_OK);
  88. }
  89. }
  90. if(m_lDeviceCount <= 0){
  91. // no devices found?...
  92. // disable OK button
  93. CWnd *pOKButton = NULL;
  94. pOKButton = GetDlgItem(IDOK);
  95. if(NULL != pOKButton){
  96. pOKButton->EnableWindow(FALSE);
  97. }
  98. // add no device message
  99. m_WiaDeviceListBox.AddString(TEXT("<No WIA Devices Detected>"));
  100. }
  101. if(pIWiaDevMgr){
  102. pIWiaDevMgr->Release();
  103. pIWiaDevMgr = NULL;
  104. }
  105. }
  106. m_WiaDeviceListBox.SetCurSel(0);
  107. return TRUE; // return TRUE unless you set the focus to a control
  108. // EXCEPTION: OCX Property Pages should return FALSE
  109. }
  110. void CWiaselect::OnOK()
  111. {
  112. INT SelectedDeviceIndex = m_WiaDeviceListBox.GetCurSel();
  113. if(SelectedDeviceIndex >= 0){
  114. m_bstrSelectedDeviceID = SysAllocString(m_bstrDeviceIDArray[SelectedDeviceIndex]);
  115. }
  116. FreebstrDeviceIDArray();
  117. CDialog::OnOK();
  118. }
  119. void CWiaselect::FreebstrDeviceIDArray()
  120. {
  121. for(LONG i = 0; i < m_lDeviceCount; i++){
  122. if(NULL != m_bstrDeviceIDArray[i]){
  123. SysFreeString(m_bstrDeviceIDArray[i]);
  124. m_bstrDeviceIDArray[i] = NULL;
  125. }
  126. }
  127. }
  128. void CWiaselect::OnCancel()
  129. {
  130. FreebstrDeviceIDArray();
  131. CDialog::OnCancel();
  132. }
  133. void CWiaselect::OnDblclkWiadeviceListbox()
  134. {
  135. OnOK();
  136. }