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.

240 lines
8.3 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <tchar.h>
  6. extern "C" {
  7. #include <wsasetup.h>
  8. }
  9. /////////////////////////////////////////////////////////////////////////////
  10. //++
  11. //
  12. // RemoveNetworkProvider
  13. //
  14. // Routine Description:
  15. // This routine uninstalls the Clustering Service Network Provider.
  16. //
  17. // Arguments:
  18. // None
  19. //
  20. // Return Value:
  21. // (DWORD) ERROR_SUCCESS - indicates success
  22. // Any other value is a Win32 error code.
  23. //
  24. // Note:
  25. // This function was adapted from removeNetworkProvider in the NT 4.0
  26. // Cluster "setup" program.
  27. //
  28. //--
  29. /////////////////////////////////////////////////////////////////////////////
  30. DWORD RemoveNetworkProvider( void )
  31. {
  32. DWORD dwReturnValue;
  33. LONG lReturnValue;
  34. //
  35. // The inf file will take care of deleting the clusnet winsock key.
  36. // We just need to delete clusnet from the list of winsock transports.
  37. //
  38. //
  39. // Open the winsock service parameters key.
  40. //
  41. HKEY hWinsockParametersRegKey;
  42. lReturnValue = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  43. TEXT("System\\CurrentControlSet\\Services\\WinSock\\Parameters"),
  44. (DWORD) 0L, // reserved
  45. MAXIMUM_ALLOWED,
  46. (PHKEY) &hWinsockParametersRegKey );
  47. dwReturnValue = (DWORD) lReturnValue;
  48. if ( lReturnValue == (LONG) ERROR_SUCCESS )
  49. {
  50. //
  51. // Update the winsock transports list.
  52. //
  53. // Allocate memory into which to read the Winsock Transports List.
  54. LPBYTE pWinsockTransportsList;
  55. DWORD dwWinsockTransportsListSize = 200; // arbitrary size
  56. pWinsockTransportsList = (LPBYTE) LocalAlloc( LPTR, dwWinsockTransportsListSize );
  57. // Was the buffer for the Winsock Transports List allocated?
  58. if ( pWinsockTransportsList != (LPBYTE) NULL )
  59. {
  60. // Read the Winsock Transports List from the registry.
  61. DWORD dwRegKeyType;
  62. lReturnValue = RegQueryValueEx( hWinsockParametersRegKey,
  63. TEXT("Transports"),
  64. (LPDWORD) NULL,
  65. &dwRegKeyType,
  66. pWinsockTransportsList,
  67. &dwWinsockTransportsListSize );
  68. // Was the Winsock Transports List read on the first attempt?
  69. if ( lReturnValue != (LONG) ERROR_SUCCESS )
  70. {
  71. // Was the buffer too small?
  72. if ( lReturnValue == (LONG) ERROR_MORE_DATA )
  73. {
  74. // The Winsock Transports List was not read form the registry because
  75. // the buffer was too small. Increase the size of the buffer.
  76. pWinsockTransportsList = (LPBYTE) LocalReAlloc( pWinsockTransportsList,
  77. (UINT) dwWinsockTransportsListSize,
  78. (UINT) LMEM_ZEROINIT );
  79. // Was the buffer reallocation successfull?
  80. if ( pWinsockTransportsList != (LPBYTE) NULL )
  81. {
  82. // Attempt to read the Wincosk Transports List a second time.
  83. lReturnValue = RegQueryValueEx( hWinsockParametersRegKey,
  84. TEXT("Transports"),
  85. (LPDWORD) NULL,
  86. &dwRegKeyType,
  87. pWinsockTransportsList,
  88. &dwWinsockTransportsListSize );
  89. // Was the Winsock Transports List read on the second attempt?
  90. if ( lReturnValue != (LONG) ERROR_SUCCESS )
  91. {
  92. dwReturnValue = (DWORD) lReturnValue;
  93. } // Was the Winsock Transports List read on the second attempt?
  94. } // Was the buffer reallocation successfull?
  95. else
  96. {
  97. dwReturnValue = GetLastError();
  98. } // Was the buffer reallocation successfull?
  99. } // Was the buffer too small?
  100. } // Was the Winsock Transports List read on the first attempt?
  101. // At this point variable lReturnValue indicates whether the Winsock
  102. // Transports List was read from the registry.
  103. if ( lReturnValue == (LONG) ERROR_SUCCESS )
  104. {
  105. // Is the type of the registry value correct?
  106. if ( dwRegKeyType == (DWORD) REG_MULTI_SZ )
  107. {
  108. LPTSTR ptszOldTransportsList;
  109. ptszOldTransportsList = (LPTSTR) pWinsockTransportsList;
  110. LPTSTR ptszNewTransportList;
  111. ptszNewTransportList = (LPTSTR) LocalAlloc( LMEM_FIXED, dwWinsockTransportsListSize );
  112. // Was the buffer for the new Winsock Transports List allocated successfully?
  113. if ( ptszNewTransportList != (LPTSTR) NULL )
  114. {
  115. // Start building a list of Winsock Transports that does not include
  116. // the Clustering Service Network Provider.
  117. LPTSTR ptszNextTransport;
  118. ptszNextTransport = ptszNewTransportList;
  119. // Initialize the size of the new Winsock Transports List to zero.
  120. dwWinsockTransportsListSize = (DWORD) 0L;
  121. DWORD dwIndividualTransportLength;
  122. while ( *ptszOldTransportsList != UNICODE_NULL )
  123. {
  124. dwIndividualTransportLength = _tcslen(ptszOldTransportsList) + 1;
  125. // This comparison is case insensitive, like registry values.
  126. if ( _tcsicmp( ptszOldTransportsList, TEXT("ClusNet") ) != 0 )
  127. {
  128. _tcscpy( ptszNextTransport, ptszOldTransportsList );
  129. ptszNextTransport += dwIndividualTransportLength;
  130. dwWinsockTransportsListSize += dwIndividualTransportLength * sizeof(WCHAR);
  131. };
  132. ptszOldTransportsList += dwIndividualTransportLength;
  133. } // end of while loop
  134. *ptszNextTransport = UNICODE_NULL;
  135. dwWinsockTransportsListSize += sizeof( UNICODE_NULL );
  136. // Save the new Winsock Transports List in the registry.
  137. ASSERT( dwWinsockTransportsListSize != 0L );
  138. lReturnValue = RegSetValueEx( hWinsockParametersRegKey,
  139. TEXT("Transports"),
  140. NULL,
  141. dwRegKeyType,
  142. (CONST BYTE *) ptszNewTransportList,
  143. dwWinsockTransportsListSize );
  144. if ( lReturnValue == (LONG) ERROR_SUCCESS )
  145. {
  146. //
  147. // Poke winsock to update the Winsock2 config
  148. //
  149. WSA_SETUP_DISPOSITION disposition;
  150. dwReturnValue = MigrateWinsockConfiguration( &disposition, NULL, NULL );
  151. }
  152. else
  153. {
  154. dwReturnValue = (DWORD) lReturnValue;
  155. }
  156. // Free the buffer for the new Winsock Transports List.
  157. LocalFree( ptszNewTransportList );
  158. }
  159. else
  160. {
  161. dwReturnValue = GetLastError();
  162. } // Was the buffer for the new Winsock Transports List allocated successfully?
  163. }
  164. else
  165. {
  166. dwReturnValue = ERROR_INVALID_PARAMETER;
  167. } // Is the type of the registry value correct?
  168. } // Did one of the attempts to read the Winsock Transports List succeed?
  169. // Free the buffer for the Winsock Transports List.
  170. if ( pWinsockTransportsList != (LPBYTE) NULL )
  171. {
  172. LocalFree( pWinsockTransportsList );
  173. }
  174. } // Was the buffer for the Winsock Transports List allocated?
  175. else
  176. {
  177. dwReturnValue = GetLastError();
  178. } // Was the buffer for the Winsock Transports List allocated?
  179. } // Was the Winsock Parameters key opened?
  180. return ( dwReturnValue );
  181. }