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.

120 lines
2.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: lockbyte.cxx
  7. //
  8. // Contents: lockbyte.cpp from OLE2
  9. //
  10. // History: 11-Apr-94 DrewB Copied from OLE2
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "headers.cxx"
  14. #pragma hdrstop
  15. #include <ole2int.h>
  16. #include "memstm.h"
  17. #include <reterr.h>
  18. // CreateILockBytesOnHGlobal
  19. //
  20. OLEAPI CreateILockBytesOnHGlobal
  21. (HGLOBAL hGlobal,
  22. BOOL fDeleteOnRelease,
  23. LPLOCKBYTES FAR* pplkbyt)
  24. {
  25. HANDLE hMem = NULL; // point to
  26. struct MEMSTM FAR* pData = NULL; // a struct MEMSTM
  27. ILockBytes FAR* pBytes = NULL;
  28. DWORD cbSize = -1L;
  29. VDATEPTRIN (pplkbyt, LPLOCKBYTES);
  30. *pplkbyt = NULL;
  31. if (NULL==hGlobal)
  32. {
  33. hGlobal = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, 0);
  34. if (hGlobal == NULL)
  35. goto ErrorExit;
  36. cbSize = 0;
  37. // REVIEW: need to free this block if error below
  38. }
  39. else
  40. {
  41. cbSize = GlobalSize (hGlobal);
  42. // Is there a way to verify a zero-sized handle?
  43. if (cbSize!=0)
  44. {
  45. // verify validity of passed-in handle
  46. if (NULL==GlobalLock(hGlobal))
  47. {
  48. // bad handle
  49. return ResultFromScode (E_INVALIDARG);
  50. }
  51. GlobalUnlock (hGlobal);
  52. }
  53. }
  54. hMem = GlobalAlloc (GMEM_DDESHARE | GMEM_MOVEABLE, sizeof (MEMSTM));
  55. if (hMem == NULL)
  56. goto ErrorExit;
  57. pData = (MEMSTM FAR*)MAKELONG(0, HIWORD(GlobalHandle(hMem)));
  58. if (pData == NULL)
  59. goto FreeMem;
  60. pData->cRef = 0;
  61. pData->cb = cbSize;
  62. pData->fDeleteOnRelease = fDeleteOnRelease;
  63. pData->hGlobal = hGlobal;
  64. pBytes = CMemBytes::Create(hMem); // Create the ILockBytes
  65. if (pBytes == NULL)
  66. goto FreeMem;
  67. *pplkbyt = pBytes;
  68. return NOERROR;
  69. FreeMem:
  70. if (hMem)
  71. GlobalFree(hMem);
  72. ErrorExit:
  73. Assert (0);
  74. return ReportResult(0, E_OUTOFMEMORY, 0, 0);
  75. }
  76. OLEAPI GetHGlobalFromILockBytes
  77. (LPLOCKBYTES plkbyt,
  78. HGLOBAL FAR* phglobal)
  79. {
  80. VDATEIFACE (plkbyt);
  81. VDATEPTRIN (phglobal, HANDLE);
  82. *phglobal = NULL;
  83. CMemBytes FAR* pCMemByte = (CMemBytes FAR*)plkbyt;
  84. if (IsBadReadPtr (&(pCMemByte->m_dwSig), sizeof(ULONG))
  85. || pCMemByte->m_dwSig != LOCKBYTE_SIG)
  86. {
  87. // we were passed someone else's implementation of ILockBytes
  88. return ResultFromScode (E_INVALIDARG);
  89. }
  90. MEMSTM FAR* pMem= pCMemByte->m_pData;
  91. if (NULL==pMem)
  92. {
  93. Assert (0);
  94. return ResultFromScode (E_OUTOFMEMORY);
  95. }
  96. Assert (pMem->cb <= GlobalSize (pMem->hGlobal));
  97. Verify (*phglobal = pMem->hGlobal);
  98. return NOERROR;
  99. }