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.

325 lines
9.5 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: guidhelp.cpp
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 9/18/1996 JonN Created
  15. //
  16. //____________________________________________________________________________
  17. #include <objbase.h>
  18. #include <basetyps.h>
  19. #include "dbg.h"
  20. #include "cstr.h"
  21. #ifndef DECLSPEC_UUID
  22. #if _MSC_VER >= 1100
  23. #define DECLSPEC_UUID(x) __declspec(uuid(x))
  24. #else
  25. #define DECLSPEC_UUID(x)
  26. #endif
  27. #endif
  28. #include <mmc.h>
  29. #include "guidhelp.h"
  30. #include "atlbase.h" // USES_CONVERSION
  31. #include "macros.h"
  32. USE_HANDLE_MACROS("GUIDHELP(guidhelp.cpp)")
  33. #ifdef _DEBUG
  34. #define new DEBUG_NEW
  35. #undef THIS_FILE
  36. static char THIS_FILE[] = __FILE__;
  37. #endif
  38. static CLIPFORMAT g_CFNodeType = 0;
  39. static CLIPFORMAT g_CFSnapInCLSID = 0;
  40. static CLIPFORMAT g_CFDisplayName = 0;
  41. HRESULT ExtractData( IDataObject* piDataObject,
  42. CLIPFORMAT cfClipFormat,
  43. PVOID pbData,
  44. DWORD cbData )
  45. {
  46. TEST_NONNULL_PTR_PARAM( piDataObject );
  47. TEST_NONNULL_PTR_PARAM( pbData );
  48. HRESULT hr = S_OK;
  49. FORMATETC formatetc = {cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  50. STGMEDIUM stgmedium = {TYMED_HGLOBAL, NULL};
  51. stgmedium.hGlobal = ::GlobalAlloc(GPTR, cbData);
  52. do // false loop
  53. {
  54. if (NULL == stgmedium.hGlobal)
  55. {
  56. ASSERT(FALSE);
  57. ////AfxThrowMemoryException();
  58. hr = E_OUTOFMEMORY;
  59. break;
  60. }
  61. hr = piDataObject->GetDataHere( &formatetc, &stgmedium );
  62. if ( FAILED(hr) )
  63. {
  64. // JonN Jan 7 1999: don't assert here, some errors are perfectly reasonable
  65. break;
  66. }
  67. PVOID pbNewData = reinterpret_cast<PVOID>(stgmedium.hGlobal);
  68. if (NULL == pbNewData)
  69. {
  70. ASSERT(FALSE);
  71. hr = E_UNEXPECTED;
  72. break;
  73. }
  74. ::memcpy( pbData, pbNewData, cbData );
  75. } while (FALSE); // false loop
  76. if (NULL != stgmedium.hGlobal)
  77. {
  78. //VERIFY( stgmedium.hGlobal);
  79. VERIFY( NULL == ::GlobalFree(stgmedium.hGlobal) );
  80. }
  81. return hr;
  82. } // ExtractData()
  83. HRESULT ExtractString( IDataObject* piDataObject,
  84. CLIPFORMAT cfClipFormat,
  85. CStr* pstr, // OUT: Pointer to CStr to store data
  86. DWORD cchMaxLength)
  87. {
  88. TEST_NONNULL_PTR_PARAM( piDataObject );
  89. TEST_NONNULL_PTR_PARAM( pstr );
  90. ASSERT( cchMaxLength > 0 );
  91. HRESULT hr = S_OK;
  92. FORMATETC formatetc = {cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  93. STGMEDIUM stgmedium = {TYMED_HGLOBAL, NULL};
  94. stgmedium.hGlobal = ::GlobalAlloc(GPTR, sizeof(WCHAR)*cchMaxLength);
  95. do // false loop
  96. {
  97. if (NULL == stgmedium.hGlobal)
  98. {
  99. ASSERT(FALSE);
  100. ////AfxThrowMemoryException();
  101. hr = E_OUTOFMEMORY;
  102. break;
  103. }
  104. hr = piDataObject->GetDataHere( &formatetc, &stgmedium );
  105. if ( FAILED(hr) )
  106. {
  107. // This failure happens when 'searching' for
  108. // clipboard format supported by the IDataObject.
  109. // t-danmo (24-Oct-96)
  110. // Skipping ASSERT( FALSE );
  111. break;
  112. }
  113. LPWSTR pszNewData = reinterpret_cast<LPWSTR>(stgmedium.hGlobal);
  114. if (NULL == pszNewData)
  115. {
  116. ASSERT(FALSE);
  117. hr = E_UNEXPECTED;
  118. break;
  119. }
  120. pszNewData[cchMaxLength-1] = L'\0'; // just to be safe
  121. USES_CONVERSION;
  122. *pstr = OLE2T(pszNewData);
  123. } while (FALSE); // false loop
  124. if (NULL != stgmedium.hGlobal)
  125. {
  126. VERIFY(NULL == ::GlobalFree(stgmedium.hGlobal));
  127. }
  128. return hr;
  129. } // ExtractString()
  130. HRESULT ExtractSnapInCLSID( IDataObject* piDataObject, CLSID* pclsidSnapin )
  131. {
  132. if( !g_CFSnapInCLSID )
  133. {
  134. USES_CONVERSION;
  135. g_CFSnapInCLSID = (CLIPFORMAT)RegisterClipboardFormat(W2T(CCF_SNAPIN_CLASSID));
  136. // ISSUE-2002/04/01-JonN ASSERT(NULL != g_CFSnapInCLSID)
  137. }
  138. return ExtractData( piDataObject, g_CFSnapInCLSID, (PVOID)pclsidSnapin, sizeof(CLSID) );
  139. }
  140. HRESULT ExtractObjectTypeGUID( IDataObject* piDataObject, GUID* pguidObjectType )
  141. {
  142. if( !g_CFNodeType )
  143. {
  144. USES_CONVERSION;
  145. g_CFNodeType = (CLIPFORMAT)RegisterClipboardFormat(W2T(CCF_NODETYPE));
  146. // ISSUE-2002/04/01-JonN ASSERT(NULL != g_CFSnapInCLSID)
  147. }
  148. return ExtractData( piDataObject, g_CFNodeType, (PVOID)pguidObjectType, sizeof(GUID) );
  149. }
  150. HRESULT GuidToCStr( CStr* pstr, const GUID& guid )
  151. {
  152. WCHAR awch[MAX_PATH];
  153. // ISSUE-2002/04/01-JonN call ZeroMemory
  154. HRESULT hr = StringFromGUID2(guid, awch, MAX_PATH); // JonN 11/21/00 PREFIX 226769
  155. ASSERT(SUCCEEDED(hr));
  156. USES_CONVERSION;
  157. LPTSTR lptstr = OLE2T(awch);
  158. *pstr = lptstr;
  159. return hr;
  160. }
  161. HRESULT CStrToGuid( const CStr& str, GUID* pguid )
  162. {
  163. USES_CONVERSION;
  164. LPOLESTR lpolestr = T2OLE(((LPTSTR)(LPCTSTR)str));
  165. HRESULT hr = CLSIDFromString(lpolestr, pguid);
  166. ASSERT(SUCCEEDED(hr));
  167. return hr;
  168. }
  169. #if 0
  170. HRESULT bstrToGuid( const bstr& str, GUID* pguid )
  171. {
  172. HRESULT hr = CLSIDFromString(const_cast<LPOLESTR>((LPCWSTR)str), pguid);
  173. ASSERT(SUCCEEDED(hr));
  174. return hr;
  175. }
  176. #endif
  177. HRESULT LoadRootDisplayName(IComponentData* pIComponentData,
  178. CStr& strDisplayName)
  179. {
  180. // ISSUE-2002/04/01-JonN test pIComponentData
  181. IDataObject* pIDataObject = NULL;
  182. HRESULT hr = pIComponentData->QueryDataObject(NULL, CCT_SNAPIN_MANAGER, &pIDataObject);
  183. CHECK_HRESULT(hr);
  184. if ( FAILED(hr) )
  185. return hr;
  186. if( !g_CFDisplayName )
  187. {
  188. USES_CONVERSION;
  189. g_CFDisplayName = (CLIPFORMAT)RegisterClipboardFormat(W2T(CCF_DISPLAY_NAME));
  190. // ISSUE-2002/04/01-JonN ASSERT(NULL != g_CFSnapInCLSID)
  191. }
  192. hr = ExtractString( pIDataObject,
  193. g_CFDisplayName,
  194. &strDisplayName,
  195. MAX_PATH); // CODEWORK maximum length
  196. CHECK_HRESULT(hr);
  197. if (pIDataObject) pIDataObject->Release(); // JonN 3/28/02
  198. return hr;
  199. }
  200. HRESULT LoadAndAddMenuItem(
  201. IContextMenuCallback* pIContextMenuCallback,
  202. UINT nResourceID, // contains text and status text seperated by '\n'
  203. long lCommandID,
  204. long lInsertionPointID,
  205. long fFlags,
  206. HINSTANCE hInst,
  207. PCTSTR pszLanguageIndependentName)
  208. {
  209. // ISSUE-2002/04/01-JonN handle these cases
  210. ASSERT( pIContextMenuCallback != NULL );
  211. ASSERT( pszLanguageIndependentName != NULL );
  212. CComPtr<IContextMenuCallback2> spiCallback2;
  213. HRESULT hr = pIContextMenuCallback->QueryInterface(IID_IContextMenuCallback2, (void **)&spiCallback2);
  214. if (FAILED(hr))
  215. return hr;
  216. // load the resource
  217. CStr strText;
  218. strText.LoadString(hInst, nResourceID );
  219. ASSERT( !strText.IsEmpty() );
  220. // split the resource into the menu text and status text
  221. CStr strStatusText;
  222. int iSeparator = strText.Find(_T('\n'));
  223. if (0 > iSeparator)
  224. {
  225. ASSERT( FALSE );
  226. strStatusText = strText;
  227. }
  228. else
  229. {
  230. strStatusText = strText.Right( strText.GetLength()-(iSeparator+1) );
  231. strText = strText.Left( iSeparator );
  232. }
  233. // add the menu item
  234. USES_CONVERSION;
  235. CONTEXTMENUITEM2 contextmenuitem;
  236. ::ZeroMemory( &contextmenuitem, sizeof(contextmenuitem) );
  237. contextmenuitem.strName = T2OLE(const_cast<LPTSTR>((LPCTSTR)strText));
  238. contextmenuitem.strStatusBarText = T2OLE(const_cast<LPTSTR>((LPCTSTR)strStatusText));
  239. contextmenuitem.lCommandID = lCommandID;
  240. contextmenuitem.lInsertionPointID = lInsertionPointID;
  241. contextmenuitem.fFlags = fFlags;
  242. contextmenuitem.fSpecialFlags = ((fFlags & MF_POPUP) ? CCM_SPECIAL_SUBMENU : 0L);
  243. contextmenuitem.strLanguageIndependentName = T2OLE(const_cast<LPTSTR>(pszLanguageIndependentName));
  244. hr = spiCallback2->AddItem( &contextmenuitem );
  245. ASSERT(hr == S_OK);
  246. return hr;
  247. }
  248. HRESULT AddSpecialSeparator(
  249. IContextMenuCallback* pIContextMenuCallback,
  250. long lInsertionPointID )
  251. {
  252. CONTEXTMENUITEM contextmenuitem;
  253. ::ZeroMemory( &contextmenuitem, sizeof(contextmenuitem) );
  254. contextmenuitem.strName = NULL;
  255. contextmenuitem.strStatusBarText = NULL;
  256. contextmenuitem.lCommandID = 0;
  257. contextmenuitem.lInsertionPointID = lInsertionPointID;
  258. contextmenuitem.fFlags = MF_SEPARATOR;
  259. contextmenuitem.fSpecialFlags = CCM_SPECIAL_SEPARATOR;
  260. HRESULT hr = pIContextMenuCallback->AddItem( &contextmenuitem );
  261. ASSERT(hr == S_OK);
  262. return hr;
  263. }
  264. HRESULT AddSpecialInsertionPoint(
  265. IContextMenuCallback* pIContextMenuCallback,
  266. long lCommandID,
  267. long lInsertionPointID )
  268. {
  269. // ISSUE-2002/04/01-JonN handle NULL
  270. CONTEXTMENUITEM contextmenuitem;
  271. ::ZeroMemory( &contextmenuitem, sizeof(contextmenuitem) );
  272. contextmenuitem.strName = NULL;
  273. contextmenuitem.strStatusBarText = NULL;
  274. contextmenuitem.lCommandID = lCommandID;
  275. contextmenuitem.lInsertionPointID = lInsertionPointID;
  276. contextmenuitem.fFlags = 0;
  277. contextmenuitem.fSpecialFlags = CCM_SPECIAL_INSERTION_POINT;
  278. HRESULT hr = pIContextMenuCallback->AddItem( &contextmenuitem );
  279. ASSERT(hr == S_OK);
  280. return hr;
  281. }