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.

175 lines
5.3 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. CString TempString = PropVar[1].bstrVal;
  77. lstrcpy(szDeviceName,TempString);
  78. // add name to listbox
  79. m_WiaDeviceListBox.AddString(szDeviceName);
  80. // add device ID to array
  81. m_bstrDeviceIDArray[m_lDeviceCount] = SysAllocString(PropVar[0].bstrVal);
  82. FreePropVariantArray(sizeof(PropSpec)/sizeof(PROPSPEC),PropVar);
  83. }
  84. // release property storage interface
  85. pIWiaPropStg->Release();
  86. // increment device counter
  87. m_lDeviceCount++;
  88. }
  89. } while (hr == S_OK);
  90. }
  91. }
  92. if(m_lDeviceCount <= 0){
  93. // no devices found?...
  94. // disable OK button
  95. CWnd *pOKButton = NULL;
  96. pOKButton = GetDlgItem(IDOK);
  97. if(NULL != pOKButton){
  98. pOKButton->EnableWindow(FALSE);
  99. }
  100. // add no device message
  101. m_WiaDeviceListBox.AddString(TEXT("<No WIA Devices Detected>"));
  102. }
  103. if(pIWiaDevMgr){
  104. pIWiaDevMgr->Release();
  105. pIWiaDevMgr = NULL;
  106. }
  107. }
  108. m_WiaDeviceListBox.SetCurSel(0);
  109. return TRUE; // return TRUE unless you set the focus to a control
  110. // EXCEPTION: OCX Property Pages should return FALSE
  111. }
  112. void CWiaselect::OnOK()
  113. {
  114. INT SelectedDeviceIndex = m_WiaDeviceListBox.GetCurSel();
  115. if(SelectedDeviceIndex >= 0){
  116. m_bstrSelectedDeviceID = SysAllocString(m_bstrDeviceIDArray[SelectedDeviceIndex]);
  117. }
  118. FreebstrDeviceIDArray();
  119. CDialog::OnOK();
  120. }
  121. void CWiaselect::FreebstrDeviceIDArray()
  122. {
  123. for(LONG i = 0; i < m_lDeviceCount; i++){
  124. if(NULL != m_bstrDeviceIDArray[i]){
  125. SysFreeString(m_bstrDeviceIDArray[i]);
  126. m_bstrDeviceIDArray[i] = NULL;
  127. }
  128. }
  129. }
  130. void CWiaselect::OnCancel()
  131. {
  132. FreebstrDeviceIDArray();
  133. CDialog::OnCancel();
  134. }
  135. void CWiaselect::OnDblclkWiadeviceListbox()
  136. {
  137. OnOK();
  138. }