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.

311 lines
9.9 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. // ISSUE-2002/02/27-JonN test for NULL pointers
  32. const CLIPFORMAT cf=pFormatEtcIn->cfFormat;
  33. if (cf == m_CFNodeType)
  34. {
  35. const GUID* pguid = GetObjectTypeGUID( m_pcookie->m_objecttype );
  36. stream_ptr s(pMedium);
  37. return s.Write(pguid, sizeof(GUID));
  38. }
  39. else if (cf == m_CFSnapInCLSID)
  40. {
  41. const GUID* pguid = &CLSID_MyComputer;
  42. stream_ptr s(pMedium);
  43. return s.Write(pguid, sizeof(GUID));
  44. }
  45. else if (cf == m_CFNodeTypeString)
  46. {
  47. const BSTR strGUID = GetObjectTypeString( m_pcookie->m_objecttype );
  48. stream_ptr s(pMedium);
  49. return s.Write(strGUID);
  50. }
  51. else if (cf == m_CFDisplayName)
  52. {
  53. return PutDisplayName(pMedium);
  54. }
  55. else if (cf == m_CFDataObjectType)
  56. {
  57. stream_ptr s(pMedium);
  58. return s.Write(&m_dataobjecttype, sizeof(m_dataobjecttype));
  59. }
  60. else if (cf == m_CFMachineName)
  61. {
  62. stream_ptr s(pMedium);
  63. LPCWSTR pszMachineName = m_pcookie->QueryNonNULLMachineName();
  64. if (IsBadStringPtr(pszMachineName,(UINT_PTR)-1)) // JonN 2/6/02 Security Push
  65. {
  66. ASSERT(FALSE);
  67. return E_FAIL;
  68. }
  69. if ( !wcsncmp (pszMachineName, L"\\\\", 2) )
  70. pszMachineName += 2; // skip whackwhack
  71. return s.Write(pszMachineName);
  72. }
  73. else if (cf == m_CFRawCookie)
  74. {
  75. stream_ptr s(pMedium);
  76. // CODEWORK This cast ensures that the data format is
  77. // always a CCookie*, even for derived subclasses
  78. CCookie* pcookie = (CCookie*)m_pcookie;
  79. return s.Write(reinterpret_cast<PBYTE>(&pcookie), sizeof(m_pcookie));
  80. }
  81. else if (cf == m_CFSnapinPreloads)
  82. {
  83. stream_ptr s(pMedium);
  84. // If this is TRUE, then the next time this snapin is loaded, it will
  85. // be preloaded to give us the opportunity to change the root node
  86. // name before the user sees it.
  87. return s.Write (reinterpret_cast<PBYTE>(&m_fAllowOverrideMachineName), sizeof (BOOL));
  88. }
  89. return DV_E_FORMATETC;
  90. MFC_CATCH;
  91. } // CMyComputerDataObject::GetDataHere()
  92. /////////////////////////////////////////////////////////////////////
  93. // CMyComputerDataObject::IDataObject::GetData()
  94. //
  95. // Write data into the storage medium.
  96. // The data will be retrieved by the Send Console Message snapin.
  97. //
  98. // HISTORY
  99. // 12-Aug-97 t-danm Creation.
  100. //
  101. HRESULT
  102. CMyComputerDataObject::GetData(
  103. FORMATETC __RPC_FAR * pFormatEtcIn,
  104. STGMEDIUM __RPC_FAR * pMedium)
  105. {
  106. // ISSUE-2002-02-27-JonN Should use MFC_TRY/MFC_CATCH
  107. // JonN 2/20/02 Security Push
  108. if (NULL == pFormatEtcIn || NULL == pMedium)
  109. {
  110. ASSERT(FALSE);
  111. return E_POINTER;
  112. }
  113. HRESULT hr = S_OK;
  114. const CLIPFORMAT cf = pFormatEtcIn->cfFormat;
  115. if (cf == m_cfSendConsoleMessageRecipients)
  116. {
  117. //
  118. // Write the list of recipients to the storage medium.
  119. // - The list of recipients is a group of UNICODE strings
  120. // terminated by TWO null characters.c
  121. // - Allocated memory must include BOTH null characters.
  122. //
  123. ASSERT (m_pcookie);
  124. if ( m_pcookie )
  125. {
  126. CString computerName = m_pcookie->QueryNonNULLMachineName ();
  127. if ( computerName.IsEmpty () )
  128. {
  129. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1 ;
  130. VERIFY (::GetComputerName (
  131. computerName.GetBufferSetLength (dwSize),
  132. &dwSize));
  133. computerName.ReleaseBuffer ();
  134. }
  135. size_t cch = computerName.GetLength () + 2;
  136. HGLOBAL hGlobal = ::GlobalAlloc (GMEM_FIXED | GMEM_ZEROINIT,
  137. cch * sizeof (WCHAR));
  138. if ( hGlobal )
  139. {
  140. // JonN 2/6/02 Security Push: memcpy -> StringCchCopy
  141. // this should leave two NULLS
  142. StringCchCopyW ((LPWSTR)hGlobal, cch, (LPCWSTR)computerName);
  143. pMedium->hGlobal = hGlobal;
  144. return S_OK;
  145. }
  146. else
  147. return E_OUTOFMEMORY;
  148. }
  149. else
  150. return E_UNEXPECTED;
  151. } else if (cf == m_CFNodeID2)
  152. {
  153. const LPCTSTR strGUID = GetObjectTypeString( m_pcookie->m_objecttype );
  154. // JonN 12/11/01 502856
  155. int cbString = (lstrlen(strGUID) + 1) * sizeof(TCHAR);
  156. pMedium->tymed = TYMED_HGLOBAL;
  157. pMedium->hGlobal = ::GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE,
  158. sizeof(SNodeID2) + cbString);
  159. if (!(pMedium->hGlobal))
  160. {
  161. hr = E_OUTOFMEMORY;
  162. } else
  163. {
  164. SNodeID2 *pNodeID = (SNodeID2 *)GlobalLock(pMedium->hGlobal);
  165. if (!pNodeID)
  166. {
  167. hr = HRESULT_FROM_WIN32(GetLastError());
  168. GlobalFree(pMedium->hGlobal);
  169. pMedium->hGlobal = NULL;
  170. } else
  171. {
  172. pNodeID->dwFlags = 0;
  173. pNodeID->cBytes = cbString;
  174. // JonN 2/6/02 Security Push: function usage approved
  175. CopyMemory(pNodeID->id, strGUID, cbString );
  176. GlobalUnlock(pMedium->hGlobal);
  177. }
  178. }
  179. } else
  180. {
  181. hr = DV_E_FORMATETC;
  182. }
  183. return hr;
  184. } // CMyComputerDataObject::GetData()
  185. //#endif // __DAN_MORIN_HARDCODED_CONTEXT_MENU_EXTENSION__
  186. HRESULT CMyComputerDataObject::Initialize(
  187. CMyComputerCookie* pcookie,
  188. DATA_OBJECT_TYPES type,
  189. BOOL fAllowOverrideMachineName)
  190. {
  191. // ISSUE-2002-/02/27-JonN check "type" parameter
  192. if (NULL == pcookie || NULL != m_pcookie)
  193. {
  194. ASSERT(FALSE);
  195. return E_UNEXPECTED;
  196. }
  197. m_dataobjecttype = type;
  198. m_pcookie = pcookie;
  199. m_fAllowOverrideMachineName = fAllowOverrideMachineName;
  200. ((CRefcountedObject*)m_pcookie)->AddRef();
  201. return S_OK;
  202. }
  203. CMyComputerDataObject::~CMyComputerDataObject()
  204. {
  205. if (NULL != m_pcookie)
  206. {
  207. ((CRefcountedObject*)m_pcookie)->Release();
  208. }
  209. else
  210. {
  211. ASSERT(FALSE);
  212. }
  213. }
  214. HRESULT CMyComputerDataObject::PutDisplayName(STGMEDIUM* pMedium)
  215. // Writes the "friendly name" to the provided storage medium
  216. // Returns the result of the write operation
  217. {
  218. // JonN 2/20/02 Security Push
  219. if (NULL == pMedium)
  220. {
  221. ASSERT(FALSE);
  222. return E_POINTER;
  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_CFNodeID2 =
  233. (CLIPFORMAT)RegisterClipboardFormat(CCF_NODEID2);
  234. CLIPFORMAT CMyComputerDataObject::m_CFMachineName =
  235. (CLIPFORMAT)RegisterClipboardFormat(L"MMC_SNAPIN_MACHINE_NAME");
  236. CLIPFORMAT CDataObject::m_CFRawCookie =
  237. (CLIPFORMAT)RegisterClipboardFormat(L"MYCOMPUT_SNAPIN_RAW_COOKIE");
  238. STDMETHODIMP CMyComputerComponentData::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT* ppDataObject)
  239. {
  240. MFC_TRY;
  241. // ISSUE-2002/02/27-JonN This would be more efficient if an instance of
  242. // CMyComputerDataObject were permanently attached to CMyComputerCookie,
  243. // or better yet, if they were the same object. QueryDataObject gets
  244. // called a lot...
  245. CMyComputerCookie* pUseThisCookie = (CMyComputerCookie*)ActiveBaseCookie(reinterpret_cast<CCookie*>(cookie));
  246. // JonN 2/20/02 Security Push
  247. if (NULL == pUseThisCookie)
  248. {
  249. ASSERT(FALSE);
  250. return E_POINTER;
  251. }
  252. CComObject<CMyComputerDataObject>* pDataObject = NULL;
  253. HRESULT hRes = CComObject<CMyComputerDataObject>::CreateInstance(&pDataObject);
  254. if ( FAILED(hRes) )
  255. return hRes;
  256. HRESULT hr = pDataObject->Initialize( pUseThisCookie, type, m_fAllowOverrideMachineName);
  257. if ( SUCCEEDED(hr) )
  258. {
  259. hr = pDataObject->QueryInterface(IID_IDataObject,
  260. reinterpret_cast<void**>(ppDataObject));
  261. }
  262. if ( FAILED(hr) )
  263. {
  264. delete pDataObject;
  265. return hr;
  266. }
  267. return hr;
  268. MFC_CATCH;
  269. }