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.

276 lines
7.7 KiB

  1. /*======================================================================================//
  2. | Process Control //
  3. | //
  4. |Copyright (c) 1998 Sequent Computer Systems, Incorporated. All rights reserved. //
  5. | //
  6. |File Name: Globals.cpp //
  7. | //
  8. |Description: //
  9. | //
  10. |Created: Paul Skoglund 07-1998 //
  11. | //
  12. |Rev History: //
  13. | //
  14. |=======================================================================================*/
  15. #include "StdAfx.h"
  16. #include "mmc.h"
  17. #include "Globals.h"
  18. #include "DataObj.h"
  19. #include "BaseNode.h"
  20. PROPERTY_CHANGE_HDR * AllocPropChangeInfo(CBaseNode *pFolder, int nHint, COMPUTER_CONNECTION_INFO &Target, BOOL bScopeItem, int nPageRef)
  21. {
  22. PROPERTY_CHANGE_HDR *ptr = (PROPERTY_CHANGE_HDR *) CoTaskMemAlloc(sizeof(PROPERTY_CHANGE_HDR));
  23. if (ptr)
  24. {
  25. ptr->pFolder = pFolder;
  26. ptr->nHint = nHint;
  27. ptr->Target = Target;
  28. ptr->bScopeItem = bScopeItem;
  29. ptr->nPageRef = nPageRef;
  30. }
  31. return ptr;
  32. }
  33. PROPERTY_CHANGE_HDR *FreePropChangeInfo(PROPERTY_CHANGE_HDR *pInfo)
  34. {
  35. if (pInfo)
  36. {
  37. CoTaskMemFree(pInfo);
  38. }
  39. return NULL;
  40. }
  41. const TCHAR *LoadStringHelper(ITEM_STR strOut, int id)
  42. {
  43. strOut[0] = 0;
  44. VERIFY( LoadString(_Module.GetResourceInstance(), id, strOut, MAX_ITEM_LEN) );
  45. return strOut;
  46. }
  47. HRESULT UpdateRegistryHelper(int Id, BOOL bRegister)
  48. {
  49. TCHAR SnapinNameStr[MAX_ITEM_LEN];
  50. const TCHAR *SnapinName;
  51. struct _ATL_REGMAP_ENTRY MapEntries[] = {
  52. { _T("SNAPIN_NAME"), _T("Process Control") },
  53. { NULL, NULL }
  54. };
  55. SnapinName = LoadStringHelper(SnapinNameStr, IDS_PROCCON_SNAPIN_NAME);
  56. if (SnapinName) {
  57. MapEntries[0].szData = SnapinName;
  58. }
  59. return _Module.UpdateRegistryFromResource(Id, bRegister, MapEntries);
  60. }
  61. //---------------------------------------------------------------------------
  62. // Returns the current object based on the s_cfInternal clipboard format
  63. //
  64. CBaseNode* ExtractBaseObject
  65. (
  66. LPDATAOBJECT ipDataObject // [in] IComponent pointer
  67. )
  68. {
  69. HGLOBAL hGlobal;
  70. HRESULT hr = S_OK;
  71. CBaseNode *pNode = NULL;
  72. hr = ExtractFromDataObject( ipDataObject,
  73. CDataObject::s_cfBaseInternal,
  74. sizeof(CBaseNode **),
  75. &hGlobal
  76. );
  77. if( SUCCEEDED(hr) )
  78. {
  79. pNode = *(CBaseNode **)(hGlobal);
  80. ASSERT( NULL != pNode );
  81. HGLOBAL hRetVal = GlobalFree(hGlobal);
  82. ASSERT( NULL == hRetVal ); // Must return NULL
  83. }
  84. return pNode;
  85. } // end ExtractBaseObject()
  86. CDataObject* ExtractOwnDataObject
  87. (
  88. LPDATAOBJECT ipDataObject // [in] IComponent pointer
  89. )
  90. {
  91. HGLOBAL hGlobal;
  92. HRESULT hr = S_OK;
  93. CDataObject* pDO = NULL;
  94. hr = ExtractFromDataObject( ipDataObject,
  95. CDataObject::s_cfInternal,
  96. sizeof(CDataObject **),
  97. &hGlobal
  98. );
  99. if( SUCCEEDED(hr) )
  100. {
  101. pDO = *(CDataObject **)(hGlobal);
  102. ASSERT( NULL != pDO );
  103. HGLOBAL hRetVal = GlobalFree(hGlobal);
  104. ASSERT( NULL == hRetVal ); // Must return NULL
  105. }
  106. return pDO;
  107. } // end ExtractOwnDataObject()
  108. //---------------------------------------------------------------------------
  109. // Extracts data based on the passed-in clipboard format
  110. //
  111. HRESULT ExtractFromDataObject
  112. (
  113. LPDATAOBJECT ipDataObject, // [in] Points to data object
  114. UINT cfClipFormat, // [in] Clipboard format to use
  115. SIZE_T nByteCount, // [in] Number of bytes to allocate
  116. HGLOBAL *phGlobal // [out] Points to the data we want
  117. )
  118. {
  119. HRESULT hr = S_OK;
  120. STGMEDIUM stgmedium = { TYMED_HGLOBAL, NULL };
  121. FORMATETC formatetc = { (CLIPFORMAT) cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  122. *phGlobal = NULL;
  123. ASSERT( NULL != ipDataObject );
  124. if (!ipDataObject)
  125. return E_INVALIDARG;
  126. do
  127. {
  128. // Allocate memory for the stream
  129. stgmedium.hGlobal = GlobalAlloc( GMEM_SHARE, nByteCount );
  130. if( !stgmedium.hGlobal )
  131. {
  132. hr = E_OUTOFMEMORY;
  133. ATLTRACE( _T("Out of memory\n") );
  134. break;
  135. }
  136. // Attempt to get data from the object
  137. hr = ipDataObject->GetDataHere( &formatetc, &stgmedium );
  138. if (FAILED(hr))
  139. {
  140. break;
  141. }
  142. // stgmedium now has the data we need
  143. *phGlobal = stgmedium.hGlobal;
  144. stgmedium.hGlobal = NULL;
  145. } while (0);
  146. if (FAILED(hr) && stgmedium.hGlobal)
  147. {
  148. HGLOBAL hRetVal = GlobalFree(stgmedium.hGlobal);
  149. ASSERT( NULL == hRetVal ); // Must return NULL
  150. }
  151. return hr;
  152. } // end ExtractFromDataObject()
  153. static UINT s_cfMultiSelect = ::RegisterClipboardFormat(CCF_MMC_MULTISELECT_DATAOBJECT);
  154. BOOL IsMMCMultiSelectDataObject(IDataObject* pDataObject)
  155. {
  156. if (pDataObject == NULL)
  157. return FALSE;
  158. ASSERT(s_cfMultiSelect != 0);
  159. FORMATETC fmt = { (CLIPFORMAT) s_cfMultiSelect, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  160. return (pDataObject->QueryGetData(&fmt) == S_OK);
  161. }
  162. // Caller must release memory, by calling LocalFree on the returned pointer
  163. TCHAR *FormatErrorMessageIntoBuffer(DWORD nLastError)
  164. {
  165. TCHAR *pLastErrorText = NULL;
  166. UINT_PTR vars[2];
  167. FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
  168. NULL, nLastError,
  169. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  170. (TCHAR *)&pLastErrorText, 0, NULL );
  171. if ( !pLastErrorText )
  172. {
  173. FormatMessage( FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,
  174. _Module.GetResourceInstance(), nLastError,
  175. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  176. (TCHAR *)&pLastErrorText, 0, NULL );
  177. }
  178. // $$ if string was returned consider striping trailing line feed and/or period?
  179. if (pLastErrorText)
  180. vars[0] = (UINT_PTR) pLastErrorText;
  181. else
  182. vars[0] = (UINT_PTR) _T("");
  183. vars[1] = (UINT_PTR) (LONG_PTR) nLastError;
  184. TCHAR *pMsgBuf = NULL;
  185. FormatMessage(
  186. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  187. _Module.GetResourceInstance(),
  188. MSG_OPERATION_FAILED_WITHCODES,
  189. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  190. (TCHAR *) &pMsgBuf,
  191. 0, // minimum buffer allocation
  192. (va_list *) &vars // message inserts
  193. );
  194. LocalFree( pLastErrorText );
  195. if (!pMsgBuf)
  196. {
  197. ASSERT(pMsgBuf);
  198. pMsgBuf = (TCHAR *) LocalAlloc(0, 128);
  199. if (pMsgBuf)
  200. _stprintf( pMsgBuf, TEXT("Error 0x%lx. A description of the error was not located."), nLastError ); // Do NOT localize -- last resort.
  201. }
  202. return pMsgBuf;
  203. }
  204. BOOL ReportPCError(DWORD nLastError, HWND hwnd)
  205. {
  206. TCHAR *pMsgBuf = FormatErrorMessageIntoBuffer(nLastError);
  207. if ( pMsgBuf )
  208. {
  209. ATLTRACE( (TCHAR *) pMsgBuf );
  210. ATLTRACE( _T("\n") );
  211. int ret = ::MessageBox(hwnd, pMsgBuf, NULL, MB_OK | MB_ICONWARNING);
  212. LocalFree(pMsgBuf);
  213. return TRUE;
  214. }
  215. return FALSE;
  216. }