hglobalstream.c 18.4 KB
Newer Older
1 2 3 4
/*
 * HGLOBAL Stream implementation
 *
 * This file contains the implementation of the stream interface
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 * for streams contained supported by an HGLOBAL pointer.
6 7
 *
 * Copyright 1999 Francis Beaudet
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22
 */
23 24 25

#include "config.h"

26 27
#include <assert.h>
#include <stdlib.h>
28
#include <stdarg.h>
29 30 31
#include <stdio.h>
#include <string.h>

32
#define COBJMACROS
33 34
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
35

36
#include "windef.h"
37
#include "winbase.h"
38
#include "winuser.h"
39 40
#include "objbase.h"
#include "ole2.h"
41
#include "winerror.h"
42
#include "winternl.h"
43

44
#include "wine/debug.h"
45

46
WINE_DEFAULT_DEBUG_CHANNEL(storage);
47 48 49 50

/****************************************************************************
 * HGLOBALStreamImpl definition.
 *
Huw Davies's avatar
Huw Davies committed
51
 * This class implements the IStream interface and represents a stream
52 53 54 55
 * supported by an HGLOBAL pointer.
 */
struct HGLOBALStreamImpl
{
56
  const IStreamVtbl *lpVtbl;   /* Needs to be the first item in the struct
57
			  * since we want to cast this in an IStream pointer */
58

59 60 61
  /*
   * Reference count
   */
62
  LONG		     ref;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  /*
   * Support for the stream
   */
  HGLOBAL supportHandle;

  /*
   * This flag is TRUE if the HGLOBAL is destroyed when the stream
   * is finally released.
   */
  BOOL    deleteOnRelease;

  /*
   * Helper variable that contains the size of the stream
   */
  ULARGE_INTEGER     streamSize;

  /*
   * This is the current position of the cursor in the stream
   */
  ULARGE_INTEGER     currentPosition;
};

typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;

/***
 * This is the destructor of the HGLOBALStreamImpl class.
 *
91
 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
92 93 94
 * class. The pointer passed-in to this function will be freed and will not
 * be valid anymore.
 */
95
static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
96
{
97
  TRACE("(%p)\n", This);
98

99 100 101 102 103 104 105 106 107 108 109 110
  /*
   * Release the HGlobal if the constructor asked for that.
   */
  if (This->deleteOnRelease)
  {
    GlobalFree(This->supportHandle);
    This->supportHandle=0;
  }

  /*
   * Finally, free the memory used-up by the class.
   */
111
  HeapFree(GetProcessHeap(), 0, This);
112 113
}

114 115 116 117 118 119 120 121 122 123 124
/***
 * This implements the IUnknown method AddRef for this
 * class
 */
static ULONG WINAPI HGLOBALStreamImpl_AddRef(
		IStream* iface)
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
  return InterlockedIncrement(&This->ref);
}

125 126 127 128
/***
 * This implements the IUnknown method QueryInterface for this
 * class
 */
129
static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
130
		  IStream*     iface,
131 132
		  REFIID         riid,	      /* [in] */
		  void**         ppvObject)   /* [iid_is][out] */
133 134 135 136 137 138 139 140
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;

  /*
   * Perform a sanity check on the parameters.
   */
  if (ppvObject==0)
    return E_INVALIDARG;
141

142 143 144 145
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
146

147 148 149
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
150 151 152
  if (IsEqualIID(&IID_IUnknown, riid) ||
      IsEqualIID(&IID_ISequentialStream, riid) ||
      IsEqualIID(&IID_IStream, riid))
153 154 155
  {
    *ppvObject = (IStream*)This;
  }
156

157 158 159 160 161
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
    return E_NOINTERFACE;
162

163 164 165 166 167
  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
  HGLOBALStreamImpl_AddRef(iface);
168 169

  return S_OK;
170 171 172 173 174 175
}

/***
 * This implements the IUnknown method Release for this
 * class
 */
