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.

293 lines
8.2 KiB

  1. // DataObj.cpp : Implementation of data object classes
  2. #include "stdafx.h"
  3. #include "stdutils.h" // GetObjectType() utility routines
  4. #include "macros.h"
  5. USE_HANDLE_MACROS("MYCOMPUT(dataobj.cpp)")
  6. #include "dataobj.h"
  7. #include "compdata.h"
  8. #include "resource.h" // IDS_SCOPE_MYCOMPUTER
  9. #include <comstrm.h>
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #include "stddtobj.cpp"
  16. #ifdef __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  17. // Additional clipboard formats for the Service context menu extension
  18. CLIPFORMAT g_cfServiceName = (CLIPFORMAT)::RegisterClipboardFormat(L"FILEMGMT_SNAPIN_SERVICE_NAME");
  19. CLIPFORMAT g_cfServiceDisplayName = (CLIPFORMAT)::RegisterClipboardFormat(L"FILEMGMT_SNAPIN_SERVICE_DISPLAYNAME");
  20. #endif // __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  21. // Additional clipboard formats for the Send Console Message snapin
  22. //CLIPFORMAT CMyComputerDataObject::m_cfSendConsoleMessageText = (CLIPFORMAT)::RegisterClipboardFormat(_T("mmc.sendcmsg.MessageText"));
  23. CLIPFORMAT CMyComputerDataObject::m_cfSendConsoleMessageRecipients = (CLIPFORMAT)::RegisterClipboardFormat(_T("mmc.sendcmsg.MessageRecipients"));
  24. /////////////////////////////////////////////////////////////////////
  25. // CMyComputerDataObject::IDataObject::GetDataHere()
  26. HRESULT CMyComputerDataObject::GetDataHere(
  27. FORMATETC __RPC_FAR *pFormatEtcIn,
  28. STGMEDIUM __RPC_FAR *pMedium)
  29. {
  30. MFC_TRY;
  31. const CLIPFORMAT cf=pFormatEtcIn->cfFormat;
  32. if (cf == m_CFNodeType)
  33. {
  34. const GUID* pguid = GetObjectTypeGUID( m_pcookie->m_objecttype );
  35. stream_ptr s(pMedium);
  36. return s.Write(pguid, sizeof(GUID));
  37. }
  38. else if (cf == m_CFSnapInCLSID)
  39. {
  40. const GUID* pguid = &CLSID_MyComputer;
  41. stream_ptr s(pMedium);
  42. return s.Write(pguid, sizeof(GUID));
  43. }
  44. else if (cf == m_CFNodeTypeString)
  45. {
  46. const BSTR strGUID = GetObjectTypeString( m_pcookie->m_objecttype );
  47. stream_ptr s(pMedium);
  48. return s.Write(strGUID);
  49. }
  50. else if (cf == m_CFDisplayName)
  51. {
  52. return PutDisplayName(pMedium);
  53. }
  54. else if (cf == m_CFDataObjectType)
  55. {
  56. stream_ptr s(pMedium);
  57. return s.Write(&m_dataobjecttype, sizeof(m_dataobjecttype));
  58. }
  59. else if (cf == m_CFMachineName)
  60. {
  61. stream_ptr s(pMedium);
  62. LPCWSTR pszMachineName = m_pcookie->QueryNonNULLMachineName();
  63. if ( !wcsncmp (pszMachineName, L"\\\\", 2) )
  64. pszMachineName += 2; // skip whackwhack
  65. return s.Write(pszMachineName);
  66. }
  67. else if (cf == m_CFRawCookie)
  68. {
  69. stream_ptr s(pMedium);
  70. // CODEWORK This cast ensures that the data format is
  71. // always a CCookie*, even for derived subclasses
  72. CCookie* pcookie = (CCookie*)m_pcookie;
  73. return s.Write(reinterpret_cast<PBYTE>(&pcookie), sizeof(m_pcookie));
  74. }
  75. else if (cf == m_CFSnapinPreloads)
  76. {
  77. stream_ptr s(pMedium);
  78. // If this is TRUE, then the next time this snapin is loaded, it will
  79. // be preloaded to give us the opportunity to change the root node
  80. // name before the user sees it.
  81. return s.Write (reinterpret_cast<PBYTE>(&m_fAllowOverrideMachineName), sizeof (BOOL));
  82. }
  83. #ifdef __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  84. else if (cf == g_cfServiceName)
  85. {
  86. CString str = _T("alerter");
  87. stream_ptr s(pMedium);
  88. return s.Write(str);
  89. }
  90. else if (cf == g_cfServiceDisplayName)
  91. {
  92. CString str = _T("Alerter!");
  93. stream_ptr s(pMedium);
  94. return s.Write(str);
  95. }
  96. #endif // __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  97. return DV_E_FORMATETC;
  98. MFC_CATCH;
  99. } // CMyComputerDataObject::GetDataHere()
  100. //#ifdef __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  101. /////////////////////////////////////////////////////////////////////
  102. // CMyComputerDataObject::IDataObject::GetData()
  103. //
  104. // Write data into the storage medium.
  105. // The data will be retrieved by the Send Console Message snapin.
  106. //
  107. // HISTORY
  108. // 12-Aug-97 t-danm Creation.
  109. //
  110. HRESULT
  111. CMyComputerDataObject::GetData(
  112. FORMATETC __RPC_FAR * pFormatEtcIn,
  113. STGMEDIUM __RPC_FAR * pMedium)
  114. {
  115. ASSERT(pFormatEtcIn != NULL);
  116. ASSERT(pMedium != NULL);
  117. const CLIPFORMAT cf = pFormatEtcIn->cfFormat;
  118. /*
  119. if (cf == g_cfSendConsoleMessageText)
  120. {
  121. //
  122. // Write the message text into the storage medium.
  123. // - The text must be in UINCODE.
  124. // - The text must terminate by a null character.
  125. // - Multiple lines can be made using the CRLF ("\n\r") pair.
  126. // - Allocated memory must include null character.
  127. //
  128. const WCHAR szMessage[] =
  129. L"The domain controller \\\\DANMORIN will crash in about 5 minutes.\r\nPlease disconnect now!";
  130. HGLOBAL hGlobal = GlobalAlloc(GMEM_FIXED, sizeof(szMessage));
  131. if (hGlobal == NULL)
  132. return E_OUTOFMEMORY;
  133. memcpy(OUT hGlobal, IN szMessage, sizeof(szMessage));
  134. pMedium->hGlobal = hGlobal;
  135. return S_OK;
  136. }
  137. */
  138. if (cf == m_cfSendConsoleMessageRecipients)
  139. {
  140. //
  141. // Write the list of recipients to the storage medium.
  142. // - The list of recipients is a group of UNICODE strings
  143. // terminated by TWO null characters.c
  144. // - Allocated memory must include BOTH null characters.
  145. //
  146. ASSERT (m_pcookie);
  147. if ( m_pcookie )
  148. {
  149. CString computerName = m_pcookie->QueryNonNULLMachineName ();
  150. if ( computerName.IsEmpty () )
  151. {
  152. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1 ;
  153. VERIFY (::GetComputerName (
  154. computerName.GetBufferSetLength (dwSize),
  155. &dwSize));
  156. computerName.ReleaseBuffer ();
  157. }
  158. size_t len = computerName.GetLength () + 2;
  159. WCHAR* pgrszRecipients = new WCHAR[len];
  160. if ( pgrszRecipients )
  161. {
  162. ::ZeroMemory (pgrszRecipients, len * sizeof (WCHAR));
  163. wcscpy (pgrszRecipients, (LPCTSTR) computerName); // this should leave two NULLS
  164. HGLOBAL hGlobal = ::GlobalAlloc (GMEM_FIXED, len * sizeof (WCHAR));
  165. if ( hGlobal )
  166. {
  167. memcpy (OUT hGlobal, pgrszRecipients, len * sizeof (WCHAR));
  168. pMedium->hGlobal = hGlobal;
  169. return S_OK;
  170. }
  171. else
  172. return E_OUTOFMEMORY;
  173. delete [] pgrszRecipients;
  174. }
  175. else
  176. return E_OUTOFMEMORY;
  177. }
  178. else
  179. return E_UNEXPECTED;
  180. /*
  181. const WCHAR grszRecipients[] = L"FooBar\0t-danm\0t-danm-2\0ZooBar\0";
  182. HGLOBAL hGlobal = GlobalAlloc(GMEM_FIXED, sizeof(grszRecipients));
  183. if (hGlobal == NULL)
  184. return E_OUTOFMEMORY;
  185. memcpy(OUT hGlobal, IN grszRecipients, sizeof(grszRecipients));
  186. pMedium->hGlobal = hGlobal;
  187. return S_OK;
  188. */
  189. }
  190. return DV_E_FORMATETC; // Invalid/unknown clipboard format
  191. } // CMyComputerDataObject::GetData()
  192. //#endif // __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  193. HRESULT CMyComputerDataObject::Initialize(
  194. CMyComputerCookie* pcookie,
  195. DATA_OBJECT_TYPES type,
  196. BOOL fAllowOverrideMachineName)
  197. {
  198. if (NULL == pcookie || NULL != m_pcookie)
  199. {
  200. ASSERT(FALSE);
  201. return E_UNEXPECTED;
  202. }
  203. m_dataobjecttype = type;
  204. m_pcookie = pcookie;
  205. m_fAllowOverrideMachineName = fAllowOverrideMachineName;
  206. ((CRefcountedObject*)m_pcookie)->AddRef();
  207. return S_OK;
  208. }
  209. CMyComputerDataObject::~CMyComputerDataObject()
  210. {
  211. if (NULL != m_pcookie)
  212. {
  213. ((CRefcountedObject*)m_pcookie)->Release();
  214. }
  215. else
  216. {
  217. ASSERT(FALSE);
  218. }
  219. }
  220. HRESULT CMyComputerDataObject::PutDisplayName(STGMEDIUM* pMedium)
  221. // Writes the "friendly name" to the provided storage medium
  222. // Returns the result of the write operation
  223. {
  224. CString strDisplayName = m_pcookie->QueryTargetServer();
  225. CString formattedName = FormatDisplayName (strDisplayName);
  226. stream_ptr s(pMedium);
  227. return s.Write(formattedName);
  228. }
  229. // Register the clipboard formats
  230. CLIPFORMAT CMyComputerDataObject::m_CFDisplayName =
  231. (CLIPFORMAT)RegisterClipboardFormat(CCF_DISPLAY_NAME);
  232. CLIPFORMAT CMyComputerDataObject::m_CFMachineName =
  233. (CLIPFORMAT)RegisterClipboardFormat(L"MMC_SNAPIN_MACHINE_NAME");
  234. CLIPFORMAT CDataObject::m_CFRawCookie =
  235. (CLIPFORMAT)RegisterClipboardFormat(L"MYCOMPUT_SNAPIN_RAW_COOKIE");
  236. STDMETHODIMP CMyComputerComponentData::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT* ppDataObject)
  237. {
  238. MFC_TRY;
  239. CMyComputerCookie* pUseThisCookie = (CMyComputerCookie*)ActiveBaseCookie(reinterpret_cast<CCookie*>(cookie));
  240. CComObject<CMyComputerDataObject>* pDataObject = NULL;
  241. HRESULT hRes = CComObject<CMyComputerDataObject>::CreateInstance(&pDataObject);
  242. if ( FAILED(hRes) )
  243. return hRes;
  244. HRESULT hr = pDataObject->Initialize( pUseThisCookie, type, m_fAllowOverrideMachineName);
  245. if ( SUCCEEDED(hr) )
  246. {
  247. hr = pDataObject->QueryInterface(IID_IDataObject,
  248. reinterpret_cast<void**>(ppDataObject));
  249. }
  250. if ( FAILED(hr) )
  251. {
  252. delete pDataObject;
  253. return hr;
  254. }
  255. return hr;
  256. MFC_CATCH;
  257. }