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.

256 lines
8.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: M O D E M D I . C P P
  7. //
  8. // Contents: Modem coclass device installer hook.
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 7 May 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "ncreg.h"
  18. #include "ncsetup.h"
  19. HRESULT
  20. HrUpdateLegacyRasTapiDevices ();
  21. //+---------------------------------------------------------------------------
  22. //
  23. // Function: HrModemClassCoInstaller
  24. //
  25. // Purpose: Responds to co-class installer messages to install and remove
  26. // net modem adapters.
  27. //
  28. // Arguments:
  29. // dif [in] See SetupApi.
  30. // hdi [in]
  31. // pdeid [in]
  32. // pContext [inout]
  33. //
  34. // Returns: S_OK, SPAPI_E_DI_POSTPROCESSING_REQUIRED, or an error code.
  35. //
  36. // Author: shaunco 3 Aug 1997
  37. //
  38. // Notes:
  39. //
  40. HRESULT
  41. HrModemClassCoInstaller (
  42. IN DI_FUNCTION dif,
  43. IN HDEVINFO hdi,
  44. IN PSP_DEVINFO_DATA pdeid,
  45. IN OUT PCOINSTALLER_CONTEXT_DATA pContext)
  46. {
  47. HRESULT hr = S_OK;
  48. if (DIF_INSTALLDEVICE == dif)
  49. {
  50. // When we're called during preprocessing, indicated
  51. // we require postprocessing.
  52. //
  53. if (!pContext->PostProcessing)
  54. {
  55. // Documentation indicates it, so we'll assert it.
  56. AssertSz (NO_ERROR == pContext->InstallResult,
  57. "HrModemClassCoInstaller: Bug in SetupApi! "
  58. "InstallResult should be NO_ERROR.");
  59. // Make sure they wouldn't loose our context info
  60. // even if we used it.
  61. #ifdef DBG
  62. pContext->PrivateData = NULL;
  63. #endif // DBG
  64. hr = SPAPI_E_DI_POSTPROCESSING_REQUIRED;
  65. }
  66. else
  67. {
  68. // Check out "context info" to make sure they didn't
  69. // touch it.
  70. //
  71. AssertSz (!pContext->PrivateData, "HrModemClassCoInstaller: "
  72. "Bug in SetupApi! You sunk my battleship! "
  73. "(I mean, you trashed my PrivateData)");
  74. // We are now in the postprocessing phase.
  75. // We will install a virtual network adapter for
  76. // the modem that was just installed but only if
  77. // it was installed successfully.
  78. //
  79. // We should have handled this case back in ModemClassCoInstaller.
  80. //
  81. AssertSz (NO_ERROR == pContext->InstallResult,
  82. "HrModemClassCoInstaller: Bug in ModemClassCoInstaller! "
  83. "InstallResult should be NO_ERROR or we would have "
  84. "returned immediately.");
  85. hr = S_OK;
  86. }
  87. }
  88. else if (DIF_REMOVE == dif)
  89. {
  90. // We're not going to fail remove operations. It's bad enough
  91. // when a user can't add a modem. It pisses them off to no end
  92. // if they can't remove them.
  93. //
  94. hr = S_OK;
  95. }
  96. else if (DIF_DESTROYPRIVATEDATA == dif)
  97. {
  98. (VOID) HrUpdateLegacyRasTapiDevices ();
  99. }
  100. TraceError ("HrModemClassCoInstaller",
  101. (SPAPI_E_DI_POSTPROCESSING_REQUIRED == hr) ? S_OK : hr);
  102. return hr;
  103. }
  104. //+---------------------------------------------------------------------------
  105. //
  106. // Function: HrUpdateLegacyRasTapiDevices
  107. //
  108. // Purpose: Legacy applications such as HPC Explorer 1.1 require
  109. // that modems that are "enabled" for RAS use be specified
  110. // under HKLM\Software\Microsoft\Ras\Tapi Devices\Unimodem.
  111. // The values that exist under these keys are multi-sz's of
  112. // COM ports, Friendly names, and Usage. This routine sets
  113. // those keys corresponding to all modems present on the
  114. // machine (and active in this HW profile).
  115. //
  116. // Arguments:
  117. // (none)
  118. //
  119. // Returns: S_OK or an error code.
  120. //
  121. // Author: shaunco 19 Mar 1998
  122. //
  123. // Notes:
  124. //
  125. HRESULT
  126. HrUpdateLegacyRasTapiDevices ()
  127. {
  128. // Keep lists of strings that we will write as multi-sz to
  129. // HKLM\Software\Microsoft\Ras\Tapi Devices\Unimodem.
  130. //
  131. list<tstring*> lstrAddress;
  132. list<tstring*> lstrFriendlyName;
  133. list<tstring*> lstrUsage;
  134. // Get all of the installed modems.
  135. //
  136. HDEVINFO hdi;
  137. HRESULT hr = HrSetupDiGetClassDevs (&GUID_DEVCLASS_MODEM, NULL,
  138. NULL, DIGCF_PRESENT | DIGCF_PROFILE, &hdi);
  139. if (SUCCEEDED(hr))
  140. {
  141. // Declare these outside the while loop to avoid construction
  142. // destruction at each iteration.
  143. //
  144. tstring strAttachedTo;
  145. tstring strFriendlyName;
  146. // Enumerate the devices and open their dev reg key.
  147. //
  148. DWORD dwIndex = 0;
  149. SP_DEVINFO_DATA deid;
  150. while (SUCCEEDED(hr = HrSetupDiEnumDeviceInfo (hdi, dwIndex++, &deid)))
  151. {
  152. // Try to open the registry key for this modem. If it fails,
  153. // ignore and move on to the next.
  154. //
  155. HKEY hkey;
  156. hr = HrSetupDiOpenDevRegKey(hdi, &deid,
  157. DICS_FLAG_GLOBAL, 0, DIREG_DRV,
  158. KEY_READ, &hkey);
  159. if (SUCCEEDED(hr))
  160. {
  161. // Get the AttachedTo and FriendlyName values for the modem.
  162. // PnPAttachedTo will be present for PnP modems.
  163. //
  164. static const WCHAR c_szModemAttachedTo [] = L"AttachedTo";
  165. static const WCHAR c_szModemPnPAttachedTo[] = L"PnPAttachedTo";
  166. static const WCHAR c_szModemFriendlyName [] = L"FriendlyName";
  167. static const WCHAR c_szUsage [] = L"ClientAndServer";
  168. // Look for PnPAttached to first, then fallback to AttachedTo
  169. // if it failed.
  170. //
  171. hr = HrRegQueryString (hkey, c_szModemPnPAttachedTo,
  172. &strAttachedTo);
  173. if (FAILED(hr))
  174. {
  175. hr = HrRegQueryString (hkey, c_szModemAttachedTo,
  176. &strAttachedTo);
  177. }
  178. if (SUCCEEDED(hr))
  179. {
  180. hr = HrRegQueryString (hkey, c_szModemFriendlyName,
  181. &strFriendlyName);
  182. if (SUCCEEDED(hr))
  183. {
  184. // Add them to our lists.
  185. lstrAddress .push_back (new tstring (strAttachedTo));
  186. lstrFriendlyName.push_back (new tstring (strFriendlyName));
  187. lstrUsage .push_back (new tstring (c_szUsage));
  188. }
  189. }
  190. RegCloseKey (hkey);
  191. }
  192. }
  193. if (HRESULT_FROM_WIN32(ERROR_NO_MORE_ITEMS) == hr)
  194. {
  195. hr = S_OK;
  196. }
  197. SetupDiDestroyDeviceInfoList (hdi);
  198. }
  199. // Now save the lists as multi-sz's.
  200. //
  201. static const WCHAR c_szRegKeyLegacyRasUnimodemTapiDevices[]
  202. = L"Software\\Microsoft\\Ras\\Tapi Devices\\Unimodem";
  203. HKEY hkey;
  204. hr = HrRegCreateKeyEx (HKEY_LOCAL_MACHINE,
  205. c_szRegKeyLegacyRasUnimodemTapiDevices,
  206. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hkey, NULL);
  207. if (SUCCEEDED(hr))
  208. {
  209. static const WCHAR c_szRegValAddress [] = L"Address";
  210. static const WCHAR c_szRegValFriendlyName [] = L"Friendly Name";
  211. static const WCHAR c_szRegValUsage [] = L"Usage";
  212. (VOID) HrRegSetColString (hkey, c_szRegValAddress, lstrAddress);
  213. (VOID) HrRegSetColString (hkey, c_szRegValFriendlyName, lstrFriendlyName);
  214. (VOID) HrRegSetColString (hkey, c_szRegValUsage, lstrUsage);
  215. static const WCHAR c_szRegValMediaType [] = L"Media Type";
  216. static const WCHAR c_szRegValModem [] = L"Modem";
  217. (VOID) HrRegSetSz (hkey, c_szRegValMediaType, c_szRegValModem);
  218. RegCloseKey (hkey);
  219. }
  220. FreeCollectionAndItem (lstrUsage);
  221. FreeCollectionAndItem (lstrFriendlyName);
  222. FreeCollectionAndItem (lstrAddress);
  223. TraceError ("HrUpdateLegacyRasTapiDevices", hr);
  224. return hr;
  225. }