176
static ULONG WINAPI HGLOBALStreamImpl_Release(
177 178 179 180
		IStream* iface)
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
  ULONG newRef;
181

182
  newRef = InterlockedDecrement(&This->ref);
183

184 185 186 187 188 189 190
  /*
   * If the reference count goes down to 0, perform suicide.
   */
  if (newRef==0)
  {
    HGLOBALStreamImpl_Destroy(This);
  }
191

192 193 194 195 196 197 198 199 200 201 202 203
  return newRef;
}

/***
 * This method is part of the ISequentialStream interface.
 *
 * If reads a block of information from the stream at the current
 * position. It then moves the current position at the end of the
 * read block
 *
 * See the documentation of ISequentialStream for more info.
 */
204
static HRESULT WINAPI HGLOBALStreamImpl_Read(
205 206
		  IStream*     iface,
		  void*          pv,        /* [length_is][size_is][out] */
207 208
		  ULONG          cb,        /* [in] */
		  ULONG*         pcbRead)   /* [out] */
209 210 211 212 213 214
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;

  void* supportBuffer;
  ULONG bytesReadBuffer;
  ULONG bytesToReadFromBuffer;
215

216
  TRACE("(%p, %p, %d, %p)\n", iface,
217
	pv, cb, pcbRead);
218 219

  /*
220 221 222 223 224
   * If the caller is not interested in the nubmer of bytes read,
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbRead==0)
    pcbRead = &bytesReadBuffer;
225

226 227 228 229
  /*
   * Using the known size of the stream, calculate the number of bytes
   * to read from the block chain
   */
230
  bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
231 232 233 234 235

  /*
   * Lock the buffer in position and copy the data.
   */
  supportBuffer = GlobalLock(This->supportHandle);
236 237 238 239 240 241
  if (!supportBuffer)
  {
      WARN("read from invalid hglobal %p\n", This->supportHandle);
      *pcbRead = 0;
      return S_OK;
  }
242

243
  memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
244 245 246 247

  /*
   * Move the current position to the new position
   */
248
  This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
249 250 251 252 253 254 255 256 257 258

  /*
   * Return the number of bytes read.
   */
  *pcbRead = bytesToReadFromBuffer;

  /*
   * Cleanup
   */
  GlobalUnlock(This->supportHandle);
259

260
  /*
261
   * Always returns S_OK even if the end of the stream is reached before the
262 263
   * buffer is filled
   */
264

265
  return S_OK;
266
}
267

268 269 270 271 272 273 274 275 276 277
/***
 * This method is part of the ISequentialStream interface.
 *
 * It writes a block of information to the stream at the current
 * position. It then moves the current position at the end of the
 * written block. If the stream is too small to fit the block,
 * the stream is grown to fit.
 *
 * See the documentation of ISequentialStream for more info.
 */
278
static HRESULT WINAPI HGLOBALStreamImpl_Write(
279
	          IStream*     iface,
280 281 282
		  const void*    pv,          /* [size_is][in] */
		  ULONG          cb,          /* [in] */
		  ULONG*         pcbWritten)  /* [out] */
283 284 285 286 287 288
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;

  void*          supportBuffer;
  ULARGE_INTEGER newSize;
  ULONG          bytesWritten = 0;
289

290
  TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
291

292 293 294 295 296 297
  /*
   * If the caller is not interested in the number of bytes written,
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbWritten == 0)
    pcbWritten = &bytesWritten;
298

299
  if (cb == 0)
300 301
    goto out;

302 303
  *pcbWritten = 0;

304 305
  newSize.u.HighPart = 0;
  newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
306

307 308 309
  /*
   * Verify if we need to grow the stream
   */
310
  if (newSize.u.LowPart > This->streamSize.u.LowPart)
311 312
  {
    /* grow stream */
313 314 315
    HRESULT hr = IStream_SetSize(iface, newSize);
    if (FAILED(hr))
    {
316
      ERR("IStream_SetSize failed with error 0x%08x\n", hr);
317 318
      return hr;
    }
319
  }
