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.

135 lines
3.1 KiB

  1. #include "stock.h"
  2. #pragma hdrstop
  3. #ifndef UNICODE
  4. #define UNICODE
  5. #endif
  6. #include <dobjutil.h>
  7. STDAPI DataObj_SetBlob(IDataObject *pdtobj, UINT cf, LPCVOID pvBlob, UINT cbBlob)
  8. {
  9. HRESULT hr = E_OUTOFMEMORY;
  10. void *pv = GlobalAlloc(GPTR, cbBlob);
  11. if (pv)
  12. {
  13. CopyMemory(pv, pvBlob, cbBlob);
  14. hr = DataObj_SetGlobal(pdtobj, cf, pv);
  15. if (FAILED(hr))
  16. GlobalFree((HGLOBAL)pv);
  17. }
  18. return hr;
  19. }
  20. STDAPI DataObj_GetBlob(IDataObject *pdtobj, UINT cf, void *pvBlob, UINT cbBlob)
  21. {
  22. STGMEDIUM medium = {0};
  23. FORMATETC fmte = {(CLIPFORMAT) cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  24. HRESULT hr = pdtobj->GetData(&fmte, &medium);
  25. if (SUCCEEDED(hr))
  26. {
  27. void *pv = GlobalLock(medium.hGlobal);
  28. if (pv)
  29. {
  30. CopyMemory(pvBlob, pv, cbBlob);
  31. GlobalUnlock(medium.hGlobal);
  32. }
  33. else
  34. {
  35. hr = E_UNEXPECTED;
  36. }
  37. ReleaseStgMedium(&medium);
  38. }
  39. return hr;
  40. }
  41. STDAPI DataObj_SetDWORD(IDataObject *pdtobj, UINT cf, DWORD dw)
  42. {
  43. return DataObj_SetBlob(pdtobj, cf, &dw, sizeof(DWORD));
  44. }
  45. STDAPI_(DWORD) DataObj_GetDWORD(IDataObject *pdtobj, UINT cf, DWORD dwDefault)
  46. {
  47. DWORD dwRet;
  48. if (FAILED(DataObj_GetBlob(pdtobj, cf, &dwRet, sizeof(DWORD))))
  49. dwRet = dwDefault;
  50. return dwRet;
  51. }
  52. STDAPI DataObj_SetGlobal(IDataObject *pdtobj, UINT cf, HGLOBAL hGlobal)
  53. {
  54. FORMATETC fmte = {(CLIPFORMAT) cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  55. STGMEDIUM medium = {0};
  56. medium.tymed = TYMED_HGLOBAL;
  57. medium.hGlobal = hGlobal;
  58. medium.pUnkForRelease = NULL;
  59. // give the data object ownership of ths
  60. return pdtobj->SetData(&fmte, &medium, TRUE);
  61. }
  62. STDAPI_(LPIDA) DataObj_GetHIDAEx(IDataObject *pdtobj, CLIPFORMAT cf, STGMEDIUM *pmedium)
  63. {
  64. FORMATETC fmte = {cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  65. if (pmedium)
  66. {
  67. pmedium->pUnkForRelease = NULL;
  68. pmedium->hGlobal = NULL;
  69. }
  70. if (!pmedium)
  71. {
  72. if (S_OK == pdtobj->QueryGetData(&fmte))
  73. return (LPIDA)TRUE;
  74. else
  75. return (LPIDA)FALSE;
  76. }
  77. else if (SUCCEEDED(pdtobj->GetData(&fmte, pmedium)))
  78. {
  79. return (LPIDA)GlobalLock(pmedium->hGlobal);
  80. }
  81. return NULL;
  82. }
  83. STDAPI_(LPIDA) DataObj_GetHIDA(IDataObject *pdtobj, STGMEDIUM *pmedium)
  84. {
  85. static CLIPFORMAT cfHIDA = 0;
  86. if (!cfHIDA)
  87. {
  88. cfHIDA = (CLIPFORMAT)RegisterClipboardFormat(CFSTR_SHELLIDLIST);
  89. }
  90. return DataObj_GetHIDAEx(pdtobj, cfHIDA, pmedium);
  91. }
  92. STDAPI_(void) ReleaseStgMediumHGLOBAL(void *pv, STGMEDIUM *pmedium)
  93. {
  94. if (pmedium->hGlobal && (pmedium->tymed == TYMED_HGLOBAL))
  95. {
  96. #ifdef DEBUG
  97. if (pv)
  98. {
  99. void *pvT = (void *)GlobalLock(pmedium->hGlobal);
  100. ASSERT(pvT == pv);
  101. GlobalUnlock(pmedium->hGlobal);
  102. }
  103. #endif
  104. GlobalUnlock(pmedium->hGlobal);
  105. }
  106. else
  107. {
  108. ASSERT(0);
  109. }
  110. ReleaseStgMedium(pmedium);
  111. }
  112. STDAPI_(void) HIDA_ReleaseStgMedium(LPIDA pida, STGMEDIUM * pmedium)
  113. {
  114. ReleaseStgMediumHGLOBAL((void *)pida, pmedium);
  115. }