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.

178 lines
3.0 KiB

  1. //
  2. // Copyright 1997 - Microsoft
  3. //
  4. //
  5. // VARCONV.CPP - Handlers for converting from/to VARIANTs.
  6. //
  7. #include "pch.h"
  8. #include "varconv.h"
  9. DEFINE_MODULE("IMADMUI")
  10. //
  11. // StringArrayToVariant( )
  12. //
  13. // Creats an array of Vars that are BSTRs.
  14. HRESULT
  15. StringArrayToVariant(
  16. VARIANT * pvData,
  17. LPWSTR lpszData[], // array of LPWSTRs
  18. DWORD dwCount ) // number of items in the array
  19. {
  20. TraceFunc( "StringArrayToVariant( ... )\n" );
  21. HRESULT hr;
  22. DWORD dw;
  23. VARIANT * pvar;
  24. SAFEARRAY * sa;
  25. SAFEARRAYBOUND rgsabound[1];
  26. rgsabound[0].lLbound = 0;
  27. rgsabound[0].cElements = dwCount;
  28. sa = SafeArrayCreate( VT_VARIANT, 1, rgsabound );
  29. if ( !sa )
  30. RRETURN(E_OUTOFMEMORY);
  31. hr = THR( SafeArrayAccessData( sa, (void**) &pvar ) );
  32. if (hr)
  33. goto Error;
  34. for ( dw = 0; dw < dwCount; dw++ )
  35. {
  36. hr = THR( PackStringToVariant( &pvar[dw], lpszData[dw] ) );
  37. if (hr)
  38. {
  39. SafeArrayUnaccessData( sa );
  40. goto Error;
  41. }
  42. }
  43. SafeArrayUnaccessData( sa );
  44. pvData->vt = VT_ARRAY | VT_VARIANT;
  45. pvData->parray = sa;
  46. Error:
  47. HRETURN(hr);
  48. }
  49. //
  50. // PackStringToVariant( )
  51. //
  52. HRESULT
  53. PackStringToVariant(
  54. VARIANT * pvData,
  55. LPWSTR lpszData )
  56. {
  57. TraceFunc( "PackStringToVariant( )\n" );
  58. BSTR bstrData = NULL;
  59. if ( !lpszData || !pvData )
  60. RRETURN(E_INVALIDARG);
  61. bstrData = SysAllocString(lpszData);
  62. if ( !bstrData )
  63. RRETURN(E_OUTOFMEMORY);
  64. pvData->vt = VT_BSTR;
  65. pvData->bstrVal = bstrData;
  66. HRETURN(S_OK);
  67. }
  68. //
  69. // PackBytesToVariant( )
  70. //
  71. HRESULT
  72. PackBytesToVariant(
  73. VARIANT* pvData,
  74. LPBYTE lpData,
  75. DWORD cbBytes )
  76. {
  77. TraceFunc( "PackBytesToVariant( )\n" );
  78. HRESULT hr = S_OK;
  79. LPBYTE ptr;
  80. SAFEARRAY* sa = NULL;
  81. SAFEARRAYBOUND rgsabound[1];
  82. if ( !lpData )
  83. RRETURN(E_INVALIDARG);
  84. if ( !pvData )
  85. RRETURN(E_INVALIDARG);
  86. rgsabound[0].lLbound = 0;
  87. rgsabound[0].cElements = cbBytes;
  88. sa = SafeArrayCreate( VT_UI1, 1, rgsabound );
  89. if ( !sa )
  90. RRETURN(E_OUTOFMEMORY);
  91. hr = THR( SafeArrayAccessData( sa, (LPVOID*)&ptr ) );
  92. if (hr)
  93. goto Error;
  94. CopyMemory( ptr, lpData, cbBytes );
  95. SafeArrayUnaccessData( sa );
  96. pvData->vt = VT_UI1 | VT_ARRAY;
  97. pvData->parray = sa;
  98. Cleanup:
  99. HRETURN(hr);
  100. Error:
  101. if ( sa )
  102. SafeArrayDestroy( sa );
  103. goto Cleanup;
  104. }
  105. //
  106. // PackDWORDToVariant( )
  107. //
  108. HRESULT
  109. PackDWORDToVariant(
  110. VARIANT * pvData,
  111. DWORD dwData )
  112. {
  113. TraceFunc( "PackDWORDToVariant( )\n" );
  114. if ( !pvData )
  115. RRETURN(E_INVALIDARG);
  116. pvData->vt = VT_I4;
  117. pvData->lVal = dwData;
  118. HRETURN(S_OK);
  119. }
  120. //
  121. // PackBOOLToVariant( )
  122. //
  123. HRESULT
  124. PackBOOLToVariant(
  125. VARIANT * pvData,
  126. BOOL fData )
  127. {
  128. TraceFunc( "PackBOOLToVariant( )\n" );
  129. if ( !pvData )
  130. RETURN(E_INVALIDARG);
  131. pvData->vt = VT_BOOL;
  132. V_BOOL( pvData ) = (VARIANT_BOOL)fData;
  133. RETURN(S_OK);
  134. }