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.

226 lines
7.1 KiB

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // oemconfigdll.cpp
  7. //
  8. // Description:
  9. // Implementation of GetUniqueSuffix function
  10. // which returns a unique suffix to the caller
  11. //
  12. // Author:
  13. // Alp Onalan Created: Oct 6 2000
  14. //
  15. //////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "..\saconfig\saconfigcommon.h"
  18. #include <atlimpl.cpp>
  19. const WCHAR REGSTR_VAL_WMICLASSNAME[]=L"WMIClassName";
  20. const WCHAR REGSTR_VAL_WMIPROPERTYNAME[]=L"WMIPropertyName";
  21. const WCHAR WSZ_NAMESPACE[]=L"\\\\.\\root\\cimv2";
  22. #define BLOCKSIZE (32 * sizeof(WCHAR))
  23. #define CVTBUFSIZE (309+40) /* # of digits in max. dp value + slop (this size stolen from cvt.h in c runtime library) */
  24. bool GetWMISettings(WCHAR *wszWMIClassName,WCHAR *wszWMIPropertyName)
  25. {
  26. bool fRes=true;
  27. DWORD dwSize=0;
  28. CRegKey hConfigKey;
  29. LONG lRes=0;
  30. do{
  31. lRes=hConfigKey.Open(HKEY_LOCAL_MACHINE,
  32. REGKEY_SACONFIG,
  33. KEY_READ);
  34. if(lRes!=ERROR_SUCCESS)
  35. {
  36. SATracePrintf("Unable to open saconfig regkey, lRes= %x", lRes);
  37. fRes=false;
  38. break;
  39. }
  40. lRes=hConfigKey.QueryValue(NULL,REGSTR_VAL_WMICLASSNAME,&dwSize);
  41. lRes=hConfigKey.QueryValue(wszWMIClassName,REGSTR_VAL_WMICLASSNAME,&dwSize);
  42. if(lRes!=ERROR_SUCCESS)
  43. {
  44. SATracePrintf("Unable to query WMIClassName regkey lRes= %x",lRes);
  45. fRes=false;
  46. break;
  47. }
  48. lRes=hConfigKey.QueryValue(NULL,REGSTR_VAL_WMIPROPERTYNAME,&dwSize);
  49. lRes=hConfigKey.QueryValue(wszWMIPropertyName,REGSTR_VAL_WMIPROPERTYNAME,&dwSize);
  50. if(lRes!=ERROR_SUCCESS)
  51. {
  52. SATracePrintf("Unable to query WMIPropertyName regkey lRes= %x",lRes);
  53. fRes=false;
  54. break;
  55. }
  56. }while(false);
  57. if(hConfigKey.m_hKey)
  58. {
  59. hConfigKey.Close();
  60. }
  61. return fRes;
  62. }
  63. STDAPI GetUniqueSuffix (WCHAR *wszUniqueSuffix)
  64. {
  65. WCHAR *wszWMIClassName=new WCHAR[NAMELENGTH];
  66. WCHAR *wszWMIPropertyName=new WCHAR[NAMELENGTH];
  67. SATraceString("OEMDLL::GetUniqueSuffix called");
  68. HRESULT hRes=S_OK;
  69. hRes=CoInitialize(NULL);
  70. do
  71. {
  72. if(false==GetWMISettings(wszWMIClassName,wszWMIPropertyName))
  73. {
  74. SATracePrintf("OEMDLL::GetUniqueSuffix: GetWMISettings failed %x", hRes);
  75. break;
  76. }
  77. if(hRes!=S_OK)
  78. {
  79. // break;
  80. }
  81. hRes = CoInitializeSecurity( NULL, -1, NULL, NULL,
  82. RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
  83. RPC_C_IMP_LEVEL_IDENTIFY,
  84. NULL, EOAC_NONE, 0);
  85. if(hRes!=S_OK)
  86. {
  87. SATracePrintf("OEMDLL::GetUniqueSuffix: CoInitializeSecurity failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  88. //break;
  89. }
  90. //
  91. // Create an instance of the WbemLocator interface.
  92. //
  93. CComPtr<IWbemLocator> pIWbemLocator;
  94. hRes=CoCreateInstance(CLSID_WbemLocator,
  95. NULL,
  96. CLSCTX_INPROC_SERVER,
  97. IID_IWbemLocator,
  98. (LPVOID *) &pIWbemLocator);
  99. if(FAILED(hRes))
  100. {
  101. SATracePrintf("OEMDLL::GetUniqueSuffix: CoCreatInstance(IWbemLocator) failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  102. break;
  103. }
  104. //
  105. // Using the locator, connect to CIMOM in the given namespace.
  106. //
  107. BSTR bstrNamespace = SysAllocString(WSZ_NAMESPACE);
  108. CComPtr<IWbemServices> pIWbemServices;
  109. hRes=pIWbemLocator->ConnectServer(bstrNamespace,
  110. NULL, //using current account
  111. NULL, //using current password
  112. 0L, // locale
  113. 0L, // securityFlags
  114. NULL, // authority (domain for NTLM)
  115. NULL, // context
  116. &pIWbemServices);
  117. SysFreeString(bstrNamespace);
  118. if(hRes!=WBEM_S_NO_ERROR)
  119. {
  120. SATracePrintf("OEMDLL::GetUniqueSuffix: ConnectServer failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  121. break;
  122. }
  123. //
  124. //Create an enumeration of the object(s)
  125. //
  126. BSTR bstrClassName;
  127. bstrClassName = SysAllocString(wszWMIClassName);
  128. CComPtr<IEnumWbemClassObject> pEnum;
  129. hRes = pIWbemServices->CreateInstanceEnum(bstrClassName,
  130. WBEM_FLAG_SHALLOW,
  131. NULL,
  132. &pEnum);
  133. SysFreeString(bstrClassName);
  134. if (hRes!=WBEM_S_NO_ERROR)
  135. {
  136. SATracePrintf("OEMDLL::GetUniqueSuffix:CreateInstanceEnum failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  137. break;
  138. }
  139. //
  140. // Pull the object from the enumeration (there's only one)
  141. //
  142. ULONG uReturned = 1;
  143. CComPtr<IWbemClassObject> pWin32BIOS;
  144. hRes=pEnum -> Next( WBEM_INFINITE, // timeout value - this one is blocking
  145. 1, // return just one object
  146. &pWin32BIOS, // pointer to the object
  147. &uReturned); // number obtained: one or zero
  148. if (hRes!=WBEM_S_NO_ERROR)
  149. {
  150. SATracePrintf("OEMDLL::GetUniqueSuffix:pEnum->Next failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  151. break;
  152. }
  153. VARIANT varSerialNumber;
  154. VariantInit(&varSerialNumber);
  155. //
  156. // Get the desired property from the object
  157. //
  158. hRes = pWin32BIOS->Get(wszWMIPropertyName, 0L, &varSerialNumber, NULL, NULL);
  159. if (hRes!=WBEM_S_NO_ERROR)
  160. {
  161. SATracePrintf("OEMDLL::GetUniqueSuffix:pWin32BIOS->Get failed, hRes= %x,getlasterr= %x", hRes,GetLastError());
  162. break;
  163. }
  164. //ValueToString(&varSerialNumber,&wszUniqueSuffix);
  165. if( VT_BSTR != V_VT(&varSerialNumber))
  166. {
  167. SATraceString("OEMDLL:WMI property is not CIM_STRING.Currently other types are not supported");
  168. break;
  169. }
  170. BSTR bstrSerialNumber=SysAllocString(V_BSTR(&varSerialNumber));
  171. wcscpy(wszUniqueSuffix,bstrSerialNumber);
  172. SysFreeString(bstrSerialNumber);
  173. VariantClear(&varSerialNumber);
  174. }while(false);
  175. CoUninitialize();
  176. return hRes;
  177. }
  178. BOOL APIENTRY DllMain( HANDLE hModule,
  179. DWORD ul_reason_for_call,
  180. LPVOID lpReserved
  181. )
  182. {
  183. SATraceString("OEMConfigdll.dll loaded");
  184. return TRUE;
  185. }