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.

157 lines
4.7 KiB

  1. // This is a part of the Microsoft Management Console.
  2. // Copyright (C) 1995-1996 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Management Console and related
  7. // electronic documentation provided with the interfaces.
  8. #include "precomp.hxx"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // Sample code to show how to Create DataObjects
  16. // Minimal error checking for clarity
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // Snap-in NodeType in both GUID format and string format
  19. // Note - Typically there is a node type for each different object, sample
  20. // only uses one node type.
  21. unsigned int CDataObject::m_cfNodeType = RegisterClipboardFormat(CCF_NODETYPE);
  22. unsigned int CDataObject::m_cfNodeTypeString = RegisterClipboardFormat(CCF_SZNODETYPE);
  23. unsigned int CDataObject::m_cfDisplayName = RegisterClipboardFormat(CCF_DISPLAY_NAME);
  24. unsigned int CDataObject::m_cfCoClass = RegisterClipboardFormat(CCF_SNAPIN_CLASSID);
  25. // Step 3
  26. unsigned int CDataObject::m_cfInternal = RegisterClipboardFormat(SNAPIN_INTERNAL);
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CDataObject implementations
  29. STDMETHODIMP CDataObject::GetDataHere(LPFORMATETC lpFormatetc, LPSTGMEDIUM lpMedium)
  30. {
  31. HRESULT hr = DV_E_CLIPFORMAT;
  32. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  33. // Based on the CLIPFORMAT write data to the stream
  34. const CLIPFORMAT cf = lpFormatetc->cfFormat;
  35. if(cf == m_cfNodeType)
  36. {
  37. hr = CreateNodeTypeData(lpMedium);
  38. }
  39. else if(cf == m_cfNodeTypeString)
  40. {
  41. hr = CreateNodeTypeStringData(lpMedium);
  42. }
  43. else if (cf == m_cfDisplayName)
  44. {
  45. hr = CreateDisplayName(lpMedium);
  46. }
  47. else if (cf == m_cfCoClass)
  48. {
  49. hr = CreateCoClassID(lpMedium);
  50. }
  51. else if (cf == m_cfInternal)
  52. {
  53. hr = CreateInternal(lpMedium);
  54. }
  55. return hr;
  56. }
  57. // Note - Sample does not implement these
  58. STDMETHODIMP CDataObject::GetData(LPFORMATETC lpFormatetcIn, LPSTGMEDIUM lpMedium)
  59. {
  60. return E_NOTIMPL;
  61. }
  62. STDMETHODIMP CDataObject::EnumFormatEtc(DWORD dwDirection, LPENUMFORMATETC* ppEnumFormatEtc)
  63. {
  64. return E_NOTIMPL;
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CDataObject creation members
  68. HRESULT CDataObject::Create(const void* pBuffer, int len, LPSTGMEDIUM lpMedium)
  69. {
  70. HRESULT hr = DV_E_TYMED;
  71. // Do some simple validation
  72. if (pBuffer == NULL || lpMedium == NULL)
  73. return E_POINTER;
  74. // Make sure the type medium is HGLOBAL
  75. if (lpMedium->tymed == TYMED_HGLOBAL)
  76. {
  77. // Create the stream on the hGlobal passed in
  78. LPSTREAM lpStream;
  79. hr = CreateStreamOnHGlobal(lpMedium->hGlobal, FALSE, &lpStream);
  80. if (SUCCEEDED(hr))
  81. {
  82. // Write to the stream the number of bytes
  83. unsigned long written;
  84. hr = lpStream->Write(pBuffer, len, &written);
  85. // Because we told CreateStreamOnHGlobal with 'FALSE',
  86. // only the stream is released here.
  87. // Note - the caller (i.e. snap-in, object) will free the HGLOBAL
  88. // at the correct time. This is according to the IDataObject specification.
  89. lpStream->Release();
  90. }
  91. }
  92. return hr;
  93. }
  94. HRESULT CDataObject::CreateNodeTypeData(LPSTGMEDIUM lpMedium)
  95. {
  96. // Create the node type object in GUID format
  97. return Create(reinterpret_cast<const void*>(&cNodeType), sizeof(GUID), lpMedium);
  98. }
  99. HRESULT CDataObject::CreateNodeTypeStringData(LPSTGMEDIUM lpMedium)
  100. {
  101. // Create the node type object in GUID string format
  102. return Create(cszNodeType, ((wcslen(cszNodeType)+1) * sizeof(wchar_t)), lpMedium);
  103. }
  104. HRESULT CDataObject::CreateDisplayName(LPSTGMEDIUM lpMedium)
  105. {
  106. // This is the display named used in the scope pane and snap-in manager
  107. // Load the name from resource
  108. // Note - if this is not provided, the console will used the snap-in name
  109. CString szDispName;
  110. szDispName.LoadString(IDS_NODENAME);
  111. return Create(szDispName, ((szDispName.GetLength()+1) * sizeof(wchar_t)), lpMedium);
  112. }
  113. HRESULT CDataObject::CreateCoClassID(LPSTGMEDIUM lpMedium)
  114. {
  115. // Create the CoClass information
  116. if (m_fMachine)
  117. return Create((LPVOID)&CLSID_MachineSnapin, sizeof(CLSID), lpMedium);
  118. else
  119. return Create((LPVOID)&CLSID_Snapin, sizeof(CLSID), lpMedium);
  120. }
  121. HRESULT CDataObject::CreateInternal(LPSTGMEDIUM lpMedium)
  122. {
  123. return Create(&m_internal, sizeof(INTERNAL), lpMedium);
  124. }