320

321 322 323 324
  /*
   * Lock the buffer in position and copy the data.
   */
  supportBuffer = GlobalLock(This->supportHandle);
325 326 327 328 329
  if (!supportBuffer)
  {
      WARN("write to invalid hglobal %p\n", This->supportHandle);
      return S_OK;
  }
330

331
  memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
332 333 334 335

  /*
   * Move the current position to the new position
   */
336
  This->currentPosition.u.LowPart+=cb;
337 338

  /*
339
   * Cleanup
340
   */
341
  GlobalUnlock(This->supportHandle);
342

343
out:
344
  /*
345
   * Return the number of bytes read.
346
   */
347
  *pcbWritten = cb;
348

349 350 351 352 353 354 355 356 357 358
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * It will move the current stream pointer according to the parameters
 * given.
 *
 * See the documentation of IStream for more info.
359
 */
360
static HRESULT WINAPI HGLOBALStreamImpl_Seek(
361
		  IStream*      iface,
362 363
		  LARGE_INTEGER   dlibMove,         /* [in] */
		  DWORD           dwOrigin,         /* [in] */
364 365 366 367 368 369
		  ULARGE_INTEGER* plibNewPosition) /* [out] */
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;

  ULARGE_INTEGER newPosition;

370
  TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
371
	dlibMove.u.LowPart, dwOrigin, plibNewPosition);
372

373 374 375 376 377 378 379
  /*
   * The file pointer is moved depending on the given "function"
   * parameter.
   */
  switch (dwOrigin)
  {
    case STREAM_SEEK_SET:
380 381
      newPosition.u.HighPart = 0;
      newPosition.u.LowPart = 0;
382 383
      break;
    case STREAM_SEEK_CUR:
384
      newPosition = This->currentPosition;
385 386
      break;
    case STREAM_SEEK_END:
387
      newPosition = This->streamSize;
388 389 390 391 392 393 394 395 396 397
      break;
    default:
      return STG_E_INVALIDFUNCTION;
  }

  /*
   * Move the actual file pointer
   * If the file pointer ends-up after the end of the stream, the next Write operation will
   * make the file larger. This is how it is documented.
   */
398 399
  if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;

400 401 402 403
  newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);

  if (plibNewPosition) *plibNewPosition = newPosition;
  This->currentPosition = newPosition;
404

405 406 407 408 409 410 411 412 413 414 415 416
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * It will change the size of a stream.
 *
 * TODO: Switch from small blocks to big blocks and vice versa.
 *
 * See the documentation of IStream for more info.
 */
417
static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
418
				     IStream*      iface,
419
				     ULARGE_INTEGER  libNewSize)   /* [in] */
420 421
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
422
  HGLOBAL supportHandle;
423

424
  TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
425

426
  /*
427
   * HighPart is ignored as shown in tests
428
   */
429

430
  if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
431 432 433 434 435
    return S_OK;

  /*
   * Re allocate the HGlobal to fit the new size of the stream.
   */
436
  supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
437 438

  if (supportHandle == 0)
439
    return E_OUTOFMEMORY;
440

441
  This->supportHandle = supportHandle;
442
  This->streamSize.u.LowPart = libNewSize.u.LowPart;
443

444 445
  return S_OK;
}
446

447 448 449 450 451 452 453
/***
 * This method is part of the IStream interface.
 *
 * It will copy the 'cb' Bytes to 'pstm' IStream.
 *
 * See the documentation of IStream for more info.
 */
454
static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
455
				    IStream*      iface,
456 457 458 459
				    IStream*      pstm,         /* [unique][in] */
				    ULARGE_INTEGER  cb,           /* [in] */
				    ULARGE_INTEGER* pcbRead,      /* [out] */
				    ULARGE_INTEGER* pcbWritten)   /* [out] */
