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.

180 lines
4.1 KiB

  1. // wiatestDoc.cpp : implementation of the CWiatestDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "wiatest.h"
  5. #include "wiatestDoc.h"
  6. #include "wiaselect.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CWiatestDoc
  14. IMPLEMENT_DYNCREATE(CWiatestDoc, CDocument)
  15. BEGIN_MESSAGE_MAP(CWiatestDoc, CDocument)
  16. //{{AFX_MSG_MAP(CWiatestDoc)
  17. // NOTE - the ClassWizard will add and remove mapping macros here.
  18. // DO NOT EDIT what you see in these blocks of generated code!
  19. //}}AFX_MSG_MAP
  20. END_MESSAGE_MAP()
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CWiatestDoc construction/destruction
  23. CWiatestDoc::CWiatestDoc()
  24. {
  25. m_pIRootItem = NULL;
  26. m_pICurrentItem = NULL;
  27. }
  28. CWiatestDoc::~CWiatestDoc()
  29. {
  30. ReleaseItems();
  31. }
  32. BOOL CWiatestDoc::OnNewDocument()
  33. {
  34. BOOL bSuccess = FALSE;
  35. if (!CDocument::OnNewDocument())
  36. return bSuccess;
  37. // select a WIA device
  38. CWiaselect SelectDeviceDlg;
  39. if(SelectDeviceDlg.DoModal() != IDOK){
  40. // no device was selected, so do not create a new document
  41. return bSuccess;
  42. }
  43. // a WIA device was selected, so continue
  44. HRESULT hr = S_OK;
  45. IWiaDevMgr *pIWiaDevMgr = NULL;
  46. hr = CoCreateInstance(CLSID_WiaDevMgr, NULL, CLSCTX_LOCAL_SERVER, IID_IWiaDevMgr,(void**)&pIWiaDevMgr);
  47. if(FAILED(hr)){
  48. // creation of device manager failed, so we can not continue
  49. ErrorMessageBox(IDS_WIATESTERROR_COCREATEWIADEVMGR,hr);
  50. return bSuccess;
  51. }
  52. // create WIA device
  53. hr = pIWiaDevMgr->CreateDevice(SelectDeviceDlg.m_bstrSelectedDeviceID, &m_pIRootItem);
  54. if(FAILED(hr)){
  55. bSuccess = FALSE;
  56. // creation of device failed, so we can not continue
  57. ErrorMessageBox(IDS_WIATESTERROR_CREATEDEVICE,hr);
  58. } else {
  59. bSuccess = TRUE;
  60. }
  61. // release WIA device manager
  62. pIWiaDevMgr->Release();
  63. // set document's title to be the WIA device's name
  64. TCHAR szDeviceName[MAX_PATH];
  65. GetDeviceName(szDeviceName);
  66. SetTitle(szDeviceName);
  67. return bSuccess;
  68. }
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CWiatestDoc serialization
  71. void CWiatestDoc::Serialize(CArchive& ar)
  72. {
  73. if (ar.IsStoring())
  74. {
  75. // TODO: add storing code here
  76. }
  77. else
  78. {
  79. // TODO: add loading code here
  80. }
  81. }
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CWiatestDoc diagnostics
  84. #ifdef _DEBUG
  85. void CWiatestDoc::AssertValid() const
  86. {
  87. CDocument::AssertValid();
  88. }
  89. void CWiatestDoc::Dump(CDumpContext& dc) const
  90. {
  91. CDocument::Dump(dc);
  92. }
  93. #endif //_DEBUG
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CWiatestDoc commands
  96. void CWiatestDoc::ReleaseItems()
  97. {
  98. // is CurrentItem different from RootItem?
  99. if(m_pICurrentItem != m_pIRootItem){
  100. // release CurrentItem
  101. if(m_pICurrentItem){
  102. m_pICurrentItem->Release();
  103. }
  104. // release RootItem
  105. if(m_pIRootItem){
  106. m_pIRootItem->Release();
  107. }
  108. } else {
  109. // CurrentItem is the RootItem
  110. // release RootItem and set CurrentItem to NULL
  111. if(m_pIRootItem){
  112. m_pIRootItem->Release();
  113. }
  114. }
  115. m_pIRootItem = NULL;
  116. m_pICurrentItem = NULL;
  117. }
  118. HRESULT CWiatestDoc::GetDeviceName(LPTSTR szDeviceName)
  119. {
  120. HRESULT hr = S_OK;
  121. if(NULL == m_pIRootItem){
  122. return E_FAIL;
  123. }
  124. CWiahelper WIA;
  125. hr = WIA.SetIWiaItem(m_pIRootItem);
  126. if(SUCCEEDED(hr)){
  127. hr = WIA.ReadPropertyString(WIA_DIP_DEV_NAME,szDeviceName);
  128. }
  129. return hr;
  130. }
  131. HRESULT CWiatestDoc::SetCurrentIWiaItem(IWiaItem *pIWiaItem)
  132. {
  133. HRESULT hr = S_OK;
  134. if(m_pICurrentItem){
  135. m_pICurrentItem->Release();
  136. m_pICurrentItem = NULL;
  137. }
  138. // AddRef the item, becuase we are storing it
  139. pIWiaItem->AddRef();
  140. // set the current item
  141. m_pICurrentItem = pIWiaItem;
  142. return hr;
  143. }