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.

364 lines
9.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: M A I N . C P P
  7. //
  8. // Contents: Code to provide a simple cmdline interface to
  9. // the sample code functions
  10. //
  11. // Notes: The code in this file is not required to access any
  12. // netcfg functionality. It merely provides a simple cmdline
  13. // interface to the sample code functions provided in
  14. // file snetcfg.cpp.
  15. //
  16. // Author: kumarp 28-September-98
  17. //
  18. //----------------------------------------------------------------------------
  19. #include "pch.h"
  20. #pragma hdrstop
  21. #include "snetcfg.h"
  22. #include "error.h"
  23. #include <wbemcli.h>
  24. #include <winnls.h>
  25. #include "tracelog.h"
  26. BOOL g_fVerbose = FALSE;
  27. BOOL WlbsCheckSystemVersion ();
  28. BOOL WlbsCheckFiles ();
  29. HRESULT WlbsRegisterDlls ();
  30. HRESULT WlbsCompileMof ();
  31. // ----------------------------------------------------------------------
  32. //
  33. // Function: wmain
  34. //
  35. // Purpose: The main function
  36. //
  37. // Arguments: standard main args
  38. //
  39. // Returns: 0 on success, non-zero otherwise
  40. //
  41. // Author: kumarp 25-December-97
  42. //
  43. // Notes:
  44. //
  45. EXTERN_C int __cdecl wmain (int argc, WCHAR * argv[]) {
  46. HRESULT hr = S_OK;
  47. WCHAR ch;
  48. enum NetClass nc = NC_Unknown;
  49. WCHAR szFileFullPath[MAX_PATH+1];
  50. WCHAR szFileFullPathDest[MAX_PATH+1];
  51. PWCHAR pwc;
  52. PWSTR szFileComponent;
  53. TRACELogRegister(L"wlbs");
  54. LOG_INFO("Checking Windows version information.");
  55. if (!WlbsCheckSystemVersion()) {
  56. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_INVALID_OS);
  57. LOG_ERROR("The NLB install pack can only be used on Windows 2000 Server Service Pack 1 or higher.");
  58. goto error;
  59. }
  60. LOG_INFO("Checking for previous NLB installations.");
  61. hr = FindIfComponentInstalled(_TEXT("ms_wlbs"));
  62. if (hr == S_OK) {
  63. /* AppCenter request on 1.9.01 to revert to S_FALSE in this case due to RTM proximity. */
  64. /* hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_ALREADY_INSTALLED); */
  65. hr = S_FALSE;
  66. LOG_ERROR("Network Load Balancing Service is already installed.");
  67. goto error;
  68. }
  69. if (FAILED(hr)) {
  70. LOG_ERROR("Warning: Error querying for Network Load Balancing Service. There may be errors in this installtion.");
  71. }
  72. LOG_INFO("Checking for necessary files.");
  73. if (!WlbsCheckFiles()) {
  74. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_FILES_MISSING);
  75. LOG_ERROR1("Please install the NLB hotfix before running %ls", argv[0]);
  76. goto error;
  77. }
  78. GetModuleFileName(NULL, szFileFullPath, MAX_PATH + 1);
  79. pwc = wcsrchr(szFileFullPath, L'\\');
  80. * (pwc + 1) = L'\0';
  81. wcscat(szFileFullPath, L"netwlbs.inf");
  82. LOG_INFO("Checking language version.");
  83. switch (GetSystemDefaultLangID()) {
  84. case 0x0409: case 0x0809: case 0x0c09: case 0x1009: case 0x1409: case 0x1809: case 0x1c09:
  85. case 0x2009: case 0x2409: case 0x2809: case 0x2c09:
  86. LOG_INFO1("English version detected 0x%x.", GetSystemDefaultLangID());
  87. wcscat (szFileFullPath, L".eng");
  88. break;
  89. case 0x0411:
  90. LOG_INFO1("Japanese version detected 0x%x.", GetSystemDefaultLangID());
  91. wcscat (szFileFullPath, L".jpn");
  92. break;
  93. case 0x0407: case 0x0807: case 0x0c07: case 0x1007: case 0x1407:
  94. LOG_INFO1("German version detected 0x%x.", GetSystemDefaultLangID());
  95. wcscat (szFileFullPath, L".ger");
  96. break;
  97. case 0x040c: case 0x080c: case 0x0c0c: case 0x100c: case 0x140c:
  98. LOG_INFO1("French version detected 0x%x.", GetSystemDefaultLangID());
  99. wcscat (szFileFullPath, L".fr");
  100. break;
  101. default:
  102. LOG_INFO1("Unsupported Language.Please contact PSS for a new %ls.", argv[0]);
  103. wcscat (szFileFullPath, L".eng");
  104. break;
  105. }
  106. /* First copy the .inf file. */
  107. GetWindowsDirectory(szFileFullPathDest, MAX_PATH + 1);
  108. wcscat(szFileFullPathDest, L"\\INF\\netwlbs.inf");
  109. LOG_INFO("Copying the NLB .inf file.");
  110. if (!CopyFile(szFileFullPath, szFileFullPathDest, FALSE)) {
  111. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_INF_FAILURE);
  112. LOG_ERROR2("Warning: Unable to copy the inf file %ls %ls.", szFileFullPath, szFileFullPathDest);
  113. goto error;
  114. }
  115. /* Now install the service. */
  116. hr = HrInstallNetComponent(L"ms_wlbs", NC_NetService, szFileFullPathDest);
  117. if (!SUCCEEDED(hr)) {
  118. LOG_ERROR("Error installing Network Load Balancing.");
  119. goto error;
  120. } else {
  121. LOG_INFO("Installation of Network Load Balancing done.");
  122. }
  123. /* Change working directory to %TEMP%. Needed because of a IMofCopiler::CompileFile error on Win2K. */
  124. WCHAR * szTempDir = _wgetenv(L"TEMP");
  125. _wchdir(szTempDir);
  126. LOG_INFO("Registering NLB Dlls.");
  127. /* Register the provider .dll here. */
  128. hr = WlbsRegisterDlls();
  129. if (!SUCCEEDED(hr)) {
  130. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_REGISTER_DLL);
  131. LOG_ERROR("Error registering NLB Dlls.");
  132. goto error;
  133. }
  134. LOG_INFO("Compiling the NLB MOF.");
  135. /* Compile wlbsprov.mof here */
  136. hr = WlbsCompileMof();
  137. if (!SUCCEEDED(hr)) {
  138. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_COMPILE_MOF);
  139. LOG_ERROR("Error compiling the NLB MOF.");
  140. goto error;
  141. }
  142. LOG_INFO("WLBS Setup successful.");
  143. return hr;
  144. error:
  145. LOG_ERROR1("WLBS Setup failed 0x%x", hr);
  146. return hr;
  147. }
  148. /* This checks whether the system on which NLB is being installed is a W2K Server or not. */
  149. BOOL WlbsCheckSystemVersion () {
  150. OSVERSIONINFOEX osinfo;
  151. osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  152. if (!GetVersionEx((LPOSVERSIONINFO)&osinfo)) return FALSE;
  153. /* For install, we return TRUE only if its Windows 2000 Server. */
  154. if ((osinfo.dwMajorVersion == 5) &&
  155. (osinfo.dwMinorVersion == 0) &&
  156. (osinfo.wProductType == VER_NT_SERVER) &&
  157. !(osinfo.wSuiteMask & VER_SUITE_ENTERPRISE) &&
  158. !(osinfo.wSuiteMask & VER_SUITE_DATACENTER))
  159. return TRUE;
  160. return FALSE;
  161. }
  162. BOOL WlbsCheckFiles () {
  163. WCHAR * FileList [] = {
  164. L"\\system32\\drivers\\wlbs.sys",
  165. L"\\system32\\wlbs.exe",
  166. L"\\help\\wlbs.chm",
  167. L"\\help\\wlbs.hlp",
  168. L"\\system32\\wlbsctrl.dll",
  169. L"\\system32\\wbem\\wlbsprov.dll",
  170. L"\\system32\\wbem\\wlbsprov.mof",
  171. L"\\system32\\wbem\\wlbsprov.mfl",
  172. L"\\inf\\netwlbsm.inf",
  173. NULL
  174. };
  175. WCHAR wszPath [MAX_PATH + 1];
  176. PWCHAR pwc;
  177. INT i = 0;
  178. BOOL first = FALSE;
  179. WIN32_FIND_DATA FileFind;
  180. HANDLE hdl;
  181. while (FileList [i] != NULL) {
  182. GetWindowsDirectory(wszPath, MAX_PATH + 1);
  183. wcscat(wszPath, FileList [i]);
  184. hdl = FindFirstFile(wszPath, & FileFind);
  185. if (hdl == INVALID_HANDLE_VALUE) {
  186. if (!first) {
  187. first = TRUE;
  188. LOG_ERROR("Error: The following files were not found:");
  189. }
  190. LOG_ERROR1("%\tls",wszPath);
  191. }
  192. if (hdl != INVALID_HANDLE_VALUE)
  193. FindClose(hdl);
  194. i++;
  195. }
  196. return !first;
  197. }
  198. HRESULT WlbsRegisterDlls () {
  199. WCHAR * DllList [] = {
  200. L"\\wbem\\wlbsprov.dll",
  201. NULL
  202. };
  203. INT i = 0;
  204. WCHAR pszDllPath [MAX_PATH + 1];
  205. HINSTANCE hLib;
  206. CHAR * pszDllEntryPoint = "DllRegisterServer";
  207. HRESULT (STDAPICALLTYPE * lpDllEntryPoint)(void);
  208. HRESULT hr = S_OK;
  209. if (!GetSystemDirectory(pszDllPath, MAX_PATH + 1)) {
  210. hr = E_UNEXPECTED;
  211. LOG_ERROR("GetSystemDirectoryFailed.");
  212. return hr;
  213. }
  214. wcscat(pszDllPath, DllList [0]);
  215. if (FAILED(hr = OleInitialize(NULL))) {
  216. LOG_ERROR("OleInitialize Failed.");
  217. return hr;
  218. }
  219. hLib = LoadLibrary(pszDllPath);
  220. if (hLib == NULL) {
  221. hr = E_UNEXPECTED;
  222. LOG_ERROR("LoadLibrary for wlbsprov.dll Failed.");
  223. goto CleanOle;
  224. }
  225. (FARPROC &)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint);
  226. if (lpDllEntryPoint == NULL) {
  227. hr = E_UNEXPECTED;
  228. LOG_ERROR("DllRegisterServer was not found.");
  229. goto CleanLib;
  230. }
  231. if (FAILED(hr = (*lpDllEntryPoint)())) {
  232. LOG_ERROR("DllRegisterServer failed.");
  233. goto CleanLib;
  234. }
  235. LOG_INFO("Dll Registration Succeeded.");
  236. CleanLib:
  237. FreeLibrary(hLib);
  238. CleanOle:
  239. OleUninitialize();
  240. return hr;
  241. }
  242. HRESULT WlbsCompileMof () {
  243. WCHAR * MofList [] = {
  244. L"\\wbem\\wlbsprov.mof",
  245. NULL
  246. };
  247. IMofCompiler * pMofComp = NULL;
  248. WBEM_COMPILE_STATUS_INFO Info;
  249. HRESULT hr = S_OK;
  250. WCHAR pszMofPath [MAX_PATH + 1];
  251. if (!GetSystemDirectory(pszMofPath, MAX_PATH + 1)) {
  252. hr = E_UNEXPECTED;
  253. LOG_ERROR("GetSystemDirectoryFailed.");
  254. return hr;
  255. }
  256. wcscat(pszMofPath, MofList [0]);
  257. hr = CoInitialize(NULL);
  258. if (FAILED(hr)) {
  259. LOG_ERROR("CoInitialize failed.");
  260. return hr;
  261. }
  262. hr = CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (LPVOID *)&pMofComp);
  263. if (FAILED(hr)) {
  264. LOG_ERROR("CoCreateInstance Failed.");
  265. switch (hr) {
  266. case REGDB_E_CLASSNOTREG:
  267. LOG_ERROR("Not registered.");
  268. break;
  269. case CLASS_E_NOAGGREGATION:
  270. LOG_ERROR("No aggregration.");
  271. break;
  272. default:
  273. LOG_ERROR1("Error ox%x.", hr);
  274. break;
  275. }
  276. CoUninitialize();
  277. return hr;
  278. }
  279. hr = pMofComp->CompileFile(pszMofPath, NULL, NULL, NULL, NULL, 0, 0, 0, &Info);
  280. if (hr != WBEM_S_NO_ERROR)
  281. LOG_ERROR("Compile Failed.");
  282. pMofComp->Release();
  283. CoUninitialize();
  284. return Info.hRes;
  285. }