460 461 462 463 464 465 466
{
  HRESULT        hr = S_OK;
  BYTE           tmpBuffer[128];
  ULONG          bytesRead, bytesWritten, copySize;
  ULARGE_INTEGER totalBytesRead;
  ULARGE_INTEGER totalBytesWritten;

467
  TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
468
	cb.u.LowPart, pcbRead, pcbWritten);
469

470 471 472 473 474 475
  /*
   * Sanity check
   */
  if ( pstm == 0 )
    return STG_E_INVALIDPOINTER;

476 477
  totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
  totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
478 479 480 481 482 483

  /*
   * use stack to store data temporarly
   * there is surely more performant way of doing it, for now this basic
   * implementation will do the job
   */
484
  while ( cb.u.LowPart > 0 )
485
  {
486
    if ( cb.u.LowPart >= 128 )
487 488
      copySize = 128;
    else
489
      copySize = cb.u.LowPart;
490

491 492 493
    hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
    if (FAILED(hr))
        break;
494

495
    totalBytesRead.u.LowPart += bytesRead;
496

497
    if (bytesRead)
498
    {
499 500 501 502 503
        hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
        if (FAILED(hr))
            break;

        totalBytesWritten.u.LowPart += bytesWritten;
504
    }
505

506
    if (bytesRead!=copySize)
507
      cb.u.LowPart = 0;
508
    else
509
      cb.u.LowPart -= bytesRead;
510 511 512 513 514 515 516
  }

  /*
   * Update number of bytes read and written
   */
  if (pcbRead)
  {
517 518
    pcbRead->u.LowPart = totalBytesRead.u.LowPart;
    pcbRead->u.HighPart = totalBytesRead.u.HighPart;
519 520 521 522
  }

  if (pcbWritten)
  {
523 524
    pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
    pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
525 526 527 528 529 530 531
  }
  return hr;
}

/***
 * This method is part of the IStream interface.
 *
532
 * For streams supported by HGLOBALS, this function does nothing.
533 534 535
 * This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
536
 */
537
static HRESULT WINAPI HGLOBALStreamImpl_Commit(
538
		  IStream*      iface,
539
		  DWORD         grfCommitFlags)  /* [in] */
540 541 542 543 544 545 546
{
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
547
 * For streams supported by HGLOBALS, this function does nothing.
548 549 550
 * This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
551
 */
552
static HRESULT WINAPI HGLOBALStreamImpl_Revert(
553 554 555 556 557 558 559 560
		  IStream* iface)
{
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
561
 * For streams supported by HGLOBALS, this function does nothing.
562 563 564
 * This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
565
 */
566
static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
567
		  IStream*       iface,
568 569 570
		  ULARGE_INTEGER libOffset,   /* [in] */
		  ULARGE_INTEGER cb,          /* [in] */
		  DWORD          dwLockType)  /* [in] */
571
{
572
  return STG_E_INVALIDFUNCTION;
573 574 575 576 577
}

/*
 * This method is part of the IStream interface.
 *
578
 * For streams supported by HGLOBALS, this function does nothing.
579 580 581
 * This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
582
 */
583
static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
584
		  IStream*       iface,
585 586 587
		  ULARGE_INTEGER libOffset,   /* [in] */
		  ULARGE_INTEGER cb,          /* [in] */
		  DWORD          dwLockType)  /* [in] */
