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.

261 lines
7.8 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. main.c
  5. Abstract:
  6. This module contains the startup and termination code for the Configuration
  7. Manager (cfgmgr32).
  8. Author:
  9. Paula Tomlinson (paulat) 6-20-1995
  10. Environment:
  11. User mode only.
  12. Revision History:
  13. 3-Mar-1995 paulat
  14. Creation and initial implementation.
  15. --*/
  16. //
  17. // includes
  18. //
  19. #include "precomp.h"
  20. #pragma hdrstop
  21. #include "cfgi.h"
  22. //
  23. // global data
  24. //
  25. HANDLE hInst;
  26. PVOID hLocalStringTable = NULL; // handle to local string table
  27. PVOID hLocalBindingHandle = NULL; // rpc binding handle to local machine
  28. WORD LocalServerVersion = 0; // local machine internal server version
  29. WCHAR LocalMachineNameNetBIOS[MAX_PATH + 3];
  30. WCHAR LocalMachineNameDnsFullyQualified[MAX_PATH + 3];
  31. CRITICAL_SECTION BindingCriticalSection;
  32. CRITICAL_SECTION StringTableCriticalSection;
  33. BOOL
  34. CfgmgrEntry(
  35. PVOID hModule,
  36. ULONG Reason,
  37. PCONTEXT pContext
  38. )
  39. /*++
  40. Routine Description:
  41. This is the standard DLL entrypoint routine, called whenever a process
  42. or thread attaches or detaches.
  43. Arguments:
  44. hModule - PVOID parameter that specifies the handle of the DLL
  45. Reason - ULONG parameter that specifies the reason this entrypoint
  46. was called (either PROCESS_ATTACH, PROCESS_DETACH,
  47. THREAD_ATTACH, or THREAD_DETACH).
  48. pContext - Not used.
  49. (when cfgmgr32 is initialized by setupapi - as should almost
  50. always be the case - this is the 'Reserved' argument supplied to
  51. setupapi's DllMain entrypoint)
  52. Return value:
  53. Returns true if initialization completed successfully, false is not.
  54. --*/
  55. {
  56. UNREFERENCED_PARAMETER(pContext);
  57. hInst = (HANDLE)hModule;
  58. switch(Reason) {
  59. case DLL_PROCESS_ATTACH: {
  60. WCHAR szTemp[MAX_PATH + 1];
  61. ULONG ulSize;
  62. size_t len;
  63. //
  64. // InitializeCriticalSection may raise STATUS_NO_MEMORY exception
  65. //
  66. try {
  67. InitializeCriticalSection(&BindingCriticalSection);
  68. InitializeCriticalSection(&StringTableCriticalSection);
  69. } except(EXCEPTION_EXECUTE_HANDLER) {
  70. return FALSE;
  71. }
  72. //
  73. // save the NetBIOS name of the local machine for later use.
  74. // note that the size of the DNS computer name buffer is MAX_PATH+3,
  75. // which is actually much larger than MAX_COMPUTERNAME_LENGTH, the
  76. // max length returned for ComputerNameNetBIOS.
  77. //
  78. ulSize = SIZECHARS(szTemp);
  79. if(!GetComputerNameEx(ComputerNameNetBIOS, szTemp, &ulSize)) {
  80. //
  81. // ISSUE-2002/03/05-jamesca: Can we actually run w/o knowing
  82. // the local machine name???
  83. //
  84. *LocalMachineNameNetBIOS = L'\0';
  85. } else {
  86. if (FAILED(StringCchLength(
  87. szTemp,
  88. SIZECHARS(szTemp),
  89. &len))) {
  90. return FALSE;
  91. }
  92. //
  93. // always save local machine name in "\\name format"
  94. //
  95. if((len > 2) &&
  96. (szTemp[0] == L'\\') && (szTemp[1] == L'\\')) {
  97. //
  98. // The name is already in the correct format.
  99. //
  100. if (FAILED(StringCchCopy(
  101. LocalMachineNameNetBIOS,
  102. SIZECHARS(LocalMachineNameNetBIOS),
  103. szTemp))) {
  104. return FALSE;
  105. }
  106. } else {
  107. //
  108. // Prepend UNC path prefix
  109. //
  110. if (FAILED(StringCchCopy(
  111. LocalMachineNameNetBIOS,
  112. SIZECHARS(LocalMachineNameNetBIOS),
  113. L"\\\\"))) {
  114. return FALSE;
  115. }
  116. if (FAILED(StringCchCat(
  117. LocalMachineNameNetBIOS,
  118. SIZECHARS(LocalMachineNameNetBIOS),
  119. szTemp))) {
  120. return FALSE;
  121. }
  122. }
  123. }
  124. //
  125. // save the DNS name of the local machine for later use.
  126. // note that the size of the DNS computer name buffer is MAX_PATH+3,
  127. // which is actually larger than DNS_MAX_NAME_BUFFER_LENGTH, the max
  128. // length for ComputerNameDnsFullyQualified.
  129. //
  130. ulSize = SIZECHARS(szTemp);
  131. if(!GetComputerNameEx(ComputerNameDnsFullyQualified, szTemp, &ulSize)) {
  132. //
  133. // ISSUE-2002/03/05-jamesca: Can we actually run w/o knowing
  134. // the local machine name???
  135. //
  136. *LocalMachineNameDnsFullyQualified = L'\0';
  137. } else {
  138. if (FAILED(StringCchLength(
  139. szTemp,
  140. SIZECHARS(szTemp),
  141. &len))) {
  142. return FALSE;
  143. }
  144. //
  145. // always save local machine name in "\\name format"
  146. //
  147. if((len > 2) &&
  148. (szTemp[0] == L'\\') && (szTemp[1] == L'\\')) {
  149. //
  150. // The name is already in the correct format.
  151. //
  152. if (FAILED(StringCchCopy(
  153. LocalMachineNameDnsFullyQualified,
  154. SIZECHARS(LocalMachineNameDnsFullyQualified),
  155. szTemp))) {
  156. return FALSE;
  157. }
  158. } else {
  159. //
  160. // Prepend UNC path prefix
  161. //
  162. if (FAILED(StringCchCopy(
  163. LocalMachineNameDnsFullyQualified,
  164. SIZECHARS(LocalMachineNameDnsFullyQualified),
  165. L"\\\\"))) {
  166. return FALSE;
  167. }
  168. if (FAILED(StringCchCat(
  169. LocalMachineNameDnsFullyQualified,
  170. SIZECHARS(LocalMachineNameDnsFullyQualified),
  171. szTemp))) {
  172. return FALSE;
  173. }
  174. }
  175. }
  176. break;
  177. }
  178. case DLL_PROCESS_DETACH:
  179. //
  180. // release the rpc binding for the local machine
  181. //
  182. if (hLocalBindingHandle != NULL) {
  183. PNP_HANDLE_unbind(NULL, (handle_t)hLocalBindingHandle);
  184. hLocalBindingHandle = NULL;
  185. }
  186. //
  187. // release the string table for the local machine
  188. //
  189. if (hLocalStringTable != NULL) {
  190. pSetupStringTableDestroy(hLocalStringTable);
  191. hLocalStringTable = NULL;
  192. }
  193. DeleteCriticalSection(&BindingCriticalSection);
  194. DeleteCriticalSection(&StringTableCriticalSection);
  195. break;
  196. case DLL_THREAD_ATTACH:
  197. case DLL_THREAD_DETACH:
  198. break;
  199. }
  200. return TRUE;
  201. } // CfgmgrEntry