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.

586 lines
14 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992-2001.
  5. //
  6. // File: COMMON.CPP
  7. //
  8. // Contents: Debug & Utility functions
  9. //
  10. // Notes:
  11. //
  12. // Author: Alok Sinha
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <netcfgn.h>
  18. #include "common.h"
  19. #ifdef DBG
  20. void TraceMsg (LPWSTR szFormat, ...)
  21. {
  22. static WCHAR szTempBuf[4096];
  23. va_list arglist;
  24. va_start(arglist, szFormat);
  25. vswprintf( szTempBuf, szFormat, arglist );
  26. OutputDebugStringW( szTempBuf );
  27. va_end(arglist);
  28. }
  29. void DumpChangeFlag (DWORD dwChangeFlag)
  30. {
  31. TraceMsg( L" ChangeFlag:" );
  32. if ( dwChangeFlag & NCN_ADD ) {
  33. TraceMsg( L" NCN_ADD" );
  34. }
  35. if ( dwChangeFlag & NCN_REMOVE ) {
  36. TraceMsg( L" NCN_REMOVE" );
  37. }
  38. if ( dwChangeFlag & NCN_UPDATE ) {
  39. TraceMsg( L" NCN_UPDATE" );
  40. }
  41. if ( dwChangeFlag & NCN_ENABLE ) {
  42. TraceMsg( L" NCN_ENABLE" );
  43. }
  44. if ( dwChangeFlag & NCN_DISABLE ) {
  45. TraceMsg( L" NCN_DISABLE" );
  46. }
  47. if ( dwChangeFlag & NCN_BINDING_PATH ) {
  48. TraceMsg( L" NCN_BINDING_PATH" );
  49. }
  50. if ( dwChangeFlag & NCN_PROPERTYCHANGE ) {
  51. TraceMsg( L" NCN_PROPERTYCHANGE" );
  52. }
  53. if ( dwChangeFlag & NCN_NET ) {
  54. TraceMsg( L" NCN_NET" );
  55. }
  56. if ( dwChangeFlag & NCN_NETTRANS ) {
  57. TraceMsg( L" NCN_NETTRANS" );
  58. }
  59. if ( dwChangeFlag & NCN_NETCLIENT ) {
  60. TraceMsg( L" NCN_NETCLIENT" );
  61. }
  62. if ( dwChangeFlag & NCN_NETSERVICE ) {
  63. TraceMsg( L" NCN_NETSERVICE" );
  64. }
  65. TraceMsg( L"\n" );
  66. return;
  67. }
  68. void DumpBindingPath (INetCfgBindingPath *pncbp)
  69. {
  70. INetCfgComponent *pncc;
  71. IEnumNetCfgBindingInterface *pencbi;
  72. INetCfgBindingInterface *pncbi;
  73. LPWSTR lpsz;
  74. DWORD dwIndex;
  75. ULONG ulCount;
  76. HRESULT hr;
  77. hr = pncbp->GetPathToken( &lpsz );
  78. if ( hr == S_OK ) {
  79. TraceMsg( L" BindingPath: %s\n",
  80. lpsz );
  81. CoTaskMemFree( lpsz );
  82. }
  83. else {
  84. TraceMsg( L" BindingPath: GetPathToken failed(HRESULT %x).\n",
  85. hr );
  86. }
  87. #ifdef VERBOSE_TRACE
  88. hr = pncbp->EnumBindingInterfaces( &pencbi );
  89. if ( hr == S_OK ) {
  90. hr = pencbi ->Next( 1, &pncbi, &ulCount );
  91. for (dwIndex=0; hr == S_OK; dwIndex++ ) {
  92. hr = pncbi->GetName( &lpsz );
  93. if ( hr == S_OK ) {
  94. TraceMsg( L" BindingInterface(%d): %s\n",
  95. dwIndex, lpsz );
  96. CoTaskMemFree( lpsz );
  97. }
  98. else {
  99. TraceMsg( L" BindingInterface(%d): GetName failed(HRESULT %x).\n",
  100. dwIndex, hr );
  101. }
  102. hr = pncbi->GetUpperComponent( &pncc );
  103. if ( hr == S_OK ) {
  104. TraceMsg( L" \tUpperComponent of the interface(%d)...\n",
  105. dwIndex );
  106. DumpComponent( pncc );
  107. ReleaseObj( pncc );
  108. }
  109. else {
  110. TraceMsg( L" UpperComponent: GetUpperComponent failed(HRESULT = %x).\n",
  111. hr );
  112. }
  113. hr = pncbi->GetLowerComponent( &pncc );
  114. if ( hr == S_OK ) {
  115. TraceMsg( L" \tLowerComponent of the interface(%d)...\n",
  116. dwIndex );
  117. DumpComponent( pncc );
  118. ReleaseObj( pncc );
  119. }
  120. else {
  121. TraceMsg( L" LowerComponent: GetLowerComponent failed(HRESULT = %x).\n",
  122. hr );
  123. }
  124. ReleaseObj( pncbi );
  125. hr = pencbi ->Next( 1,
  126. &pncbi,
  127. &ulCount );
  128. }
  129. ReleaseObj( pencbi );
  130. }
  131. else {
  132. TraceMsg( L" EnumBindingInterfaces failed, (HRESULT = %x)\n",
  133. hr );
  134. }
  135. #endif
  136. return;
  137. }
  138. void DumpComponent (INetCfgComponent *pncc)
  139. {
  140. LPWSTR lpsz;
  141. DWORD dwChars;
  142. ULONG ulStatus;
  143. HRESULT hr;
  144. hr = pncc->GetDisplayName( &lpsz );
  145. if ( hr == S_OK ) {
  146. TraceMsg( L" \t\tComponent: %s\n",
  147. lpsz );
  148. CoTaskMemFree( lpsz );
  149. }
  150. else {
  151. TraceMsg( L" GetDisplay failed(HRESULT %x).\n",
  152. hr );
  153. }
  154. hr = pncc->GetCharacteristics( &dwChars );
  155. if ( hr == S_OK ) {
  156. TraceMsg( L" \t\tCharacteristics:" );
  157. if ( dwChars & NCF_HIDDEN ) {
  158. TraceMsg( L" NCF_HIDDEN" );
  159. }
  160. if ( dwChars & NCF_NO_SERVICE ) {
  161. TraceMsg( L" NCF_NO_SERVICE" );
  162. }
  163. if ( dwChars & NCF_VIRTUAL ) {
  164. TraceMsg( L" NCF_VIRTUAL" );
  165. }
  166. if ( dwChars & NCF_PHYSICAL ) {
  167. TraceMsg( L" NCF_PHYSICAL" );
  168. }
  169. if ( dwChars & NCF_FILTER ) {
  170. TraceMsg( L" NCF_FILTER" );
  171. }
  172. if ( dwChars & NCF_NOT_USER_REMOVABLE ) {
  173. TraceMsg( L" NCF_NOT_USER_REMOVABLE" );
  174. }
  175. if ( dwChars & NCF_HAS_UI ) {
  176. TraceMsg( L" NCF_HAS_UI" );
  177. }
  178. if ( dwChars & NCF_SOFTWARE_ENUMERATED ) {
  179. TraceMsg( L" NCF_SOFTWARE_ENUMERATED" );
  180. }
  181. if ( dwChars & NCF_MULTIPORT_INSTANCED_ADAPTER ) {
  182. TraceMsg( L" NCF_MULTIPORT_INSTANCED_ADAPTER" );
  183. }
  184. TraceMsg( L"\n" );
  185. }
  186. else {
  187. TraceMsg( L" GetCharacteristics failed(HRESULT %x).\n",
  188. hr );
  189. }
  190. hr = pncc->GetId( &lpsz );
  191. if ( hr == S_OK ) {
  192. TraceMsg( L" \t\tHardware Id: %s\n",
  193. lpsz );
  194. CoTaskMemFree( lpsz );
  195. }
  196. else {
  197. TraceMsg( L" GetId failed(HRESULT %x).\n",
  198. hr );
  199. }
  200. hr = pncc->GetBindName( &lpsz );
  201. if ( hr == S_OK ) {
  202. TraceMsg( L" \t\tBindName: %s\n",
  203. lpsz );
  204. CoTaskMemFree( lpsz );
  205. }
  206. else {
  207. TraceMsg( L" GetBindName failed(HRESULT %x).\n",
  208. hr );
  209. }
  210. if ( dwChars & NCF_PHYSICAL ) {
  211. hr = pncc->GetDeviceStatus( &ulStatus );
  212. if ( hr == S_OK ) {
  213. TraceMsg( L" \t\tDeviceStatus: %#x\n",
  214. ulStatus );
  215. }
  216. else {
  217. TraceMsg( L" GetDeviceStatus failed(HRESULT %x).\n",
  218. hr );
  219. }
  220. }
  221. return;
  222. }
  223. #endif
  224. HRESULT HrFindInstance (INetCfg *pnc,
  225. GUID &guidInstance,
  226. INetCfgComponent **ppnccMiniport)
  227. {
  228. IEnumNetCfgComponent *pencc;
  229. INetCfgComponent *pncc;
  230. GUID guid;
  231. WCHAR szGuid[MAX_PATH+1];
  232. ULONG ulCount;
  233. BOOL found;
  234. HRESULT hr;
  235. TraceMsg( L"-->HrFindInstance.\n" );
  236. hr = pnc->EnumComponents( &GUID_DEVCLASS_NET,
  237. &pencc );
  238. if ( hr == S_OK ) {
  239. StringFromGUID2( guidInstance,
  240. szGuid,
  241. MAX_PATH+1 );
  242. TraceMsg( L" Looking for component with InstanceGuid %s\n",
  243. szGuid );
  244. hr = pencc->Next( 1,
  245. &pncc,
  246. &ulCount );
  247. for ( found=FALSE; (hr == S_OK) && (found == FALSE); ) {
  248. hr = pncc->GetInstanceGuid( &guid );
  249. if ( hr == S_OK ) {
  250. StringFromGUID2( guid,
  251. szGuid,
  252. MAX_PATH+1 );
  253. TraceMsg( L" Found component with InstanceGuid %s\n",
  254. szGuid );
  255. found = IsEqualGUID( guid,
  256. guidInstance );
  257. if ( found == FALSE ) {
  258. ReleaseObj( pncc );
  259. hr = pencc->Next( 1,
  260. &pncc,
  261. &ulCount );
  262. }
  263. else {
  264. *ppnccMiniport = pncc;
  265. }
  266. }
  267. }
  268. ReleaseObj( pencc );
  269. }
  270. else {
  271. TraceMsg( L" EnumComponents failed(HRESULT = %x).\n",
  272. hr );
  273. }
  274. TraceMsg( L"<--HrFindInstance(HRESULT = %x).\n",
  275. hr );
  276. return hr;
  277. }
  278. LONG AddToMultiSzValue( HKEY hkeyAdapterGuid,
  279. LPWSTR szMiniportGuid)
  280. {
  281. LPWSTR lpCurrentValue=NULL;
  282. LPWSTR lpNewValue=NULL;
  283. DWORD dwLen;
  284. DWORD dwNewLen;
  285. LONG lResult;
  286. dwLen = 0;
  287. lResult = RegQueryValueExW(
  288. hkeyAdapterGuid,
  289. c_szUpperBindings,
  290. NULL,
  291. NULL,
  292. NULL,
  293. &dwLen );
  294. if ( dwLen != 0 ) {
  295. lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  296. if ( lpCurrentValue ) {
  297. lResult = RegQueryValueExW( hkeyAdapterGuid,
  298. c_szUpperBindings,
  299. NULL,
  300. NULL,
  301. (LPBYTE)lpCurrentValue,
  302. &dwLen );
  303. }
  304. else {
  305. lResult = ERROR_NOT_ENOUGH_MEMORY;
  306. }
  307. }
  308. else {
  309. dwLen = sizeof(WCHAR);
  310. lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  311. if ( !lpCurrentValue ) {
  312. lResult = ERROR_NOT_ENOUGH_MEMORY;
  313. }
  314. else {
  315. lResult = ERROR_SUCCESS;
  316. }
  317. }
  318. if ( lResult == ERROR_SUCCESS ) {
  319. dwNewLen = dwLen + ((wcslen(szMiniportGuid) + 1) * sizeof(WCHAR));
  320. lpNewValue = (LPWSTR)malloc( dwNewLen );
  321. if ( lpNewValue ) {
  322. wcscpy( lpNewValue, szMiniportGuid );
  323. CopyMemory( lpNewValue+wcslen(szMiniportGuid)+1,
  324. lpCurrentValue,
  325. dwLen );
  326. lResult = RegSetValueExW( hkeyAdapterGuid,
  327. c_szUpperBindings,
  328. 0,
  329. REG_MULTI_SZ,
  330. (LPBYTE)lpNewValue,
  331. dwNewLen );
  332. }
  333. else {
  334. lResult = ERROR_NOT_ENOUGH_MEMORY;
  335. }
  336. }
  337. if ( lpCurrentValue ) {
  338. free( lpCurrentValue );
  339. }
  340. if ( lpNewValue ) {
  341. free( lpNewValue );
  342. }
  343. return lResult;
  344. }
  345. LONG DeleteFromMultiSzValue( HKEY hkeyAdapterGuid,
  346. LPWSTR szMiniportGuid)
  347. {
  348. LPWSTR lpCurrentValue=NULL;
  349. LPWSTR lpNewValue=NULL;
  350. LPWSTR lpCurrentValueTemp;
  351. LPWSTR lpNewValueTemp;
  352. DWORD dwLen;
  353. DWORD dwNewLen;
  354. LONG lResult;
  355. dwLen = 0;
  356. lResult = RegQueryValueExW(
  357. hkeyAdapterGuid,
  358. c_szUpperBindings,
  359. NULL,
  360. NULL,
  361. NULL,
  362. &dwLen );
  363. if ( lResult == ERROR_SUCCESS ) {
  364. lpCurrentValue = (LPWSTR)calloc( dwLen, 1 );
  365. lpNewValue = (LPWSTR)calloc( dwLen, 1 );
  366. if ( lpCurrentValue && lpNewValue ) {
  367. lResult = RegQueryValueExW(
  368. hkeyAdapterGuid,
  369. c_szUpperBindings,
  370. NULL,
  371. NULL,
  372. (LPBYTE)lpCurrentValue,
  373. &dwLen );
  374. if ( lResult == ERROR_SUCCESS ) {
  375. lpCurrentValueTemp = lpCurrentValue;
  376. lpNewValueTemp = lpNewValue;
  377. while( wcslen(lpCurrentValueTemp) ) {
  378. if ( _wcsicmp(lpCurrentValueTemp, szMiniportGuid) ) {
  379. wcscpy( lpNewValueTemp, lpCurrentValueTemp );
  380. lpNewValueTemp += wcslen(lpNewValueTemp) + 1;
  381. }
  382. lpCurrentValueTemp += wcslen(lpCurrentValueTemp) + 1;
  383. } //end of while
  384. dwNewLen = dwLen - ((wcslen(szMiniportGuid) + 1) * sizeof(WCHAR));
  385. if ( dwNewLen > 2 ) {
  386. lResult = RegSetValueExW( hkeyAdapterGuid,
  387. c_szUpperBindings,
  388. 0,
  389. REG_MULTI_SZ,
  390. (LPBYTE)lpNewValue,
  391. dwNewLen );
  392. }
  393. else {
  394. lResult = RegDeleteValueW( hkeyAdapterGuid,
  395. c_szUpperBindings );
  396. }
  397. }
  398. }
  399. else {
  400. lResult = ERROR_NOT_ENOUGH_MEMORY;
  401. }
  402. }
  403. if ( lpCurrentValue ) {
  404. free( lpCurrentValue );
  405. }
  406. if ( lpNewValue ) {
  407. free( lpNewValue );
  408. }
  409. return lResult;
  410. }
  411. LPWSTR AddDevicePrefix (LPWSTR lpStr)
  412. {
  413. LPWSTR lpNewStr;
  414. lpNewStr = (LPWSTR)malloc( (wcslen(lpStr) +
  415. wcslen(c_szDevicePrefix) + 1) *
  416. sizeof(WCHAR) );
  417. if ( lpNewStr ) {
  418. wcscpy( lpNewStr, c_szDevicePrefix );
  419. wcscat( lpNewStr, lpStr );
  420. }
  421. return lpNewStr;
  422. }
  423. LPWSTR RemoveDevicePrefix (LPWSTR lpStr)
  424. {
  425. int i;
  426. LPWSTR lpNewStr;
  427. LPWSTR lpTemp;
  428. lpTemp = wcsrchr( lpStr, '\\' );
  429. if ( lpTemp ) {
  430. lpNewStr = _wcsdup( lpTemp+1 );
  431. }
  432. else {
  433. lpNewStr = NULL;
  434. }
  435. return lpNewStr;
  436. }