588 589 590 591 592 593 594 595 596 597 598
{
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * This method returns information about the current
 * stream.
 *
 * See the documentation of IStream for more info.
599
 */
600
static HRESULT WINAPI HGLOBALStreamImpl_Stat(
601 602
		  IStream*     iface,
		  STATSTG*     pstatstg,     /* [out] */
603
		  DWORD        grfStatFlag)  /* [in] */
604 605 606 607 608 609 610 611 612 613 614
{
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;

  memset(pstatstg, 0, sizeof(STATSTG));

  pstatstg->pwcsName = NULL;
  pstatstg->type     = STGTY_STREAM;
  pstatstg->cbSize   = This->streamSize;

  return S_OK;
}
615

616
static HRESULT WINAPI HGLOBALStreamImpl_Clone(
617
		  IStream*     iface,
618
		  IStream**    ppstm) /* [out] */
619
{
620 621 622 623 624 625 626 627 628 629 630
  ULARGE_INTEGER dummy;
  LARGE_INTEGER offset;
  HRESULT hr;
  HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
  TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
  hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
  if(FAILED(hr))
    return hr;
  offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
  HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
  return S_OK;
631
}
632 633 634 635

/*
 * Virtual function table for the HGLOBALStreamImpl class.
 */
636
static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
{
    HGLOBALStreamImpl_QueryInterface,
    HGLOBALStreamImpl_AddRef,
    HGLOBALStreamImpl_Release,
    HGLOBALStreamImpl_Read,
    HGLOBALStreamImpl_Write,
    HGLOBALStreamImpl_Seek,
    HGLOBALStreamImpl_SetSize,
    HGLOBALStreamImpl_CopyTo,
    HGLOBALStreamImpl_Commit,
    HGLOBALStreamImpl_Revert,
    HGLOBALStreamImpl_LockRegion,
    HGLOBALStreamImpl_UnlockRegion,
    HGLOBALStreamImpl_Stat,
    HGLOBALStreamImpl_Clone
};

/******************************************************************************
** HGLOBALStreamImpl implementation
*/

/***
 * This is the constructor for the HGLOBALStreamImpl class.
 *
 * Params:
 *    hGlobal          - Handle that will support the stream. can be NULL.
 *    fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
 *                       when the IStream object is destroyed.
 */
666
static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
		HGLOBAL  hGlobal,
		BOOL     fDeleteOnRelease)
{
  HGLOBALStreamImpl* newStream;

  newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));

  if (newStream!=0)
  {
    /*
     * Set-up the virtual function table and reference count.
     */
    newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
    newStream->ref    = 0;

    /*
     * Initialize the support.
     */
    newStream->supportHandle = hGlobal;
    newStream->deleteOnRelease = fDeleteOnRelease;

    /*
     * This method will allocate a handle if one is not supplied.
     */
    if (!newStream->supportHandle)
    {
      newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
					     GMEM_SHARE, 0);
    }

    /*
     * Start the stream at the beginning.
     */
    newStream->currentPosition.u.HighPart = 0;
    newStream->currentPosition.u.LowPart = 0;

    /*
     * Initialize the size of the stream to the size of the handle.
     */
    newStream->streamSize.u.HighPart = 0;
    newStream->streamSize.u.LowPart  = GlobalSize(newStream->supportHandle);
  }

  return newStream;
}


/***********************************************************************
 *           CreateStreamOnHGlobal     [OLE32.@]
 */
HRESULT WINAPI CreateStreamOnHGlobal(
		HGLOBAL   hGlobal,
		BOOL      fDeleteOnRelease,
		LPSTREAM* ppstm)
{
  HGLOBALStreamImpl* newStream;

  newStream = HGLOBALStreamImpl_Construct(hGlobal,
					  fDeleteOnRelease);

  if (newStream!=NULL)
  {
    return IUnknown_QueryInterface((IUnknown*)newStream,
				   &IID_IStream,
				   (void**)ppstm);
  }

  return E_OUTOFMEMORY;
}

/***********************************************************************
 *           GetHGlobalFromStream     [OLE32.@]
 */
HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
{
  HGLOBALStreamImpl* pStream;

  if (pstm == NULL)
    return E_INVALIDARG;

  pStream = (HGLOBALStreamImpl*) pstm;

  /*
   * Verify that the stream object was created with CreateStreamOnHGlobal.
   */
  if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
    *phglobal = pStream->supportHandle;
  else
  {
    *phglobal = 0;
    return E_INVALIDARG;
  }

  return S_OK;
}