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.

143 lines
3.5 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. SIZE_T cb = GlobalSize(medium.hGlobal);
  31. if (cb < cbBlob)
  32. {
  33. RIP(!"Corrupted IDataObject - HGLOBAL too small");
  34. hr = E_UNEXPECTED;
  35. }
  36. else
  37. {
  38. CopyMemory(pvBlob, pv, cbBlob);
  39. }
  40. GlobalUnlock(medium.hGlobal);
  41. }
  42. else
  43. {
  44. hr = E_UNEXPECTED;
  45. }
  46. ReleaseStgMedium(&medium);
  47. }
  48. return hr;
  49. }
  50. STDAPI DataObj_SetDWORD(IDataObject *pdtobj, UINT cf, DWORD dw)
  51. {
  52. return DataObj_SetBlob(pdtobj, cf, &dw, sizeof(DWORD));
  53. }
  54. STDAPI_(DWORD) DataObj_GetDWORD(IDataObject *pdtobj, UINT cf, DWORD dwDefault)
  55. {
  56. DWORD dwRet;
  57. if (FAILED(DataObj_GetBlob(pdtobj, cf, &dwRet, sizeof(DWORD))))
  58. dwRet = dwDefault;
  59. return dwRet;
  60. }
  61. STDAPI DataObj_SetGlobal(IDataObject *pdtobj, UINT cf, HGLOBAL hGlobal)
  62. {
  63. FORMATETC fmte = {(CLIPFORMAT) cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  64. STGMEDIUM medium = {0};
  65. medium.tymed = TYMED_HGLOBAL;
  66. medium.hGlobal = hGlobal;
  67. medium.pUnkForRelease = NULL;
  68. // give the data object ownership of ths
  69. return pdtobj->SetData(&fmte, &medium, TRUE);
  70. }
  71. STDAPI_(LPIDA) DataObj_GetHIDAEx(IDataObject *pdtobj, CLIPFORMAT cf, STGMEDIUM *pmedium)
  72. {
  73. FORMATETC fmte = {cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  74. if (pmedium)
  75. {
  76. pmedium->pUnkForRelease = NULL;
  77. pmedium->hGlobal = NULL;
  78. }
  79. if (!pmedium)
  80. {
  81. if (S_OK == pdtobj->QueryGetData(&fmte))
  82. return (LPIDA)TRUE;
  83. else
  84. return (LPIDA)FALSE;
  85. }
  86. else if (SUCCEEDED(pdtobj->GetData(&fmte, pmedium)))
  87. {
  88. return (LPIDA)GlobalLock(pmedium->hGlobal);
  89. }
  90. return NULL;
  91. }
  92. STDAPI_(LPIDA) DataObj_GetHIDA(IDataObject *pdtobj, STGMEDIUM *pmedium)
  93. {
  94. static CLIPFORMAT cfHIDA = 0;
  95. if (!cfHIDA)
  96. {
  97. cfHIDA = (CLIPFORMAT)RegisterClipboardFormat(CFSTR_SHELLIDLIST);
  98. }
  99. return DataObj_GetHIDAEx(pdtobj, cfHIDA, pmedium);
  100. }
  101. STDAPI_(void) ReleaseStgMediumHGLOBAL(void *pv, STGMEDIUM *pmedium)
  102. {
  103. if (pmedium->hGlobal && (pmedium->tymed == TYMED_HGLOBAL))
  104. {
  105. #ifdef DEBUG
  106. if (pv)
  107. {
  108. void *pvT = (void *)GlobalLock(pmedium->hGlobal);
  109. ASSERT(pvT == pv);
  110. GlobalUnlock(pmedium->hGlobal);
  111. }
  112. #endif
  113. GlobalUnlock(pmedium->hGlobal);
  114. }
  115. else
  116. {
  117. ASSERT(0);
  118. }
  119. ReleaseStgMedium(pmedium);
  120. }
  121. STDAPI_(void) HIDA_ReleaseStgMedium(LPIDA pida, STGMEDIUM * pmedium)
  122. {
  123. ReleaseStgMediumHGLOBAL((void *)pida, pmedium);
  124. }