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.

108 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name :
  4. sahlp.h
  5. Abstract:
  6. Simple helpers for dealing with safearrays
  7. Author:
  8. Kulo Rajasekaran (kulor) 5/25/2000
  9. Environment:
  10. Win32
  11. Project:
  12. utils
  13. Revision History:
  14. --*/
  15. #pragma once
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // SA_CreateOneDim
  18. //
  19. // Description
  20. // Use this to create a 0 based, one dimensional safearray of any of the datatypes
  21. // supported by VARIANT
  22. //
  23. // Ex: SAFEARRAY *psa = NULL;
  24. // SA_CreateOneDim ( psa, 5, VT_BSTR );
  25. // for (ULONG i=0 ; i<5 ; i++ ) {
  26. // SafeArrayPutElement ( psa, &i, pSomeValidBstrHere );
  27. //
  28. inline HRESULT SA_CreateOneDim
  29. (
  30. SAFEARRAY **ppsa,
  31. DWORD cElems,
  32. short vt
  33. )
  34. {
  35. HRESULT hr = S_OK;
  36. SAFEARRAY *psa = NULL;
  37. SAFEARRAYBOUND bound;
  38. long *plAddr = NULL;
  39. long lIdx = 0L;
  40. UINT nElemSize = 0;
  41. bound.lLbound = 0;
  42. bound.cElements = cElems;
  43. psa = ::SafeArrayCreate ( vt, 1, &bound );
  44. if ( !psa ) {
  45. hr = E_FAIL;
  46. goto exit;
  47. }
  48. ::SafeArrayPtrOfIndex( psa, &lIdx, (void**)&plAddr );
  49. nElemSize = ::SafeArrayGetElemsize( psa );
  50. memset(plAddr, 0L, cElems * nElemSize);
  51. *ppsa = psa;
  52. exit:
  53. if ( FAILED(hr) )
  54. if ( psa )
  55. ::SafeArrayDestroy ( psa );
  56. return ( hr );
  57. }
  58. ///////////////////////////////////////////////////////////////////////////////
  59. // Variant_CreateOneDim
  60. //
  61. // Description
  62. // Use this to create a safearray ( of type vt ) with in the passed Variant
  63. //
  64. // See SA_CreateOneDim for description
  65. //
  66. inline HRESULT Variant_CreateOneDim ( VARIANT *pv, DWORD cElems, short vt = VT_VARIANT)
  67. {
  68. HRESULT hr;
  69. hr = ::VariantClear(pv);
  70. hr = SA_CreateOneDim ( &pv->parray, cElems, vt );
  71. if ( SUCCEEDED(hr) )
  72. pv->vt = vt | VT_ARRAY;
  73. return ( hr );
  74. }
  75. inline HRESULT Variant_DestroyOneDim( VARIANT *pv )
  76. {
  77. HRESULT hr = E_FAIL;
  78. SAFEARRAY *psa = pv->parray;
  79. _ASSERTE (psa != NULL);
  80. hr = SafeArrayDestroy(psa);
  81. return hr;
  82. }