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.

238 lines
5.8 KiB

  1. #include "precomp.h"
  2. #include "ObjAccess.h"
  3. /////////////////////////////////////////////////////////////////////////////
  4. // CObjAccess
  5. CObjAccess::CObjAccess() :
  6. m_pObj(NULL),
  7. m_pObjAccess(NULL),
  8. m_pWmiObj(NULL),
  9. m_pProps(0)
  10. {
  11. }
  12. CObjAccess::CObjAccess(const CObjAccess& other) :
  13. m_pProps(other.m_pProps),
  14. m_pObj(NULL),
  15. m_pObjAccess(NULL),
  16. m_pWmiObj(NULL)
  17. {
  18. other.m_pObj->Clone(&m_pObj);
  19. m_pObj->QueryInterface(
  20. IID_IWbemObjectAccess,
  21. (LPVOID*) &m_pObjAccess);
  22. m_pObj->QueryInterface(
  23. IID__IWmiObject,
  24. (LPVOID*) &m_pWmiObj);
  25. }
  26. CObjAccess::~CObjAccess()
  27. {
  28. if (m_pObjAccess)
  29. m_pObjAccess->Release();
  30. if (m_pObj)
  31. m_pObj->Release();
  32. if (m_pWmiObj)
  33. m_pWmiObj->Release();
  34. }
  35. const CObjAccess& CObjAccess::operator=(const CObjAccess& other)
  36. {
  37. m_pProps = other.m_pProps;
  38. other.m_pObj->Clone(&m_pObj);
  39. m_pObj->QueryInterface(
  40. IID_IWbemObjectAccess,
  41. (LPVOID*) &m_pObjAccess);
  42. m_pObj->QueryInterface(
  43. IID__IWmiObject,
  44. (LPVOID*) &m_pWmiObj);
  45. return *this;
  46. }
  47. BOOL CObjAccess::Init(
  48. IWbemServices *pSvc,
  49. LPCWSTR szClass,
  50. LPCWSTR *pszPropNames,
  51. DWORD nProps,
  52. INIT_FAILED_PROP_TYPE typeFail)
  53. {
  54. IWbemClassObject *pClass = NULL;
  55. BOOL bRet = FALSE;
  56. if (SUCCEEDED(pSvc->GetObject(
  57. _bstr_t(szClass),
  58. 0,
  59. NULL,
  60. &pClass,
  61. NULL)) &&
  62. SUCCEEDED(pClass->SpawnInstance(0, &m_pObj)))
  63. {
  64. // Get out if we don't need the whole IWbemObjectAccess stuff.
  65. if (nProps == 0)
  66. return TRUE;
  67. m_pObj->QueryInterface(IID_IWbemObjectAccess, (LPVOID*) &m_pObjAccess);
  68. m_pObj->QueryInterface(IID__IWmiObject, (LPVOID*) &m_pWmiObj);
  69. if (m_pProps.Init(nProps))
  70. {
  71. m_pProps.SetCount(nProps);
  72. bRet = TRUE;
  73. for (DWORD i = 0; i < nProps; i++)
  74. {
  75. CProp &prop = m_pProps[i];
  76. if(m_pWmiObj)
  77. {
  78. if (SUCCEEDED(m_pWmiObj->GetPropertyHandleEx(
  79. pszPropNames[i],
  80. 0,
  81. &prop.m_type,
  82. &prop.m_lHandle)))
  83. {
  84. prop.m_strName = pszPropNames[i];
  85. }
  86. }
  87. else
  88. {
  89. if (SUCCEEDED(m_pObjAccess->GetPropertyHandle(
  90. pszPropNames[i],
  91. &prop.m_type,
  92. &prop.m_lHandle)))
  93. {
  94. prop.m_strName = pszPropNames[i];
  95. }
  96. }
  97. }
  98. }
  99. }
  100. if (pClass)
  101. pClass->Release();
  102. return bRet;
  103. }
  104. BOOL CObjAccess::WriteData(DWORD dwIndex, LPVOID pData, DWORD dwSize)
  105. {
  106. CProp &prop = m_pProps[dwIndex];
  107. BOOL bRet = FALSE;
  108. // This function only works for non arrays.
  109. _ASSERT((prop.m_type & CIM_FLAG_ARRAY) == 0);
  110. bRet =
  111. SUCCEEDED(m_pObjAccess->WritePropertyValue(
  112. prop.m_lHandle,
  113. dwSize,
  114. (LPBYTE) pData));
  115. return bRet;
  116. }
  117. BOOL CObjAccess::WriteArrayData(DWORD dwIndex, LPVOID pData, DWORD dwItemSize)
  118. {
  119. if(m_pWmiObj == NULL)
  120. return TRUE; // pretend we did it
  121. CProp &prop = m_pProps[dwIndex];
  122. // This function only works for arrays.
  123. _ASSERT(prop.m_type & CIM_FLAG_ARRAY);
  124. DWORD dwItems = *(DWORD*) pData,
  125. dwSize = dwItems * dwItemSize;
  126. HRESULT hr;
  127. hr =
  128. m_pWmiObj->SetArrayPropRangeByHandle(
  129. prop.m_lHandle,
  130. WMIARRAY_FLAG_ALLELEMENTS, // flags
  131. 0, // start index
  132. dwItems, // # items
  133. dwSize, // buffer size
  134. ((LPBYTE) pData) + sizeof(DWORD)); // data buffer
  135. return SUCCEEDED(hr);
  136. }
  137. BOOL CObjAccess::WriteNonPackedArrayData(
  138. DWORD dwIndex,
  139. LPVOID pData,
  140. DWORD dwItems,
  141. DWORD dwTotalSize)
  142. {
  143. if(m_pWmiObj == NULL)
  144. return TRUE; // pretend we did it
  145. CProp &prop = m_pProps[dwIndex];
  146. HRESULT hr;
  147. // This function only works for arrays.
  148. _ASSERT(prop.m_type & CIM_FLAG_ARRAY);
  149. hr =
  150. m_pWmiObj->SetArrayPropRangeByHandle(
  151. prop.m_lHandle,
  152. WMIARRAY_FLAG_ALLELEMENTS, // flags
  153. 0, // start index
  154. dwItems, // # items
  155. dwTotalSize, // buffer size
  156. pData); // data buffer
  157. return SUCCEEDED(hr);
  158. }
  159. BOOL CObjAccess::WriteString(DWORD dwIndex, LPCWSTR szValue)
  160. {
  161. return
  162. SUCCEEDED(m_pObjAccess->WritePropertyValue(
  163. m_pProps[dwIndex].m_lHandle,
  164. (wcslen(szValue) + 1) * sizeof(WCHAR),
  165. (LPBYTE) szValue));
  166. }
  167. BOOL CObjAccess::WriteString(DWORD dwIndex, LPCSTR szValue)
  168. {
  169. return WriteString(dwIndex, (LPCWSTR) _bstr_t(szValue));
  170. }
  171. BOOL CObjAccess::WriteDWORD(DWORD dwIndex, DWORD dwValue)
  172. {
  173. return
  174. SUCCEEDED(m_pObjAccess->WriteDWORD(
  175. m_pProps[dwIndex].m_lHandle,
  176. dwValue));
  177. }
  178. BOOL CObjAccess::WriteDWORD64(DWORD dwIndex, DWORD64 dwValue)
  179. {
  180. return
  181. SUCCEEDED(m_pObjAccess->WriteQWORD(
  182. m_pProps[dwIndex].m_lHandle,
  183. dwValue));
  184. }
  185. BOOL CObjAccess::WriteNULL(DWORD dwIndex)
  186. {
  187. return
  188. SUCCEEDED(m_pObj->Put(
  189. m_pProps[dwIndex].m_strName,
  190. 0,
  191. NULL,
  192. 0));
  193. }