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.

243 lines
6.5 KiB

  1. /*-----------------------------------------------------------------------------
  2. *
  3. * File: wiautil.cpp
  4. * Author: Samuel Clement (samclem)
  5. * Date: Mon Aug 16 13:22:36 1999
  6. *
  7. * Copyright (c) 1999 Microsoft Corporation
  8. *
  9. * Description:
  10. * Contains the implementation of the Wia utility methods.
  11. *
  12. * History:
  13. * 16 Aug 1999: Created.
  14. *----------------------------------------------------------------------------*/
  15. #include "stdafx.h"
  16. /*-----------------------------------------------------------------------------
  17. * GetStringForVal
  18. *
  19. * This returns the string assioctiated with value (dwVal) in the string
  20. * table. If it is not found it will return the default value, or NULL if
  21. * not default was provided.
  22. *
  23. * pStrTable: the table to search for the given string
  24. * dwVal: what value ypu want to look for.
  25. *--(samclem)-----------------------------------------------------------------*/
  26. WCHAR* GetStringForVal( const STRINGTABLE* pStrTable, DWORD dwVal )
  27. {
  28. Assert( pStrTable != NULL );
  29. // there is always at least 2 entries in the
  30. // string table. the 0th entry is what to return
  31. // if we don't find anything.
  32. int iStr = 1;
  33. while ( pStrTable[iStr].pwchValue )
  34. {
  35. if ( pStrTable[iStr].dwStartRange >= dwVal &&
  36. pStrTable[iStr].dwEndRange <= dwVal )
  37. return pStrTable[iStr].pwchValue;
  38. iStr++;
  39. }
  40. // didn't find anything return the value at entry 0
  41. return pStrTable->pwchValue;
  42. }
  43. /*-----------------------------------------------------------------------------
  44. * GetWiaProperty
  45. *
  46. * This will get the desired property from the given property storage and
  47. * fill the our param pvaProp with the value. This prop variant doesn't have
  48. * to be initialized.
  49. *
  50. * pStg: the IWiaPropertyStorage to query for the property
  51. * propid: the property id of the property that we want
  52. * pvaProp: Out, recieves the value of the prop, or VT_EMPTY if not found.
  53. *--(samclem)-----------------------------------------------------------------*/
  54. HRESULT GetWiaProperty( IWiaPropertyStorage* pStg, PROPID propid, PROPVARIANT* pvaProp )
  55. {
  56. Assert( pStg != NULL );
  57. Assert( pvaProp != NULL );
  58. PROPSPEC pr;
  59. HRESULT hr;
  60. pr.ulKind = PRSPEC_PROPID;
  61. pr.propid = propid;
  62. PropVariantInit( pvaProp );
  63. hr = pStg->ReadMultiple( 1, &pr, pvaProp );
  64. return hr;
  65. }
  66. /*-----------------------------------------------------------------------------
  67. * GetWiaPropertyBSTR
  68. *
  69. * This will get the desired property for the given property storage, and
  70. * then attempt to convert it to a BSTR. If it can't convert the property
  71. * then an error is returned, and the out param is null.
  72. *
  73. * pStg: The IWiaPropertyStorage that we want to read the property from
  74. * propid: the property that we want to read and coherce
  75. * pbstrProp: Out, recieves the value of the property, or NULL on error
  76. *--(samclem)-----------------------------------------------------------------*/
  77. HRESULT GetWiaPropertyBSTR( IWiaPropertyStorage* pStg, PROPID propid, BSTR* pbstrProp )
  78. {
  79. Assert( pbstrProp != NULL );
  80. Assert( pStg != NULL );
  81. PROPVARIANT vaProp;
  82. *pbstrProp = NULL;
  83. if ( FAILED( GetWiaProperty( pStg, propid, &vaProp ) ) )
  84. return E_FAIL;
  85. switch ( vaProp.vt )
  86. {
  87. case VT_EMPTY:
  88. *pbstrProp = SysAllocString( L"" );
  89. break;
  90. case VT_BSTR:
  91. case VT_LPWSTR:
  92. *pbstrProp = SysAllocString( vaProp.pwszVal );
  93. break;
  94. case VT_CLSID:
  95. {
  96. OLECHAR rgoch[100] = { 0 }; // more than enough for a clsid
  97. if ( SUCCEEDED( StringFromGUID2( *vaProp.puuid, rgoch, 100 ) ) )
  98. {
  99. *pbstrProp = SysAllocString( rgoch );
  100. }
  101. }
  102. break;
  103. }
  104. PropVariantClear( &vaProp );
  105. if ( NULL == *pbstrProp )
  106. return E_FAIL;
  107. return S_OK;
  108. }
  109. // helper macro
  110. #define SETVAR( vti, in, out, field ) \
  111. (out)->vt = vti; \
  112. (out)->field = (in)->field; \
  113. break
  114. #define SETVAR_( vti, val, out, field, err ) \
  115. (out)->vt = vti; \
  116. (out)->field = val; \
  117. if ( !((out)->field) ) \
  118. return (err); \
  119. break
  120. /*-----------------------------------------------------------------------------
  121. * PropVariantToVariant
  122. *
  123. * This copies the contents of the PropVariant to a variant.
  124. *
  125. * pvaProp: the prop variant to copy from
  126. * pvaOut: the variant to copy into
  127. *--(samclem)-----------------------------------------------------------------*/
  128. HRESULT PropVariantToVariant( const PROPVARIANT* pvaProp, VARIANT* pvaOut )
  129. {
  130. Assert( pvaProp && pvaOut );
  131. // init the out param
  132. VariantInit( pvaOut );
  133. switch ( pvaProp->vt )
  134. {
  135. case VT_EMPTY:
  136. VariantInit( pvaOut );
  137. pvaOut->vt = VT_EMPTY;
  138. break;
  139. case VT_UI1:
  140. SETVAR( VT_UI1, pvaProp, pvaOut, bVal );
  141. case VT_I2:
  142. SETVAR( VT_I2, pvaProp, pvaOut, iVal );
  143. case VT_I4:
  144. SETVAR( VT_I4, pvaProp, pvaOut, lVal );
  145. case VT_R4:
  146. SETVAR( VT_R4, pvaProp, pvaOut, fltVal );
  147. case VT_R8:
  148. SETVAR( VT_R8, pvaProp, pvaOut, dblVal );
  149. case VT_CY:
  150. SETVAR( VT_CY, pvaProp, pvaOut, cyVal );
  151. case VT_DATE:
  152. SETVAR( VT_DATE, pvaProp, pvaOut, date );
  153. case VT_BSTR:
  154. SETVAR_( VT_BSTR, SysAllocString( pvaProp->bstrVal ), pvaOut, bstrVal, E_OUTOFMEMORY );
  155. case VT_BOOL:
  156. SETVAR( VT_BOOL, pvaProp, pvaOut, boolVal );
  157. case VT_LPWSTR:
  158. SETVAR_( VT_BSTR, SysAllocString( pvaProp->pwszVal ), pvaOut, bstrVal, E_OUTOFMEMORY );
  159. case VT_LPSTR:
  160. {
  161. if ( pvaProp->pszVal )
  162. {
  163. WCHAR* pwch = NULL;
  164. size_t len = strlen( pvaProp->pszVal ) + 1;
  165. pwch = new WCHAR[len];
  166. if ( !pwch )
  167. return E_OUTOFMEMORY;
  168. if ( !MultiByteToWideChar( CP_ACP, 0,
  169. pvaProp->pszVal, -1, pwch, len ) )
  170. {
  171. delete[] pwch;
  172. return E_FAIL;
  173. }
  174. pvaOut->vt = VT_BSTR;
  175. pvaOut->bstrVal = SysAllocString( pwch );
  176. delete[] pwch;
  177. if ( !pvaOut->bstrVal )
  178. return E_OUTOFMEMORY;
  179. break;
  180. }
  181. else
  182. {
  183. SETVAR_( VT_BSTR, SysAllocString( L"" ), pvaOut, bstrVal, E_OUTOFMEMORY );
  184. }
  185. }
  186. #if defined(LATERW2K_PLATSDK)
  187. case VT_UNKNOWN:
  188. pvaOut->vt = VT_UNKNOWN;
  189. pvaOut->punkVal = pvaProp->punkVal;
  190. pvaOut->punkVal->AddRef();
  191. break;
  192. case VT_DISPATCH:
  193. pvaOut->vt = VT_DISPATCH;
  194. pvaOut->pdispVal = pvaProp->pdispVal;
  195. pvaOut->pdispVal->AddRef();
  196. break;
  197. case VT_SAFEARRAY:
  198. if ( FAILED( SafeArrayCopy( pvaProp->parray, &pvaOut->parray ) ) )
  199. return E_FAIL;
  200. pvaOut->vt = VT_SAFEARRAY;
  201. break;
  202. #endif //defined(LATERW2K_PLATSDK)
  203. default:
  204. return E_FAIL;
  205. }
  206. return S_OK;
  207. }
  208. HRESULT VariantToPropVariant( const VARIANT* pvaIn, PROPVARIANT* pvaProp )
  209. {
  210. return E_NOTIMPL;
  211. }