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.

137 lines
3.0 KiB

  1. // faxhelper.cpp : IComponent Interface helpers
  2. //
  3. // these functions help with extracting dataobjects and clipboards
  4. //
  5. // stolen from the MMC SDK.
  6. // Copyright (C) 1997 Microsoft Corporation
  7. // All rights reserved.
  8. //
  9. #include "stdafx.h"
  10. #include "faxhelper.h"
  11. #pragma hdrstop
  12. /////////////////////////////////////////////////////////////////////////////
  13. // We need a few functions to help work with dataobjects and clipboard formats
  14. CFaxDataObject *
  15. ExtractOwnDataObject(
  16. IN LPDATAOBJECT lpDataObject )
  17. /*++
  18. Routine Description:
  19. This routine extracts a CFaxDataObject from a IDataObject.
  20. Arguments:
  21. lpDataObject
  22. Return Value:
  23. CFaxDataObject * on success, NULL on failure.
  24. --*/
  25. {
  26. // DebugPrint(( TEXT("Trace: ::ExtractOwnDataObjecct") ));
  27. HRESULT hr = S_OK;
  28. HGLOBAL hGlobal;
  29. CFaxDataObject *pdo = NULL;
  30. if( lpDataObject == NULL ) {
  31. return NULL;
  32. }
  33. hr = ExtractFromDataObject(lpDataObject,
  34. (CLIPFORMAT)CFaxDataObject::s_cfInternal,
  35. sizeof(CFaxDataObject **),
  36. &hGlobal);
  37. if(SUCCEEDED(hr)) {
  38. pdo = *(CFaxDataObject **)(hGlobal);
  39. assert(pdo);
  40. assert(!GlobalFree(hGlobal));
  41. }
  42. return pdo;
  43. }
  44. HRESULT
  45. ExtractFromDataObject(
  46. IN LPDATAOBJECT lpDataObject,
  47. IN CLIPFORMAT cf,
  48. IN ULONG cb,
  49. OUT HGLOBAL *phGlobal )
  50. /*++
  51. Routine Description:
  52. Asks the IDataObject for the clipboard format specified in cf
  53. Arguments:
  54. lpDataObject - the data object passed in
  55. cf - the requested clipboard format
  56. cb - the requested number of bytes to allocate for the object
  57. phGlobal - the global handle to the newly allocated info
  58. Return Value:
  59. HRESULT indicating SUCCEEDED() or FAILED()
  60. --*/
  61. {
  62. // DebugPrint(( TEXT("Trace: ::ExtractFromDataObjecct") ));
  63. HRESULT hr = S_OK;
  64. STGMEDIUM stgmedium = { TYMED_HGLOBAL, NULL};
  65. FORMATETC formatetc = { cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  66. assert(lpDataObject != NULL);
  67. if( lpDataObject == NULL ) {
  68. return E_POINTER;
  69. }
  70. *phGlobal = NULL;
  71. do
  72. {
  73. // Allocate memory for the stream
  74. stgmedium.hGlobal = GlobalAlloc(GMEM_SHARE, cb);
  75. if(!stgmedium.hGlobal) {
  76. hr = E_OUTOFMEMORY;
  77. ATLTRACE(_T("Out of memory\n"));
  78. break;
  79. }
  80. // Attempt to get data from the object
  81. try {
  82. hr = lpDataObject->GetDataHere(&formatetc, &stgmedium);
  83. } catch( ... ) {
  84. // an exception happened!
  85. hr = E_UNEXPECTED;
  86. }
  87. if(FAILED(hr)) {
  88. break;
  89. }
  90. *phGlobal = stgmedium.hGlobal;
  91. stgmedium.hGlobal = NULL;
  92. } while(0);
  93. // if the call failed, and we allocated memory,
  94. // free the memory
  95. if(FAILED(hr) && stgmedium.hGlobal) {
  96. if(!GlobalFree(stgmedium.hGlobal)) {
  97. assert(FALSE);
  98. };
  99. }
  100. return hr;
  101. }