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.

643 lines
15 KiB

  1. #include <windows.h>
  2. #include "netconn.h"
  3. #include "nconnwrap.h"
  4. #pragma warning(disable:4100) // unreferenced formal parameter
  5. #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
  6. #define ASSERT(a)
  7. BOOL WINAPI IsProtocolInstalledA(LPCSTR pszProtocolDeviceID, BOOL bExhaustive);
  8. HRESULT WINAPI InstallProtocolA(LPCSTR pszProtocol, HWND hwndParent, PROGRESS_CALLBACK pfnCallback, LPVOID pvCallbackParam);
  9. HRESULT WINAPI RemoveProtocolA(LPCSTR pszProtocol);
  10. BOOL WINAPI FindConflictingServiceA(LPCSTR pszWantService, NETSERVICE* pConflict);
  11. //
  12. // String helper class. Takes in a WCHAR string and converts it to multibyte.
  13. // Allocates and frees space if the string is long.
  14. //
  15. class CStrIn
  16. {
  17. public:
  18. CStrIn(LPCWSTR pwsz);
  19. ~CStrIn();
  20. operator char*() {return _psz;};
  21. private:
  22. LPSTR _psz;
  23. char _sz[128];
  24. };
  25. CStrIn::CStrIn(LPCWSTR pwsz)
  26. {
  27. if (pwsz)
  28. {
  29. //
  30. // If a string was passed in always return a string - even if it's
  31. // the empty string.
  32. //
  33. _psz = _sz;
  34. int cch = WideCharToMultiByte(CP_ACP, 0, pwsz, -1, _sz, ARRAYSIZE(_sz),
  35. NULL, NULL);
  36. //
  37. // If the conversion failed try to allocate a buffer to hold the
  38. // multibyte version of the string.
  39. //
  40. if (0 == cch)
  41. {
  42. *_sz = '\0';
  43. cch = WideCharToMultiByte(CP_ACP, 0, pwsz, -1, NULL, 0, NULL, NULL);
  44. if (cch)
  45. {
  46. _psz = new char[cch];
  47. if (_psz)
  48. {
  49. cch = WideCharToMultiByte(CP_ACP, 0, pwsz, -1, _psz, cch,
  50. NULL, NULL);
  51. if (0 == cch)
  52. {
  53. delete [] _psz;
  54. _psz = _sz;
  55. }
  56. }
  57. else
  58. {
  59. _psz = _sz;
  60. }
  61. }
  62. }
  63. }
  64. else
  65. {
  66. _psz = NULL;
  67. }
  68. }
  69. CStrIn::~CStrIn()
  70. {
  71. if (_psz != _sz && _psz)
  72. {
  73. delete [] _psz;
  74. }
  75. }
  76. //
  77. //
  78. //
  79. class CStrOut
  80. {
  81. public:
  82. CStrOut(LPWSTR psz, int cch);
  83. operator LPSTR() { return _sz;}
  84. operator int() { return ARRAYSIZE(_sz);}
  85. void Convert();
  86. private:
  87. LPWSTR _psz;
  88. int _cch;
  89. char _sz[MAX_PATH];
  90. };
  91. CStrOut::CStrOut(LPWSTR psz, int cch)
  92. {
  93. ASSERT(cch <= ARRAYSIZE(_sz));
  94. _psz = psz;
  95. _cch = cch;
  96. }
  97. void CStrOut::Convert()
  98. {
  99. if (_psz)
  100. {
  101. MultiByteToWideChar(CP_ACP, 0, _sz, -1, _psz, _cch);
  102. }
  103. }
  104. //
  105. //
  106. //
  107. class CStrsOut
  108. {
  109. public:
  110. CStrsOut(LPWSTR** pppsz) {_pStructW = pppsz;};
  111. operator LPSTR**() {return _pStructW ? &_pStructA : NULL;};
  112. int Convert(int cStrs);
  113. private:
  114. int ConvertStruct(LPSTR* pStructA, int nStrs, LPWSTR* pStructW, LPWSTR pszLast);
  115. DWORD SizeOfWideCharStruct(LPSTR* pStructA, int cStrs);
  116. DWORD SizeOfPointerArea(int cStrs);
  117. DWORD SizeOfWideCharStringArea(LPSTR* pStruct, int cStrs);
  118. LPSTR EndOfLastString(LPSTR* pStruct, int cStrs);
  119. LPSTR StartOfFirstString(LPSTR* pStruct);
  120. private:
  121. LPWSTR** _pStructW;
  122. LPSTR* _pStructA;
  123. };
  124. int CStrsOut::ConvertStruct(LPSTR* pStructA, int nStrs, LPWSTR* pStructW, LPWSTR pszLast)
  125. {
  126. ASSERT(pStructA);
  127. ASSERT(nStrs);
  128. ASSERT(pStructW);
  129. int nRet;
  130. LPWSTR pszCurrent = (LPWSTR)&pStructW[nStrs];
  131. for (nRet = 0; nRet < nStrs; nRet++)
  132. {
  133. pStructW[nRet] = pszCurrent;
  134. pszCurrent += MultiByteToWideChar(CP_ACP, 0, pStructA[nRet], -1,
  135. pszCurrent, pszLast - pszCurrent);
  136. }
  137. return nRet;
  138. }
  139. int CStrsOut::Convert(int cStrs)
  140. {
  141. int nRet;
  142. if (_pStructW)
  143. {
  144. nRet = 0;
  145. if (_pStructA)
  146. {
  147. DWORD cbStruct = SizeOfWideCharStruct(_pStructA, cStrs);
  148. *_pStructW = (LPWSTR*)NetConnAlloc(cbStruct);
  149. if (*_pStructW)
  150. {
  151. nRet = ConvertStruct(_pStructA, cStrs, *_pStructW,
  152. (LPWSTR)((BYTE*)*_pStructW + cbStruct));
  153. }
  154. NetConnFree(_pStructA);
  155. }
  156. else
  157. {
  158. *_pStructW = NULL;
  159. }
  160. }
  161. else
  162. {
  163. nRet = cStrs;
  164. }
  165. return nRet;
  166. }
  167. inline LPSTR CStrsOut::EndOfLastString(LPSTR* pStructA, int nStrs)
  168. {
  169. return pStructA[nStrs - 1] + lstrlenA(pStructA[nStrs - 1]);
  170. }
  171. inline LPSTR CStrsOut::StartOfFirstString(LPSTR* pStructA)
  172. {
  173. return *pStructA;
  174. }
  175. inline DWORD CStrsOut::SizeOfWideCharStringArea(LPSTR* pStructA, int nStrs)
  176. {
  177. return ((EndOfLastString(pStructA, nStrs) + 1) - StartOfFirstString(pStructA))
  178. * sizeof(WCHAR);
  179. }
  180. inline DWORD CStrsOut::SizeOfPointerArea(int nStrs)
  181. {
  182. return nStrs * sizeof(LPWSTR);
  183. }
  184. DWORD CStrsOut::SizeOfWideCharStruct(LPSTR* pStructA, int nStrs)
  185. {
  186. return SizeOfPointerArea(nStrs) + SizeOfWideCharStringArea(pStructA, nStrs);
  187. }
  188. //
  189. // Netservice helper class. Passes in an ansi NETSERVICE structure when
  190. // cast to NETSERVICEA. Copies the ansi structure to a unicode structure
  191. // coverting the strings.
  192. //
  193. #undef NETSERVICE
  194. class CNetServiceOut
  195. {
  196. public:
  197. CNetServiceOut(NETSERVICE* pNS) {_pNS = pNS;};
  198. operator NETSERVICEA*() {return _pNS ? &_NSA : NULL;};
  199. void Convert();
  200. private:
  201. NETSERVICEA _NSA;
  202. NETSERVICE* _pNS;
  203. };
  204. void CNetServiceOut::Convert()
  205. {
  206. if (_pNS)
  207. {
  208. MultiByteToWideChar(CP_ACP, 0, _NSA.szClassKey, -1, _pNS->szClassKey,
  209. ARRAYSIZE(_pNS->szClassKey));
  210. MultiByteToWideChar(CP_ACP, 0, _NSA.szDeviceID, -1, _pNS->szDeviceID,
  211. ARRAYSIZE(_pNS->szDeviceID));
  212. MultiByteToWideChar(CP_ACP, 0, _NSA.szDisplayName, -1, _pNS->szDisplayName,
  213. ARRAYSIZE(_pNS->szDisplayName));
  214. }
  215. }
  216. //
  217. //
  218. //
  219. #undef NETADAPTER
  220. class CNetAdaptersOut
  221. {
  222. public:
  223. CNetAdaptersOut(NETADAPTER** ppNA) {_ppNA = ppNA;};
  224. operator NETADAPTERA**(){return _ppNA ? &_pNAA : NULL;};
  225. int Convert(int cNAA);
  226. private:
  227. void ConvertNA(NETADAPTERA* pNAA, NETADAPTER* pNA);
  228. private:
  229. NETADAPTERA* _pNAA;
  230. NETADAPTER** _ppNA;
  231. };
  232. int CNetAdaptersOut::Convert(int cNAA)
  233. {
  234. int nRet = 0;
  235. *_ppNA = NULL;
  236. if (cNAA > 0)
  237. {
  238. if (_pNAA)
  239. {
  240. *_ppNA = (NETADAPTER*)NetConnAlloc(sizeof(NETADAPTER) * cNAA);
  241. if (*_ppNA)
  242. {
  243. for (nRet = 0; nRet < cNAA; nRet++)
  244. {
  245. ConvertNA(&_pNAA[nRet], &(*_ppNA)[nRet]);
  246. }
  247. NetConnFree(_pNAA);
  248. }
  249. }
  250. }
  251. return nRet;
  252. }
  253. void CNetAdaptersOut::ConvertNA(NETADAPTERA* pNAA, NETADAPTER* pNA)
  254. {
  255. ASSERT(pNAA);
  256. ASSERT(pNA);
  257. MultiByteToWideChar(CP_ACP, 0, pNAA->szDisplayName, -1, pNA->szDisplayName,
  258. ARRAYSIZE(pNA->szDisplayName));
  259. MultiByteToWideChar(CP_ACP, 0, pNAA->szDeviceID, -1, pNA->szDeviceID,
  260. ARRAYSIZE(pNA->szDeviceID));
  261. MultiByteToWideChar(CP_ACP, 0, pNAA->szEnumKey, -1, pNA->szEnumKey,
  262. ARRAYSIZE(pNA->szEnumKey));
  263. MultiByteToWideChar(CP_ACP, 0, pNAA->szClassKey, -1, pNA->szClassKey,
  264. ARRAYSIZE(pNA->szClassKey));
  265. MultiByteToWideChar(CP_ACP, 0, pNAA->szManufacturer, -1, pNA->szManufacturer,
  266. ARRAYSIZE(pNA->szManufacturer));
  267. MultiByteToWideChar(CP_ACP, 0, pNAA->szInfFileName, -1, pNA->szInfFileName,
  268. ARRAYSIZE(pNA->szInfFileName));
  269. pNA->bNicType = pNAA->bNicType;
  270. pNA->bNetType = pNAA->bNetType;
  271. pNA->bNetSubType = pNAA->bNetSubType;
  272. pNA->bIcsStatus = pNAA->bIcsStatus;
  273. pNA->bError = pNAA->bError;
  274. pNA->bWarning = pNAA->bWarning;
  275. pNA->devnode = pNAA->devnode;
  276. }
  277. //
  278. //
  279. //
  280. class CNetAdapterIn
  281. {
  282. public:
  283. CNetAdapterIn(const NETADAPTER* pNA);
  284. operator NETADAPTERA*() {return _pNA ? &_NAA : NULL;};
  285. private:
  286. const NETADAPTER* _pNA;
  287. NETADAPTERA _NAA;
  288. };
  289. CNetAdapterIn::CNetAdapterIn(const NETADAPTER* pNA)
  290. {
  291. _pNA = pNA;
  292. if (pNA)
  293. {
  294. WideCharToMultiByte(CP_ACP, 0, pNA->szDisplayName, -1, _NAA.szDisplayName,
  295. ARRAYSIZE(_NAA.szDisplayName), NULL, NULL);
  296. WideCharToMultiByte(CP_ACP, 0, pNA->szDeviceID, -1, _NAA.szDeviceID,
  297. ARRAYSIZE(_NAA.szDeviceID), NULL, NULL);
  298. WideCharToMultiByte(CP_ACP, 0, pNA->szEnumKey, -1, _NAA.szEnumKey,
  299. ARRAYSIZE(_NAA.szEnumKey), NULL, NULL);
  300. WideCharToMultiByte(CP_ACP, 0, pNA->szClassKey, -1, _NAA.szClassKey,
  301. ARRAYSIZE(_NAA.szClassKey), NULL, NULL);
  302. WideCharToMultiByte(CP_ACP, 0, pNA->szManufacturer, -1, _NAA.szManufacturer,
  303. ARRAYSIZE(_NAA.szManufacturer), NULL, NULL);
  304. WideCharToMultiByte(CP_ACP, 0, pNA->szInfFileName, -1, _NAA.szInfFileName,
  305. ARRAYSIZE(_NAA.szInfFileName), NULL, NULL);
  306. _NAA.bNicType = pNA->bNicType;
  307. _NAA.bNetType = pNA->bNetType;
  308. _NAA.bNetSubType = pNA->bNetSubType;
  309. _NAA.bIcsStatus = pNA->bIcsStatus;
  310. _NAA.bError = pNA->bError;
  311. _NAA.bWarning = pNA->bWarning;
  312. }
  313. }
  314. //
  315. //
  316. //
  317. #undef IsProtocolInstalled
  318. BOOL WINAPI IsProtocolInstalled(LPCWSTR pszProtocolDeviceID, BOOL bExhaustive)
  319. {
  320. CStrIn CStrProtocolDeviceID(pszProtocolDeviceID);
  321. return IsProtocolInstalledA(CStrProtocolDeviceID, bExhaustive);
  322. }
  323. #undef InstallProtocol
  324. HRESULT WINAPI InstallProtocol(LPCWSTR pszProtocol, HWND hwndParent, PROGRESS_CALLBACK pfnCallback, LPVOID pvCallbackParam)
  325. {
  326. CStrIn CStrProtocol(pszProtocol);
  327. return InstallProtocolA(CStrProtocol, hwndParent, pfnCallback, pvCallbackParam);
  328. }
  329. #undef RemoveProtocol
  330. HRESULT WINAPI RemoveProtocol(LPCWSTR pszProtocol)
  331. {
  332. CStrIn CStrProtocol(pszProtocol);
  333. return RemoveProtocolA(CStrProtocol);
  334. }
  335. #undef FindConflictingService
  336. BOOL WINAPI FindConflictingService(LPCWSTR pszWantService, NETSERVICE* pConflict)
  337. {
  338. BOOL fRet;
  339. CStrIn CStrWantService(pszWantService);
  340. CNetServiceOut CNSConflict(pConflict);
  341. fRet = FindConflictingServiceA(CStrWantService, CNSConflict);
  342. if (fRet)
  343. {
  344. CNSConflict.Convert();
  345. }
  346. return fRet;
  347. }
  348. #undef EnumNetAdapters
  349. int WINAPI EnumNetAdapters(NETADAPTER FAR** pprgNetAdapters)
  350. {
  351. int nRet;
  352. CNetAdaptersOut CNANetAdapters(pprgNetAdapters);
  353. nRet = EnumNetAdaptersA(CNANetAdapters);
  354. nRet = CNANetAdapters.Convert(nRet);
  355. return nRet;
  356. }
  357. /*
  358. #undef InstallNetAdapters
  359. HRESULT WINAPI InstallNetAdapter(LPCWSTR pszDeviceID, LPCWSTR pszInfPath, HWND hwndParent, PROGRESS_CALLBACK pfnProgress, LPVOID pvCallbackParam)
  360. {
  361. CStrIn CStrDeviceID(pszDeviceID);
  362. CStrIn CStrInfPath(pszInfPath);
  363. return InstallNetAdapterA(CStrDeviceID, CStrInfPath, hwndParent, pfnProgress, pvCallbackParam);
  364. }
  365. */
  366. #undef IsProtocolBoundToAdapter
  367. BOOL WINAPI IsProtocolBoundToAdapter(LPCWSTR pszProtocolID, const NETADAPTER* pAdapter)
  368. {
  369. CStrIn CStrProtocolID(pszProtocolID);
  370. CNetAdapterIn CNAAdapter(pAdapter);
  371. return IsProtocolBoundToAdapterA(CStrProtocolID, CNAAdapter);
  372. }
  373. /*
  374. #undef ENableNetAdapter
  375. HRESULT WINAPI EnableNetAdapter(const NETADAPTER* pAdapter)
  376. {
  377. CNetAdapterIn CNAAdapter(pAdapter);
  378. return EnableNetAdapterA(CNAAdapter);
  379. }
  380. */
  381. #undef IsClientInstalled
  382. BOOL WINAPI IsClientInstalled(LPCWSTR pszClient, BOOL bExhaustive)
  383. {
  384. CStrIn CStrClient(pszClient);
  385. return IsClientInstalledA(CStrClient, bExhaustive);
  386. }
  387. /*
  388. #undef RemoveClient
  389. HRESULT WINAPI RemoveClient(LPCWSTR pszClient)
  390. {
  391. CStrIn CStrClient(pszClient);
  392. return RemoveClientA(pszClient);
  393. }
  394. */
  395. /*
  396. #undef RemoveGhostedAdapters
  397. HRESULT WINAPI RemoveGhostedAdapters(LPCWSTR pszDeviceID)
  398. {
  399. CStrIn CStrDeviceID(pszDeviceID);
  400. return RemoveGhostedAdaptersA(CStrDeviceID);
  401. }
  402. */
  403. /*
  404. #undef RemoveUnknownAdapters
  405. HRESULT WINAPI RemoveUnknownAdapters(LPCWSTR pszDeviceID)
  406. {
  407. CStrIn CStrDeviceID(pszDeviceID);
  408. return RemoveUnknownAdaptersA(CStrDeviceID);
  409. }
  410. */
  411. /*
  412. #undef DoesAdapterMatchDeviceID
  413. BOOL WINAPI DoesAdapterMatchDeviceID(const NETADAPTER* pAdapter, LPCWSTR pszDeviceID)
  414. {
  415. CNetAdapterIn CNAAdapter(pAdapter);
  416. CStrIn CStrDeviceID(pszDeviceID);
  417. return DoesAdapterMatchDeviceIDA(CNAAdapter, CStrDeviceID);
  418. }
  419. */
  420. #undef IsAdapterBroadband
  421. BOOL WINAPI IsAdapterBroadband(const NETADAPTER* pAdapter)
  422. {
  423. CNetAdapterIn CNAAdapter(pAdapter);
  424. return IsAdapterBroadbandA(CNAAdapter);
  425. }
  426. #undef SaveBroadbandSettings
  427. void WINAPI SaveBroadbandSettings(LPCWSTR pszBroadbandAdapterNumber)
  428. {
  429. CStrIn CStrBroadbandAdapterNumber(pszBroadbandAdapterNumber);
  430. SaveBroadbandSettingsA(CStrBroadbandAdapterNumber);
  431. }
  432. /*
  433. #undef UpdateBroadbandSettings
  434. BOOL WINAPI UpdateBroadbandSettings(LPWSTR pszEnumKeyBuf, int cchEnumKeyBuf)
  435. {
  436. return FALSE;
  437. }
  438. */
  439. #undef DetectHardware
  440. HRESULT WINAPI DetectHardware(LPCWSTR pszDeviceID)
  441. {
  442. CStrIn CStrDeviceID(pszDeviceID);
  443. return DetectHardwareA(CStrDeviceID);
  444. }
  445. #undef EnableAutodial
  446. void WINAPI EnableAutodial(BOOL bAutodial, LPCWSTR pszConnection)
  447. {
  448. CStrIn CStrConnection(pszConnection);
  449. EnableAutodialA(bAutodial, CStrConnection);
  450. }
  451. #undef SetDefaultDialupConnection
  452. void WINAPI SetDefaultDialupConnection(LPCWSTR pszConnectionName)
  453. {
  454. CStrIn CStrConnectionName(pszConnectionName);
  455. SetDefaultDialupConnectionA(CStrConnectionName);
  456. }
  457. #undef GetDefaultDialupConnection
  458. void WINAPI GetDefaultDialupConnection(LPWSTR pszConnectionName, int cchMax)
  459. {
  460. CStrOut CStrConnectionName(pszConnectionName, cchMax);
  461. GetDefaultDialupConnectionA(CStrConnectionName, CStrConnectionName);
  462. CStrConnectionName.Convert();
  463. }
  464. #undef EnumMatchingNetBindings
  465. int WINAPI EnumMatchingNetBindings(LPCWSTR pszParentBinding, LPCWSTR pszDeviceID, LPWSTR** pprgBindings)
  466. {
  467. int nRet;
  468. CStrIn CStrParentBinding(pszParentBinding);
  469. CStrIn CStrDeviceID(pszDeviceID);
  470. CStrsOut CStrsBindings(pprgBindings);
  471. nRet = EnumMatchingNetBindingsA(CStrParentBinding, CStrDeviceID, CStrsBindings);
  472. nRet = CStrsBindings.Convert(nRet);
  473. return nRet;
  474. }