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.

209 lines
6.2 KiB

  1. /******************************************************************
  2. SrvProp.cpp -- Properties class definition
  3. MODULE:
  4. DhcpProv.dll
  5. DESCRIPTION:
  6. Contains the definition of the class modeling the DHCP_Server property.
  7. REVISION:
  8. 08/03/98 - created
  9. ******************************************************************/
  10. #include <stdafx.h>
  11. #include "Props.h"
  12. /*------------------------Utility functions below------------------------------*/
  13. // DESCRIPTION:
  14. // Converts a CHString containing the string representation of an IPAddress to the DHCP_IP_ADDRESS type.
  15. // I would have used here inet_addr but this one only takes char * as arguments (not wchar_t *)
  16. // Note that the IPAddress can be any substring of 'str'!
  17. BOOL inet_wstodw(CHString str, DHCP_IP_ADDRESS & IpAddress)
  18. {
  19. DWORD value;
  20. int i;
  21. for (i = 0; i < str.GetLength(); i++)
  22. if (str[i] >= _T('0') && str[i] <= _T('9'))
  23. break;
  24. IpAddress = 0;
  25. for (int k = 0; i < str.GetLength(); k++)
  26. {
  27. value = 0;
  28. // at this point, str[i] is definitely a decimal digit
  29. do
  30. {
  31. value *= 10;
  32. value += str[i++] - _T('0');
  33. } while(str[i] >= _T('0') && str[i] <= _T('9'));
  34. if (str[i] == _T('.'))
  35. {
  36. IpAddress <<= 8;
  37. IpAddress |= (value & 0xff);
  38. i++;
  39. if (str[i] >= _T('0') && str[i] <= _T('9'))
  40. continue;
  41. }
  42. if (k != 3)
  43. return FALSE;
  44. else
  45. break;
  46. }
  47. IpAddress <<= 8;
  48. IpAddress |= (value & 0xff);
  49. return TRUE;
  50. }
  51. BOOL dupDhcpBinaryData(DHCP_BINARY_DATA &src, DHCP_BINARY_DATA &dest)
  52. {
  53. dest.DataLength = src.DataLength;
  54. if (dest.DataLength == NULL)
  55. return TRUE;
  56. if (src.Data == NULL)
  57. return FALSE;
  58. dest.Data = (BYTE*)MIDL_user_allocate(dest.DataLength);
  59. if (!dest.Data)
  60. return FALSE;
  61. memcpy(dest.Data, src.Data, dest.DataLength);
  62. return TRUE;
  63. }
  64. BOOL InstanceSetByteArray(CInstance *pInstance, const CHString& strProperty, BYTE *bArray, DWORD dwSzArray)
  65. {
  66. SAFEARRAY *saVariable;
  67. SAFEARRAYBOUND saVector;
  68. HRESULT retCode = WBEM_E_FAILED;
  69. if (pInstance == NULL)
  70. return FALSE;
  71. // first create the SAFEARRAY to pass to the pInstance
  72. saVector.cElements = dwSzArray; // on the only dimension we have, there are dwSzArray elements
  73. saVector.lLbound = 0; // the lowest index of the dimension is 0
  74. saVariable = SafeArrayCreate(VT_UI1, 1, &saVector); // create a unsigned char array on one dimension
  75. if (saVariable != NULL) // continue if creation succeeded
  76. {
  77. // transfer the binary array to the SAFEARRAY
  78. for (long i = 0; i<dwSzArray; i++)
  79. {
  80. if (SafeArrayPutElement(saVariable, &i, bArray + i) != S_OK)
  81. break;
  82. }
  83. if (i == dwSzArray) // if the SAFEARRAY was built successfully continue
  84. {
  85. // build the Variant that holds the SAFEARRAY
  86. VARIANT Variant;
  87. Variant.vt = VT_UI1 | VT_ARRAY;
  88. Variant.parray = saVariable;
  89. // build the BSTR holding the property name
  90. BSTR bstrProp = strProperty.AllocSysString();
  91. if (bstrProp != NULL) // if the property was converted to BSTR successfully go on..
  92. {
  93. IWbemClassObject *wbemClassObject = pInstance->GetClassObjectInterface();
  94. if (wbemClassObject != NULL) // everything went fine, now just put the value
  95. retCode = wbemClassObject->Put(bstrProp, 0, &Variant, 0);
  96. SysFreeString ( bstrProp );
  97. }
  98. }
  99. SafeArrayDestroy(saVariable);
  100. }
  101. return retCode == WBEM_S_NO_ERROR;
  102. }
  103. BOOL InstanceGetByteArray(CInstance *pInstance, const CHString& name, BYTE *&bArray, DWORD &dwSzArray)
  104. {
  105. SAFEARRAY *saVariable;
  106. SAFEARRAYBOUND saVector;
  107. BOOL bRet = FALSE ;
  108. if (pInstance == NULL)
  109. return FALSE;
  110. IWbemClassObject *wbemClassObject = pInstance->GetClassObjectInterface();
  111. if ( wbemClassObject )
  112. {
  113. VARIANT v;
  114. VariantInit(&v);
  115. BSTR pName = name.AllocSysString();
  116. HRESULT hr = wbemClassObject->Get(pName, 0, &v, NULL, NULL);
  117. SysFreeString(pName);
  118. if ( ( v.vt != VT_NULL) && (v.vt != (VT_UI1|VT_ARRAY)))
  119. {
  120. VariantClear(&v);
  121. return FALSE ;
  122. }
  123. if (bRet = SUCCEEDED(hr))
  124. {
  125. if ( v.vt != VT_NULL && v.parray != NULL )
  126. {
  127. SAFEARRAY *t_SafeArray = v.parray ;
  128. if ( SafeArrayGetDim ( t_SafeArray ) == 1 )
  129. {
  130. LONG t_Dimension = 1 ;
  131. LONG t_LowerBound ;
  132. SafeArrayGetLBound ( t_SafeArray , t_Dimension , & t_LowerBound ) ;
  133. LONG t_UpperBound ;
  134. SafeArrayGetUBound ( t_SafeArray , t_Dimension , & t_UpperBound ) ;
  135. dwSzArray = t_UpperBound - t_LowerBound + 1 ;
  136. bArray = (BYTE*)MIDL_user_allocate(dwSzArray * sizeof(BYTE));
  137. for ( LONG t_Index = t_LowerBound ; t_Index <= t_UpperBound ; t_Index ++ )
  138. {
  139. BYTE t_Element ;
  140. SafeArrayGetElement ( t_SafeArray , &t_Index , & t_Element ) ;
  141. bArray [ t_Index ] = t_Element ;
  142. }
  143. bRet = TRUE ;
  144. }
  145. }
  146. }
  147. VariantClear(&v);
  148. }
  149. return bRet ;
  150. }
  151. /*-----------------------CDHCP_Property class definition---------------------------------*/
  152. // constructor. An instance of this class has to be defined by the parameters below:
  153. CDHCP_Property::CDHCP_Property(
  154. const WCHAR *wsPropName, // the name of the property
  155. const PFN_PROPERTY_ACTION pfnActionGet, // the pointer to its GET function
  156. const PFN_PROPERTY_ACTION pfnActionSet) // the pointer to its SET function
  157. {
  158. m_pfnActionGet = pfnActionGet;
  159. m_pfnActionSet = pfnActionSet;
  160. if (wsPropName != NULL)
  161. {
  162. m_wsPropName = new WCHAR[wcslen(wsPropName) + 1]; // the name is copied into a local member variable
  163. wcscpy(m_wsPropName, wsPropName); // released upon destruction
  164. }
  165. else
  166. m_wsPropName = NULL;
  167. }
  168. CDHCP_Property::~CDHCP_Property()
  169. {
  170. if (m_wsPropName != NULL)
  171. delete m_wsPropName; // release the memory allocated for the property name
  172. }