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.

1680 lines
47 KiB

  1. //*************************************************************
  2. //
  3. // DLL loading functions
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. //*************************************************************
  10. #include "uenv.h"
  11. //
  12. // file global variables containing pointers to APIs and
  13. // loaded modules
  14. //
  15. NETAPI32_API g_NetApi32Api;
  16. SECUR32_API g_Secur32Api;
  17. LDAP_API g_LdapApi;
  18. ICMP_API g_IcmpApi;
  19. WSOCK32_API g_WSock32Api;
  20. DS_API g_DsApi;
  21. SHELL32_API g_Shell32Api;
  22. SHLWAPI_API g_ShlwapiApi;
  23. OLE32_API g_Ole32Api;
  24. GPTEXT_API g_GpTextApi;
  25. IPHLPAPI_API g_IpHlpApi;
  26. WS2_32_API g_ws2_32Api;
  27. CRITICAL_SECTION *g_ApiDLLCritSec;
  28. //*************************************************************
  29. //
  30. // InitializeAPIs()
  31. //
  32. // Purpose: initializes API structures for delay loaded
  33. // modules
  34. //
  35. // Parameters: none
  36. //
  37. //
  38. // Return: none
  39. //
  40. //*************************************************************
  41. void InitializeAPIs( void )
  42. {
  43. ZeroMemory( &g_NetApi32Api, sizeof( NETAPI32_API ) );
  44. ZeroMemory( &g_Secur32Api, sizeof( SECUR32_API ) );
  45. ZeroMemory( &g_LdapApi, sizeof( LDAP_API ) );
  46. ZeroMemory( &g_IcmpApi, sizeof( ICMP_API ) );
  47. ZeroMemory( &g_WSock32Api, sizeof( WSOCK32_API ) );
  48. ZeroMemory( &g_DsApi, sizeof( DS_API ) );
  49. ZeroMemory( &g_Shell32Api, sizeof( SHELL32_API ) );
  50. ZeroMemory( &g_ShlwapiApi, sizeof( SHLWAPI_API ) );
  51. ZeroMemory( &g_Ole32Api, sizeof( OLE32_API ) );
  52. ZeroMemory( &g_GpTextApi, sizeof( GPTEXT_API ) );
  53. }
  54. //*************************************************************
  55. //
  56. // InitializeApiDLLsCritSec()
  57. //
  58. // Purpose: initializes a CRITICAL_SECTION for synch'ing
  59. // DLL loads
  60. //
  61. // Parameters: none
  62. //
  63. //
  64. // Return: ERROR_SUCCESS if successful
  65. // An error if it fails.
  66. //
  67. //*************************************************************
  68. DWORD InitializeApiDLLsCritSec( void )
  69. {
  70. CRITICAL_SECTION *pCritSec = NULL;
  71. DWORD result = ERROR_SUCCESS;
  72. BOOL fInitialized = FALSE;
  73. CRITICAL_SECTION *pInitial;
  74. // If the critical section already exists, return.
  75. if (g_ApiDLLCritSec != NULL)
  76. return ERROR_SUCCESS;
  77. // Allocate memory for the critial section.
  78. pCritSec = (CRITICAL_SECTION *) LocalAlloc( LMEM_FIXED,
  79. sizeof(CRITICAL_SECTION) );
  80. if (pCritSec == NULL)
  81. {
  82. result = ERROR_NOT_ENOUGH_MEMORY;
  83. goto Exit;
  84. }
  85. // Initialize the critical section. Using the flag 0x80000000
  86. // preallocates the event so that EnterCriticalSection can only
  87. // throw timeout exceptions.
  88. __try
  89. {
  90. if (!InitializeCriticalSectionAndSpinCount( pCritSec, 0x80000000 ))
  91. result = GetLastError();
  92. else
  93. fInitialized = TRUE;
  94. }
  95. __except( EXCEPTION_EXECUTE_HANDLER )
  96. {
  97. result = ERROR_NOT_ENOUGH_MEMORY;
  98. }
  99. if (result != ERROR_SUCCESS)
  100. goto Exit;
  101. // Save the critical section.
  102. pInitial = (CRITICAL_SECTION *) InterlockedCompareExchangePointer(
  103. (void **) &g_ApiDLLCritSec, (void *) pCritSec, NULL );
  104. // If the InterlockedCompareExchange succeeded, don't free the
  105. // critical section just allocated.
  106. if (pInitial == NULL)
  107. pCritSec = NULL;
  108. Exit:
  109. if (pCritSec != NULL)
  110. {
  111. if (fInitialized)
  112. DeleteCriticalSection( pCritSec );
  113. LocalFree( pCritSec );
  114. }
  115. return result;
  116. }
  117. //*************************************************************
  118. //
  119. // CloseApiDLLsCritSec()
  120. //
  121. // Purpose: clean up CRITICAL_SECTION for synch'ing
  122. // DLL loads
  123. //
  124. // Parameters: none
  125. //
  126. //
  127. // Return: none
  128. //
  129. //*************************************************************
  130. void CloseApiDLLsCritSec( void )
  131. {
  132. if (g_ApiDLLCritSec != NULL)
  133. {
  134. DeleteCriticalSection( g_ApiDLLCritSec );
  135. LocalFree( g_ApiDLLCritSec );
  136. g_ApiDLLCritSec = NULL;
  137. }
  138. }
  139. //*************************************************************
  140. //
  141. // LoadNetAPI32()
  142. //
  143. // Purpose: Loads netapi32.dll
  144. //
  145. // Parameters: pNETAPI32 - pointer to a NETAPI32_API structure to
  146. // initialize
  147. //
  148. //
  149. // Return: TRUE if successful
  150. // FALSE if an error occurs
  151. //
  152. //*************************************************************
  153. PNETAPI32_API LoadNetAPI32 ()
  154. {
  155. BOOL bResult = FALSE;
  156. PNETAPI32_API pNetAPI32 = &g_NetApi32Api;
  157. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  158. return NULL;
  159. EnterCriticalSection( g_ApiDLLCritSec );
  160. if ( pNetAPI32->hInstance ) {
  161. //
  162. // module is already loaded and initialized
  163. //
  164. LeaveCriticalSection( g_ApiDLLCritSec );
  165. return pNetAPI32;
  166. }
  167. pNetAPI32->hInstance = LoadLibrary (TEXT("netapi32.dll"));
  168. if (!pNetAPI32->hInstance) {
  169. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to load netapi32 with %d."),
  170. GetLastError()));
  171. goto Exit;
  172. }
  173. pNetAPI32->pfnDsGetDcName = (PFNDSGETDCNAME) GetProcAddress (pNetAPI32->hInstance,
  174. #ifdef UNICODE
  175. "DsGetDcNameW");
  176. #else
  177. "DsGetDcNameA");
  178. #endif
  179. if (!pNetAPI32->pfnDsGetDcName) {
  180. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find DsGetDcName with %d."),
  181. GetLastError()));
  182. goto Exit;
  183. }
  184. pNetAPI32->pfnDsGetSiteName = (PFNDSGETSITENAME) GetProcAddress (pNetAPI32->hInstance,
  185. #ifdef UNICODE
  186. "DsGetSiteNameW");
  187. #else
  188. "DsGetSiteNameA");
  189. #endif
  190. if (!pNetAPI32->pfnDsGetSiteName) {
  191. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find DsGetSiteName with %d."),
  192. GetLastError()));
  193. goto Exit;
  194. }
  195. pNetAPI32->pfnDsRoleGetPrimaryDomainInformation = (PFNDSROLEGETPRIMARYDOMAININFORMATION)GetProcAddress (pNetAPI32->hInstance,
  196. "DsRoleGetPrimaryDomainInformation");
  197. if (!pNetAPI32->pfnDsRoleGetPrimaryDomainInformation) {
  198. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find pfnDsRoleGetPrimaryDomainInformation with %d."),
  199. GetLastError()));
  200. goto Exit;
  201. }
  202. pNetAPI32->pfnDsRoleFreeMemory = (PFNDSROLEFREEMEMORY)GetProcAddress (pNetAPI32->hInstance,
  203. "DsRoleFreeMemory");
  204. if (!pNetAPI32->pfnDsRoleFreeMemory) {
  205. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find pfnDsRoleFreeMemory with %d."),
  206. GetLastError()));
  207. goto Exit;
  208. }
  209. pNetAPI32->pfnNetApiBufferFree = (PFNNETAPIBUFFERFREE) GetProcAddress (pNetAPI32->hInstance,
  210. "NetApiBufferFree");
  211. if (!pNetAPI32->pfnNetApiBufferFree) {
  212. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find NetApiBufferFree with %d."),
  213. GetLastError()));
  214. goto Exit;
  215. }
  216. pNetAPI32->pfnNetUserGetGroups = (PFNNETUSERGETGROUPS) GetProcAddress (pNetAPI32->hInstance,
  217. "NetUserGetGroups");
  218. if (!pNetAPI32->pfnNetUserGetGroups) {
  219. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find NetUserGetGroups with %d."),
  220. GetLastError()));
  221. goto Exit;
  222. }
  223. pNetAPI32->pfnNetUserGetInfo = (PFNNETUSERGETINFO) GetProcAddress (pNetAPI32->hInstance,
  224. "NetUserGetInfo");
  225. if (!pNetAPI32->pfnNetUserGetInfo) {
  226. DebugMsg((DM_WARNING, TEXT("LoadNetAPI32: Failed to find NetUserGetInfo with %d."),
  227. GetLastError()));
  228. goto Exit;
  229. }
  230. //
  231. // Success
  232. //
  233. bResult = TRUE;
  234. Exit:
  235. if (!bResult) {
  236. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  237. ev.AddArg(TEXT("netapi32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  238. if ( pNetAPI32->hInstance ) {
  239. FreeLibrary( pNetAPI32->hInstance );
  240. }
  241. ZeroMemory( pNetAPI32, sizeof( NETAPI32_API ) );
  242. pNetAPI32 = 0;
  243. }
  244. LeaveCriticalSection( g_ApiDLLCritSec );
  245. return pNetAPI32;
  246. }
  247. //*************************************************************
  248. //
  249. // LoadSecur32()
  250. //
  251. // Purpose: Loads secur32.dll
  252. //
  253. //
  254. //
  255. // Return: TRUE if successful
  256. // FALSE if an error occurs
  257. //
  258. //*************************************************************
  259. PSECUR32_API LoadSecur32 ()
  260. {
  261. BOOL bResult = FALSE;
  262. PSECUR32_API pSecur32 = &g_Secur32Api;
  263. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  264. return NULL;
  265. EnterCriticalSection( g_ApiDLLCritSec );
  266. if ( pSecur32->hInstance ) {
  267. //
  268. // module is already loaded and initialized
  269. //
  270. LeaveCriticalSection( g_ApiDLLCritSec );
  271. return pSecur32;
  272. }
  273. //
  274. // Load secur32.dll
  275. //
  276. pSecur32->hInstance = LoadLibrary (TEXT("secur32.dll"));
  277. if (!pSecur32->hInstance) {
  278. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to load secur32 with %d."),
  279. GetLastError()));
  280. goto Exit;
  281. }
  282. pSecur32->pfnGetUserNameEx = (PFNGETUSERNAMEEX)GetProcAddress (pSecur32->hInstance,
  283. #ifdef UNICODE
  284. "GetUserNameExW");
  285. #else
  286. "GetUserNameExA");
  287. #endif
  288. if (!pSecur32->pfnGetUserNameEx) {
  289. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find GetUserNameEx with %d."),
  290. GetLastError()));
  291. goto Exit;
  292. }
  293. pSecur32->pfnGetComputerObjectName = (PFNGETCOMPUTEROBJECTNAME)GetProcAddress (pSecur32->hInstance,
  294. #ifdef UNICODE
  295. "GetComputerObjectNameW");
  296. #else
  297. "GetComputerObjectNameA");
  298. #endif
  299. if (!pSecur32->pfnGetComputerObjectName) {
  300. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find GetComputerObjectName with %d."),
  301. GetLastError()));
  302. goto Exit;
  303. }
  304. pSecur32->pfnTranslateName = (PFNTRANSLATENAME)GetProcAddress (pSecur32->hInstance,
  305. #ifdef UNICODE
  306. "TranslateNameW");
  307. #else
  308. "TranslateNameA");
  309. #endif
  310. if (!pSecur32->pfnTranslateName) {
  311. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find TranslateName with %d."),
  312. GetLastError()));
  313. goto Exit;
  314. }
  315. pSecur32->pfnAcceptSecurityContext = (ACCEPT_SECURITY_CONTEXT_FN)GetProcAddress (pSecur32->hInstance,
  316. "AcceptSecurityContext");
  317. if (!pSecur32->pfnAcceptSecurityContext) {
  318. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find AcceptSecurityContext with %d."),
  319. GetLastError()));
  320. goto Exit;
  321. }
  322. pSecur32->pfnAcquireCredentialsHandle = (ACQUIRE_CREDENTIALS_HANDLE_FN)GetProcAddress (pSecur32->hInstance,
  323. #ifdef UNICODE
  324. "AcquireCredentialsHandleW");
  325. #else
  326. "AcquireCredentialsHandleA");
  327. #endif
  328. if (!pSecur32->pfnAcquireCredentialsHandle) {
  329. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find AcquireCredentialsHandle with %d."),
  330. GetLastError()));
  331. goto Exit;
  332. }
  333. pSecur32->pfnDeleteSecurityContext = (DELETE_SECURITY_CONTEXT_FN)GetProcAddress (pSecur32->hInstance,
  334. "DeleteSecurityContext");
  335. if (!pSecur32->pfnDeleteSecurityContext) {
  336. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find DeleteSecurityContext with %d."),
  337. GetLastError()));
  338. goto Exit;
  339. }
  340. pSecur32->pfnFreeContextBuffer = (FREE_CONTEXT_BUFFER_FN)GetProcAddress (pSecur32->hInstance,
  341. "FreeContextBuffer");
  342. if (!pSecur32->pfnFreeContextBuffer) {
  343. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find FreeContextBuffer with %d."),
  344. GetLastError()));
  345. goto Exit;
  346. }
  347. pSecur32->pfnFreeCredentialsHandle = (FREE_CREDENTIALS_HANDLE_FN)GetProcAddress (pSecur32->hInstance,
  348. "FreeCredentialsHandle");
  349. if (!pSecur32->pfnFreeCredentialsHandle) {
  350. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find FreeCredentialsHandle with %d."),
  351. GetLastError()));
  352. goto Exit;
  353. }
  354. pSecur32->pfnInitializeSecurityContext = (INITIALIZE_SECURITY_CONTEXT_FN)GetProcAddress (pSecur32->hInstance,
  355. #ifdef UNICODE
  356. "InitializeSecurityContextW");
  357. #else
  358. "InitializeSecurityContextA");
  359. #endif
  360. if (!pSecur32->pfnInitializeSecurityContext) {
  361. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find InitializeSecurityContext with %d."),
  362. GetLastError()));
  363. goto Exit;
  364. }
  365. pSecur32->pfnQuerySecurityContextToken = (QUERY_SECURITY_CONTEXT_TOKEN_FN)GetProcAddress (pSecur32->hInstance,
  366. "QuerySecurityContextToken");
  367. if (!pSecur32->pfnQuerySecurityContextToken) {
  368. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find QuerySecurityContextToken with %d."),
  369. GetLastError()));
  370. goto Exit;
  371. }
  372. pSecur32->pfnQuerySecurityPackageInfo = (QUERY_SECURITY_PACKAGE_INFO_FN)GetProcAddress (pSecur32->hInstance,
  373. #ifdef UNICODE
  374. "QuerySecurityPackageInfoW");
  375. #else
  376. "QuerySecurityPackageInfoA");
  377. #endif
  378. if (!pSecur32->pfnQuerySecurityPackageInfo) {
  379. DebugMsg((DM_WARNING, TEXT("LoadSecur32: Failed to find QuerySecurityPackageInfo with %d."),
  380. GetLastError()));
  381. goto Exit;
  382. }
  383. //
  384. // Success
  385. //
  386. bResult = TRUE;
  387. Exit:
  388. if (!bResult) {
  389. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  390. ev.AddArg(TEXT("secur32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  391. if ( pSecur32->hInstance ) {
  392. FreeLibrary( pSecur32->hInstance );
  393. }
  394. ZeroMemory( pSecur32, sizeof( SECUR32_API ) );
  395. pSecur32 = 0;
  396. }
  397. LeaveCriticalSection( g_ApiDLLCritSec );
  398. return pSecur32;
  399. }
  400. //*************************************************************
  401. //
  402. // LoadLDAP()
  403. //
  404. // Purpose: Loads wldap32.dll
  405. //
  406. // Parameters: pLDAP - pointer to a LDAP_API structure to
  407. // initialize
  408. //
  409. //
  410. // Return: TRUE if successful
  411. // FALSE if an error occurs
  412. //
  413. //*************************************************************
  414. PLDAP_API LoadLDAP ()
  415. {
  416. BOOL bResult = FALSE;
  417. PLDAP_API pLDAP = &g_LdapApi;
  418. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  419. return NULL;
  420. EnterCriticalSection( g_ApiDLLCritSec );
  421. if ( pLDAP->hInstance ) {
  422. //
  423. // module is already loaded and initialized
  424. //
  425. LeaveCriticalSection( g_ApiDLLCritSec );
  426. return pLDAP;
  427. }
  428. //
  429. // Load wldap32.dll
  430. //
  431. pLDAP->hInstance = LoadLibrary (TEXT("wldap32.dll"));
  432. if (!pLDAP->hInstance) {
  433. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to load wldap32 with %d."),
  434. GetLastError()));
  435. goto Exit;
  436. }
  437. pLDAP->pfnldap_open = (PFNLDAP_OPEN) GetProcAddress (pLDAP->hInstance,
  438. #ifdef UNICODE
  439. "ldap_openW");
  440. #else
  441. "ldap_openA");
  442. #endif
  443. if (!pLDAP->pfnldap_open) {
  444. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_open with %d."),
  445. GetLastError()));
  446. goto Exit;
  447. }
  448. pLDAP->pfnldap_init = (PFNLDAP_INIT) GetProcAddress (pLDAP->hInstance,
  449. #ifdef UNICODE
  450. "ldap_initW");
  451. #else
  452. "ldap_initA");
  453. #endif
  454. if (!pLDAP->pfnldap_init) {
  455. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_init with %d."),
  456. GetLastError()));
  457. goto Exit;
  458. }
  459. pLDAP->pfnldap_connect = (PFNLDAP_CONNECT) GetProcAddress (pLDAP->hInstance,
  460. "ldap_connect");
  461. if (!pLDAP->pfnldap_connect) {
  462. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_connect with %d."),
  463. GetLastError()));
  464. goto Exit;
  465. }
  466. pLDAP->pfnldap_bind_s = (PFNLDAP_BIND_S) GetProcAddress (pLDAP->hInstance,
  467. #ifdef UNICODE
  468. "ldap_bind_sW");
  469. #else
  470. "ldap_bind_sA");
  471. #endif
  472. if (!pLDAP->pfnldap_bind_s) {
  473. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_bind_s with %d."),
  474. GetLastError()));
  475. goto Exit;
  476. }
  477. pLDAP->pfnldap_search_s = (PFNLDAP_SEARCH_S) GetProcAddress (pLDAP->hInstance,
  478. #ifdef UNICODE
  479. "ldap_search_sW");
  480. #else
  481. "ldap_search_sA");
  482. #endif
  483. if (!pLDAP->pfnldap_search_s) {
  484. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_search_s with %d."),
  485. GetLastError()));
  486. goto Exit;
  487. }
  488. pLDAP->pfnldap_search_ext_s = (PFNLDAP_SEARCH_EXT_S) GetProcAddress (pLDAP->hInstance,
  489. #ifdef UNICODE
  490. "ldap_search_ext_sW");
  491. #else
  492. "ldap_search_ext_sA");
  493. #endif
  494. if (!pLDAP->pfnldap_search_ext_s) {
  495. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_search_ext_s with %d."),
  496. GetLastError()));
  497. goto Exit;
  498. }
  499. pLDAP->pfnldap_get_values = (PFNLDAP_GET_VALUES) GetProcAddress (pLDAP->hInstance,
  500. #ifdef UNICODE
  501. "ldap_get_valuesW");
  502. #else
  503. "ldap_get_valuesA");
  504. #endif
  505. if (!pLDAP->pfnldap_get_values) {
  506. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_get_values with %d."),
  507. GetLastError()));
  508. goto Exit;
  509. }
  510. pLDAP->pfnldap_value_free = (PFNLDAP_VALUE_FREE) GetProcAddress (pLDAP->hInstance,
  511. #ifdef UNICODE
  512. "ldap_value_freeW");
  513. #else
  514. "ldap_value_freeA");
  515. #endif
  516. if (!pLDAP->pfnldap_value_free) {
  517. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_value_free with %d."),
  518. GetLastError()));
  519. goto Exit;
  520. }
  521. pLDAP->pfnldap_get_values_len = (PFNLDAP_GET_VALUES_LEN) GetProcAddress (pLDAP->hInstance,
  522. #ifdef UNICODE
  523. "ldap_get_values_lenW");
  524. #else
  525. "ldap_get_values_lenA");
  526. #endif
  527. if (!pLDAP->pfnldap_get_values_len) {
  528. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_get_values_len with %d."),
  529. GetLastError()));
  530. goto Exit;
  531. }
  532. pLDAP->pfnldap_value_free_len = (PFNLDAP_VALUE_FREE_LEN) GetProcAddress (pLDAP->hInstance,
  533. "ldap_value_free_len");
  534. if (!pLDAP->pfnldap_value_free_len) {
  535. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_value_free_len with %d."),
  536. GetLastError()));
  537. goto Exit;
  538. }
  539. pLDAP->pfnldap_msgfree = (PFNLDAP_MSGFREE) GetProcAddress (pLDAP->hInstance,
  540. "ldap_msgfree");
  541. if (!pLDAP->pfnldap_msgfree) {
  542. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_msgfree with %d."),
  543. GetLastError()));
  544. goto Exit;
  545. }
  546. pLDAP->pfnldap_unbind = (PFNLDAP_UNBIND) GetProcAddress (pLDAP->hInstance,
  547. "ldap_unbind");
  548. if (!pLDAP->pfnldap_unbind) {
  549. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_unbind with %d."),
  550. GetLastError()));
  551. goto Exit;
  552. }
  553. pLDAP->pfnLdapGetLastError = (PFNLDAPGETLASTERROR) GetProcAddress (pLDAP->hInstance,
  554. "LdapGetLastError");
  555. if (!pLDAP->pfnLdapGetLastError) {
  556. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find pfnLdapGetLastError with %d."),
  557. GetLastError()));
  558. goto Exit;
  559. }
  560. pLDAP->pfnldap_first_entry = (PFNLDAP_FIRST_ENTRY) GetProcAddress (pLDAP->hInstance,
  561. "ldap_first_entry");
  562. if (!pLDAP->pfnldap_first_entry) {
  563. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_first_entry with %d."),
  564. GetLastError()));
  565. goto Exit;
  566. }
  567. pLDAP->pfnldap_next_entry = (PFNLDAP_NEXT_ENTRY) GetProcAddress (pLDAP->hInstance,
  568. "ldap_next_entry");
  569. if (!pLDAP->pfnldap_next_entry) {
  570. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_next_entry with %d."),
  571. GetLastError()));
  572. goto Exit;
  573. }
  574. pLDAP->pfnldap_get_dn = (PFNLDAP_GET_DN) GetProcAddress (pLDAP->hInstance,
  575. #ifdef UNICODE
  576. "ldap_get_dnW");
  577. #else
  578. "ldap_get_dnA");
  579. #endif
  580. if (!pLDAP->pfnldap_get_dn) {
  581. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_get_dn with %d."),
  582. GetLastError()));
  583. goto Exit;
  584. }
  585. pLDAP->pfnldap_set_option = (PFNLDAP_SET_OPTION) GetProcAddress (pLDAP->hInstance,
  586. #ifdef UNICODE
  587. "ldap_set_optionW");
  588. #else
  589. "ldap_set_option");
  590. #endif
  591. if (!pLDAP->pfnldap_set_option) {
  592. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_set_option with %d."),
  593. GetLastError()));
  594. goto Exit;
  595. }
  596. pLDAP->pfnldap_memfree = (PFNLDAP_MEMFREE) GetProcAddress (pLDAP->hInstance,
  597. #ifdef UNICODE
  598. "ldap_memfreeW");
  599. #else
  600. "ldap_memfreeA");
  601. #endif
  602. if (!pLDAP->pfnldap_memfree) {
  603. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_memfree with %d."),
  604. GetLastError()));
  605. goto Exit;
  606. }
  607. pLDAP->pfnLdapMapErrorToWin32 = (PFNLDAPMAPERRORTOWIN32) GetProcAddress (pLDAP->hInstance,
  608. "LdapMapErrorToWin32");
  609. if (!pLDAP->pfnLdapMapErrorToWin32) {
  610. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find LdapMapErrorToWin32 with %d."),
  611. GetLastError()));
  612. goto Exit;
  613. }
  614. pLDAP->pfnldap_err2string = (PFNLDAP_ERR2STRING) GetProcAddress (pLDAP->hInstance,
  615. #ifdef UNICODE
  616. "ldap_err2stringW");
  617. #else
  618. "ldap_err2stringA");
  619. #endif
  620. if (!pLDAP->pfnldap_err2string) {
  621. DebugMsg((DM_WARNING, TEXT("LoadLDAP: Failed to find ldap_err2string with %d."),
  622. GetLastError()));
  623. goto Exit;
  624. }
  625. //
  626. // Success
  627. //
  628. bResult = TRUE;
  629. Exit:
  630. if (!bResult) {
  631. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  632. ev.AddArg(TEXT("wldap32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  633. if ( pLDAP->hInstance ) {
  634. FreeLibrary( pLDAP->hInstance );
  635. }
  636. ZeroMemory( pLDAP, sizeof( LDAP_API ) );
  637. pLDAP = 0;
  638. }
  639. LeaveCriticalSection( g_ApiDLLCritSec );
  640. return pLDAP;
  641. }
  642. //*************************************************************
  643. //
  644. // LoadIcmp()
  645. //
  646. // Purpose: Loads cmp.dll
  647. //
  648. // Parameters: pIcmp - pointer to a ICMP_API structure to
  649. // initialize
  650. //
  651. //
  652. // Return: ERROR_SUCCESS if successful
  653. // result if an error occurs
  654. //
  655. //*************************************************************
  656. DWORD LoadIcmp ( PICMP_API *pIcmpOut )
  657. {
  658. DWORD dwResult = ERROR_SUCCESS;
  659. PICMP_API pIcmp = &g_IcmpApi;
  660. *pIcmpOut = NULL;
  661. dwResult = InitializeApiDLLsCritSec();
  662. if (dwResult != ERROR_SUCCESS)
  663. return dwResult;
  664. EnterCriticalSection( g_ApiDLLCritSec );
  665. if ( pIcmp->hInstance ) {
  666. //
  667. // module already loaded and initialized
  668. //
  669. LeaveCriticalSection( g_ApiDLLCritSec );
  670. *pIcmpOut = pIcmp;
  671. return ERROR_SUCCESS;
  672. }
  673. pIcmp->hInstance = LoadLibrary (TEXT("icmp.dll"));
  674. if (!pIcmp->hInstance) {
  675. dwResult = GetLastError();
  676. DebugMsg((DM_WARNING, TEXT("LoadIcmp: Failed to load icmp with %d."),
  677. GetLastError()));
  678. goto Exit;
  679. }
  680. pIcmp->pfnIcmpCreateFile = (PFNICMPCREATEFILE) GetProcAddress (pIcmp->hInstance,
  681. "IcmpCreateFile");
  682. if (!pIcmp->pfnIcmpCreateFile) {
  683. dwResult = GetLastError();
  684. DebugMsg((DM_WARNING, TEXT("LoadIcmp: Failed to find IcmpCreateFile with %d."),
  685. GetLastError()));
  686. goto Exit;
  687. }
  688. pIcmp->pfnIcmpCloseHandle = (PFNICMPCLOSEHANDLE) GetProcAddress (pIcmp->hInstance,
  689. "IcmpCloseHandle");
  690. if (!pIcmp->pfnIcmpCloseHandle) {
  691. dwResult = GetLastError();
  692. DebugMsg((DM_WARNING, TEXT("LoadIcmp: Failed to find IcmpCloseHandle with %d."),
  693. GetLastError()));
  694. goto Exit;
  695. }
  696. pIcmp->pfnIcmpSendEcho = (PFNICMPSENDECHO) GetProcAddress (pIcmp->hInstance,
  697. "IcmpSendEcho");
  698. if (!pIcmp->pfnIcmpSendEcho) {
  699. dwResult = GetLastError();
  700. DebugMsg((DM_WARNING, TEXT("LoadIcmp: Failed to find IcmpSendEcho with %d."),
  701. GetLastError()));
  702. goto Exit;
  703. }
  704. //
  705. // Success
  706. //
  707. Exit:
  708. if (dwResult != ERROR_SUCCESS) {
  709. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  710. ev.AddArg(TEXT("icmp.dll")); ev.AddArgWin32Error(dwResult); ev.Report();
  711. if ( pIcmp->hInstance ) {
  712. FreeLibrary( pIcmp->hInstance );
  713. }
  714. ZeroMemory( pIcmp, sizeof( ICMP_API ) );
  715. pIcmp = 0;
  716. }
  717. else
  718. *pIcmpOut = pIcmp;
  719. LeaveCriticalSection( g_ApiDLLCritSec );
  720. return dwResult;
  721. }
  722. //*************************************************************
  723. //
  724. // LoadWSock()
  725. //
  726. // Purpose: Loads cmp.dll
  727. //
  728. // Parameters: pWSock32 - pointer to a WSOCK32_API structure to
  729. // initialize
  730. //
  731. //
  732. // Return: TRUE if successful
  733. // FALSE if an error occurs
  734. //
  735. //*************************************************************
  736. PWSOCK32_API LoadWSock32 ()
  737. {
  738. BOOL bResult = FALSE;
  739. PWSOCK32_API pWSock32 = &g_WSock32Api;
  740. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  741. return NULL;
  742. EnterCriticalSection( g_ApiDLLCritSec );
  743. if ( pWSock32->hInstance ) {
  744. //
  745. // module already loaded and initialized
  746. //
  747. LeaveCriticalSection( g_ApiDLLCritSec );
  748. return pWSock32;
  749. }
  750. pWSock32->hInstance = LoadLibrary (TEXT("wsock32.dll"));
  751. if (!pWSock32->hInstance) {
  752. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to load wsock32 with %d."),
  753. GetLastError()));
  754. goto Exit;
  755. }
  756. pWSock32->pfninet_addr = (LPFN_INET_ADDR) GetProcAddress (pWSock32->hInstance,
  757. "inet_addr");
  758. if (!pWSock32->pfninet_addr) {
  759. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to find inet_addr with %d."),
  760. GetLastError()));
  761. goto Exit;
  762. }
  763. pWSock32->pfngethostbyname = (LPFN_GETHOSTBYNAME) GetProcAddress (pWSock32->hInstance,
  764. "gethostbyname");
  765. if (!pWSock32->pfngethostbyname) {
  766. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to find gethostbyname with %d."),
  767. GetLastError()));
  768. goto Exit;
  769. }
  770. //
  771. // Success
  772. //
  773. bResult = TRUE;
  774. Exit:
  775. if (!bResult) {
  776. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  777. ev.AddArg(TEXT("wsock32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  778. if ( pWSock32->hInstance ) {
  779. FreeLibrary( pWSock32->hInstance );
  780. }
  781. ZeroMemory( pWSock32, sizeof( WSOCK32_API ) );
  782. pWSock32 = 0;
  783. }
  784. LeaveCriticalSection( g_ApiDLLCritSec );
  785. return pWSock32;
  786. }
  787. //*************************************************************
  788. //
  789. // LoadDSAPI()
  790. //
  791. // Purpose: Loads ntdsapi.dll
  792. //
  793. // Parameters: pDSApi - pointer to a DS_API structure to initialize
  794. //
  795. // Return: TRUE if successful
  796. // FALSE if an error occurs
  797. //
  798. //*************************************************************
  799. PDS_API LoadDSApi()
  800. {
  801. BOOL bResult = FALSE;
  802. PDS_API pDSApi = &g_DsApi;
  803. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  804. return NULL;
  805. EnterCriticalSection( g_ApiDLLCritSec );
  806. if ( pDSApi->hInstance ) {
  807. //
  808. // module already loaded and initialized
  809. //
  810. LeaveCriticalSection( g_ApiDLLCritSec );
  811. return pDSApi;
  812. }
  813. pDSApi->hInstance = LoadLibrary (TEXT("ntdsapi.dll"));
  814. if (!pDSApi->hInstance) {
  815. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to load ntdsapi with %d."),
  816. GetLastError()));
  817. goto Exit;
  818. }
  819. pDSApi->pfnDsCrackNames = (PFN_DSCRACKNAMES) GetProcAddress (pDSApi->hInstance,
  820. #ifdef UNICODE
  821. "DsCrackNamesW");
  822. #else
  823. "DsCrackNamesA");
  824. #endif
  825. if (!pDSApi->pfnDsCrackNames) {
  826. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to find DsCrackNames with %d."),
  827. GetLastError()));
  828. goto Exit;
  829. }
  830. pDSApi->pfnDsFreeNameResult = (PFN_DSFREENAMERESULT) GetProcAddress (pDSApi->hInstance,
  831. #ifdef UNICODE
  832. "DsFreeNameResultW");
  833. #else
  834. "DsFreeNameResultA");
  835. #endif
  836. if (!pDSApi->pfnDsFreeNameResult) {
  837. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to find DsFreeNameResult with %d."),
  838. GetLastError()));
  839. goto Exit;
  840. }
  841. //
  842. // Success
  843. //
  844. bResult = TRUE;
  845. Exit:
  846. if (!bResult) {
  847. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  848. ev.AddArg(TEXT("ntdsapi.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  849. if ( pDSApi->hInstance ) {
  850. FreeLibrary( pDSApi->hInstance );
  851. }
  852. ZeroMemory( pDSApi, sizeof( DS_API ) );
  853. pDSApi = 0;
  854. }
  855. LeaveCriticalSection( g_ApiDLLCritSec );
  856. return pDSApi;
  857. }
  858. //*************************************************************
  859. //
  860. // LoadShell32Api()
  861. //
  862. // Purpose: Loads shell32.dll
  863. //
  864. // Parameters: pointer to hold SHELL32_API
  865. //
  866. // Return: ERROR_SUCCESS if successful
  867. // error code if not successful
  868. //
  869. //*************************************************************
  870. DWORD LoadShell32Api( PSHELL32_API *ppShell32Api )
  871. {
  872. DWORD dwErr;
  873. PSHELL32_API pShell32Api = &g_Shell32Api;
  874. dwErr = InitializeApiDLLsCritSec();
  875. if (dwErr != ERROR_SUCCESS)
  876. return dwErr;
  877. EnterCriticalSection( g_ApiDLLCritSec );
  878. if ( pShell32Api->hInstance ) {
  879. //
  880. // module already loaded and initialized
  881. //
  882. LeaveCriticalSection( g_ApiDLLCritSec );
  883. *ppShell32Api = pShell32Api;
  884. return ERROR_SUCCESS;
  885. }
  886. pShell32Api->hInstance = LoadLibrary (TEXT("shell32.dll"));
  887. if (!pShell32Api->hInstance) {
  888. dwErr = GetLastError();
  889. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to load ntdsapi with %d."),
  890. GetLastError()));
  891. goto Exit;
  892. }
  893. pShell32Api->pfnShChangeNotify = (PFNSHCHANGENOTIFY) GetProcAddress (pShell32Api->hInstance, "SHChangeNotify");
  894. if (!pShell32Api->pfnShChangeNotify) {
  895. dwErr = GetLastError();
  896. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHChangeNotify with %d."),
  897. GetLastError()));
  898. goto Exit;
  899. }
  900. pShell32Api->pfnShGetSpecialFolderPath = (PFNSHGETSPECIALFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  901. #ifdef UNICODE
  902. "SHGetSpecialFolderPathW");
  903. #else
  904. "SHGetSpecialFolderPathA");
  905. #endif
  906. if (!pShell32Api->pfnShGetSpecialFolderPath) {
  907. dwErr = GetLastError();
  908. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHGetSpecialFolderPath with %d."),
  909. GetLastError()));
  910. goto Exit;
  911. }
  912. pShell32Api->pfnShGetFolderPath = (PFNSHGETFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  913. #ifdef UNICODE
  914. "SHGetFolderPathW");
  915. #else
  916. "SHGetFolderPathA");
  917. #endif
  918. if (!pShell32Api->pfnShGetFolderPath) {
  919. dwErr = GetLastError();
  920. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHGetFolderPath with %d."),
  921. GetLastError()));
  922. goto Exit;
  923. }
  924. pShell32Api->pfnShSetFolderPath = (PFNSHSETFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  925. #ifdef UNICODE
  926. (LPCSTR)SHSetFolderW_Ord);
  927. #else
  928. (LPCSTR)SHSetFolderA_Ord);
  929. #endif
  930. if (!pShell32Api->pfnShSetFolderPath) {
  931. dwErr = GetLastError();
  932. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHSetFolderPath with %d."),
  933. GetLastError()));
  934. goto Exit;
  935. }
  936. pShell32Api->pfnShSetLocalizedName = (PFNSHSETLOCALIZEDNAME)
  937. GetProcAddress (pShell32Api->hInstance, "SHSetLocalizedName");
  938. if (!pShell32Api->pfnShSetLocalizedName) {
  939. dwErr = GetLastError();
  940. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHSetLocalizedName with %d."),
  941. GetLastError()));
  942. goto Exit;
  943. }
  944. //
  945. // Success
  946. //
  947. dwErr = ERROR_SUCCESS;
  948. Exit:
  949. if (dwErr != ERROR_SUCCESS) {
  950. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  951. ev.AddArg(TEXT("shell32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  952. if ( pShell32Api->hInstance ) {
  953. FreeLibrary( pShell32Api->hInstance );
  954. }
  955. ZeroMemory( pShell32Api, sizeof( SHELL32_API ) );
  956. pShell32Api = 0;
  957. }
  958. LeaveCriticalSection( g_ApiDLLCritSec );
  959. *ppShell32Api = pShell32Api;
  960. return dwErr;
  961. }
  962. //*************************************************************
  963. //
  964. // LoadShwapiAPI()
  965. //
  966. // Purpose: Loads shlwapi.dll
  967. //
  968. // Parameters: none
  969. //
  970. // Return: pointer to SHLWAPI_API
  971. //
  972. //*************************************************************
  973. PSHLWAPI_API LoadShlwapiApi()
  974. {
  975. BOOL bResult = FALSE;
  976. PSHLWAPI_API pShlwapiApi = &g_ShlwapiApi;
  977. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  978. return NULL;
  979. EnterCriticalSection( g_ApiDLLCritSec );
  980. if ( pShlwapiApi->hInstance ) {
  981. //
  982. // module already loaded and initialized
  983. //
  984. LeaveCriticalSection( g_ApiDLLCritSec );
  985. return pShlwapiApi;
  986. }
  987. pShlwapiApi->hInstance = LoadLibrary (TEXT("shlwapi.dll"));
  988. if (!pShlwapiApi->hInstance) {
  989. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to load ntdsapi with %d."),
  990. GetLastError()));
  991. goto Exit;
  992. }
  993. pShlwapiApi->pfnPathGetArgs = (PFNPATHGETARGS) GetProcAddress (pShlwapiApi->hInstance,
  994. #ifdef UNICODE
  995. "PathGetArgsW");
  996. #else
  997. "PathGetArgsA");
  998. #endif
  999. if (!pShlwapiApi->pfnPathGetArgs) {
  1000. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find PathGetArgs with %d."),
  1001. GetLastError()));
  1002. goto Exit;
  1003. }
  1004. pShlwapiApi->pfnPathUnquoteSpaces = (PFNPATHUNQUOTESPACES) GetProcAddress (pShlwapiApi->hInstance,
  1005. #ifdef UNICODE
  1006. "PathUnquoteSpacesW");
  1007. #else
  1008. "PathUnquoteSpacesA");
  1009. #endif
  1010. if (!pShlwapiApi->pfnPathUnquoteSpaces) {
  1011. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find PathUnquoteSpaces with %d."),
  1012. GetLastError()));
  1013. goto Exit;
  1014. }
  1015. //
  1016. // Success
  1017. //
  1018. bResult = TRUE;
  1019. Exit:
  1020. if (!bResult) {
  1021. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1022. ev.AddArg(TEXT("shlwapi.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1023. if ( pShlwapiApi->hInstance ) {
  1024. FreeLibrary( pShlwapiApi->hInstance );
  1025. }
  1026. ZeroMemory( pShlwapiApi, sizeof( SHLWAPI_API ) );
  1027. pShlwapiApi = 0;
  1028. }
  1029. LeaveCriticalSection( g_ApiDLLCritSec );
  1030. return pShlwapiApi;
  1031. }
  1032. //*************************************************************
  1033. //
  1034. // LoadOle32Api()
  1035. //
  1036. // Purpose: Loads ole32.dll
  1037. //
  1038. // Parameters: none
  1039. //
  1040. // Return: pointer to OLE32_API
  1041. //
  1042. //*************************************************************
  1043. POLE32_API LoadOle32Api()
  1044. {
  1045. BOOL bResult = FALSE;
  1046. OLE32_API *pOle32Api = &g_Ole32Api;
  1047. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1048. return NULL;
  1049. EnterCriticalSection( g_ApiDLLCritSec );
  1050. if ( pOle32Api->hInstance ) {
  1051. //
  1052. // module already loaded and initialized
  1053. //
  1054. LeaveCriticalSection( g_ApiDLLCritSec );
  1055. return pOle32Api;
  1056. }
  1057. pOle32Api->hInstance = LoadLibrary (TEXT("ole32.dll"));
  1058. if (!pOle32Api->hInstance) {
  1059. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to load ole32.dll with %d."),
  1060. GetLastError()));
  1061. goto Exit;
  1062. }
  1063. pOle32Api->pfnCoCreateInstance = (PFNCOCREATEINSTANCE) GetProcAddress (pOle32Api->hInstance,
  1064. "CoCreateInstance");
  1065. if (!pOle32Api->pfnCoCreateInstance) {
  1066. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoCreateInstance with %d."),
  1067. GetLastError()));
  1068. goto Exit;
  1069. }
  1070. pOle32Api->pfnCoInitializeEx = (PFNCOINITIALIZEEX) GetProcAddress (pOle32Api->hInstance,
  1071. "CoInitializeEx");
  1072. if (!pOle32Api->pfnCoInitializeEx) {
  1073. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoInitializeEx with %d."),
  1074. GetLastError()));
  1075. goto Exit;
  1076. }
  1077. pOle32Api->pfnCoUnInitialize = (PFNCOUNINITIALIZE) GetProcAddress (pOle32Api->hInstance,
  1078. "CoUninitialize");
  1079. if (!pOle32Api->pfnCoUnInitialize) {
  1080. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoUnInitialize with %d."),
  1081. GetLastError()));
  1082. goto Exit;
  1083. }
  1084. pOle32Api->pfnCoCreateGuid = (PFNCOCREATEGUID) GetProcAddress (pOle32Api->hInstance,
  1085. "CoCreateGuid");
  1086. if (!pOle32Api->pfnCoCreateGuid) {
  1087. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoCreateGuid with %d."),
  1088. GetLastError()));
  1089. goto Exit;
  1090. }
  1091. //
  1092. // Success
  1093. //
  1094. bResult = TRUE;
  1095. Exit:
  1096. if (!bResult) {
  1097. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1098. ev.AddArg(TEXT("ole32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1099. if ( pOle32Api->hInstance ) {
  1100. FreeLibrary( pOle32Api->hInstance );
  1101. }
  1102. ZeroMemory( pOle32Api, sizeof( OLE32_API ) );
  1103. pOle32Api = 0;
  1104. }
  1105. LeaveCriticalSection( g_ApiDLLCritSec );
  1106. return pOle32Api;
  1107. }
  1108. //*************************************************************
  1109. //
  1110. // LoadGpTextApi()
  1111. //
  1112. // Purpose: Loads gptext.dll
  1113. //
  1114. // Parameters: none
  1115. //
  1116. // Return: pointer to GPTEXT_API
  1117. //
  1118. //*************************************************************
  1119. GPTEXT_API * LoadGpTextApi()
  1120. {
  1121. BOOL bResult = FALSE;
  1122. GPTEXT_API *pGpTextApi = &g_GpTextApi;
  1123. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1124. return NULL;
  1125. EnterCriticalSection( g_ApiDLLCritSec );
  1126. if ( pGpTextApi->hInstance ) {
  1127. //
  1128. // module already loaded and initialized
  1129. //
  1130. LeaveCriticalSection( g_ApiDLLCritSec );
  1131. return pGpTextApi;
  1132. }
  1133. pGpTextApi->hInstance = LoadLibrary (TEXT("gptext.dll"));
  1134. if (!pGpTextApi->hInstance) {
  1135. DebugMsg((DM_WARNING, TEXT("LoadGpTextApi: Failed to load gptext.dll with %d."),
  1136. GetLastError()));
  1137. goto Exit;
  1138. }
  1139. pGpTextApi->pfnScrRegGPOListToWbem = (PFNSCRREGGPOLISTTOWBEM) GetProcAddress (pGpTextApi->hInstance,
  1140. "ScrRegGPOListToWbem");
  1141. if (!pGpTextApi->pfnScrRegGPOListToWbem) {
  1142. DebugMsg((DM_WARNING, TEXT("LoadGpTextApi: Failed to find ScrRegGPOListToWbem with %d."),
  1143. GetLastError()));
  1144. goto Exit;
  1145. }
  1146. //
  1147. // Success
  1148. //
  1149. bResult = TRUE;
  1150. Exit:
  1151. if (!bResult) {
  1152. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1153. ev.AddArg(TEXT("gptext.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1154. if ( pGpTextApi->hInstance ) {
  1155. FreeLibrary( pGpTextApi->hInstance );
  1156. }
  1157. ZeroMemory( pGpTextApi, sizeof( GPTEXT_API ) );
  1158. pGpTextApi = 0;
  1159. }
  1160. LeaveCriticalSection( g_ApiDLLCritSec );
  1161. return pGpTextApi;
  1162. }
  1163. PIPHLPAPI_API LoadIpHlpApi()
  1164. {
  1165. bool bResult = false;
  1166. PIPHLPAPI_API pIpHlpApi = &g_IpHlpApi;
  1167. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1168. return NULL;
  1169. EnterCriticalSection( g_ApiDLLCritSec );
  1170. if ( pIpHlpApi->hInstance )
  1171. {
  1172. //
  1173. // module is already loaded and initialized
  1174. //
  1175. LeaveCriticalSection( g_ApiDLLCritSec );
  1176. return pIpHlpApi;
  1177. }
  1178. pIpHlpApi->hInstance = LoadLibrary( L"iphlpapi.dll" );
  1179. if ( !pIpHlpApi->hInstance )
  1180. {
  1181. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to load iphlpapi with %d.", GetLastError()));
  1182. goto Exit;
  1183. }
  1184. pIpHlpApi->pfnGetBestInterface = (PFNGETBESTINTERFACE) GetProcAddress( pIpHlpApi->hInstance, "GetBestInterface" );
  1185. if ( !pIpHlpApi->pfnGetBestInterface )
  1186. {
  1187. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetBestInterface with %d.", GetLastError()));
  1188. goto Exit;
  1189. }
  1190. pIpHlpApi->pfnGetIfEntry = (PFNGETIFENTRY) GetProcAddress (pIpHlpApi->hInstance, "GetIfEntry" );
  1191. if ( !pIpHlpApi->pfnGetIfEntry )
  1192. {
  1193. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetIfEntry with %d.", GetLastError()));
  1194. goto Exit;
  1195. }
  1196. pIpHlpApi->pfnGetAdapterIndex = (PFNGETADAPTERINDEX) GetProcAddress (pIpHlpApi->hInstance, "GetAdapterIndex" );
  1197. if ( !pIpHlpApi->pfnGetAdapterIndex )
  1198. {
  1199. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetAdapterIndex with %d.", GetLastError()));
  1200. goto Exit;
  1201. }
  1202. bResult = true;
  1203. Exit:
  1204. if ( !bResult )
  1205. {
  1206. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1207. ev.AddArg( L"iphlpapi.dll" );
  1208. ev.AddArgWin32Error( GetLastError() );
  1209. ev.Report();
  1210. if ( pIpHlpApi->hInstance )
  1211. {
  1212. FreeLibrary( pIpHlpApi->hInstance );
  1213. }
  1214. ZeroMemory( pIpHlpApi, sizeof( IPHLPAPI_API ) );
  1215. pIpHlpApi = 0;
  1216. }
  1217. LeaveCriticalSection( g_ApiDLLCritSec );
  1218. return pIpHlpApi;
  1219. }
  1220. //*************************************************************
  1221. //
  1222. // Loadws2_32Api()
  1223. //
  1224. // Purpose: Loads ws2_32.dll
  1225. //
  1226. // Parameters: none
  1227. //
  1228. // Return: pointer to WS2_32_API
  1229. //
  1230. //*************************************************************
  1231. WS2_32_API * Loadws2_32Api()
  1232. {
  1233. BOOL bResult = FALSE;
  1234. WS2_32_API *pws2_32Api = &g_ws2_32Api;
  1235. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1236. return NULL;
  1237. EnterCriticalSection( g_ApiDLLCritSec );
  1238. if ( pws2_32Api->hInstance ) {
  1239. //
  1240. // module already loaded and initialized
  1241. //
  1242. LeaveCriticalSection( g_ApiDLLCritSec );
  1243. return pws2_32Api;
  1244. }
  1245. pws2_32Api->hInstance = LoadLibrary (TEXT("ws2_32.dll"));
  1246. if (!pws2_32Api->hInstance) {
  1247. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to load ws2_32.dll with %d."),
  1248. GetLastError()));
  1249. goto Exit;
  1250. }
  1251. pws2_32Api->pfnWSALookupServiceBegin = (PFNWSALOOKUPSERVICEBEGIN) GetProcAddress (pws2_32Api->hInstance,
  1252. "WSALookupServiceBeginW");
  1253. if (!pws2_32Api->pfnWSALookupServiceBegin) {
  1254. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceBegin with %d."),
  1255. GetLastError()));
  1256. goto Exit;
  1257. }
  1258. pws2_32Api->pfnWSALookupServiceNext = (PFNWSALOOKUPSERVICENEXT) GetProcAddress (pws2_32Api->hInstance,
  1259. "WSALookupServiceNextW");
  1260. if (!pws2_32Api->pfnWSALookupServiceNext) {
  1261. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceNext with %d."),
  1262. GetLastError()));
  1263. goto Exit;
  1264. }
  1265. pws2_32Api->pfnWSALookupServiceEnd = (PFNWSALOOKUPSERVICEEND) GetProcAddress (pws2_32Api->hInstance,
  1266. "WSALookupServiceEnd");
  1267. if (!pws2_32Api->pfnWSALookupServiceEnd) {
  1268. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceEnd with %d."),
  1269. GetLastError()));
  1270. goto Exit;
  1271. }
  1272. pws2_32Api->pfnWSAStartup = (PFNWSASTARTUP) GetProcAddress (pws2_32Api->hInstance,
  1273. "WSAStartup");
  1274. if (!pws2_32Api->pfnWSAStartup) {
  1275. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSAStartup with %d."),
  1276. GetLastError()));
  1277. goto Exit;
  1278. }
  1279. pws2_32Api->pfnWSACleanup = (PFNWSACLEANUP) GetProcAddress (pws2_32Api->hInstance,
  1280. "WSACleanup");
  1281. if (!pws2_32Api->pfnWSACleanup) {
  1282. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSACleanup with %d."),
  1283. GetLastError()));
  1284. goto Exit;
  1285. }
  1286. pws2_32Api->pfnWSAGetLastError = (PFNWSAGETLASTERROR) GetProcAddress (pws2_32Api->hInstance,
  1287. "WSAGetLastError");
  1288. if (!pws2_32Api->pfnWSAGetLastError) {
  1289. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSAGetLastError with %d."),
  1290. GetLastError()));
  1291. goto Exit;
  1292. }
  1293. //
  1294. // Success
  1295. //
  1296. bResult = TRUE;
  1297. Exit:
  1298. if (!bResult) {
  1299. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1300. ev.AddArg(TEXT("ws2_32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1301. if ( pws2_32Api->hInstance ) {
  1302. FreeLibrary( pws2_32Api->hInstance );
  1303. }
  1304. ZeroMemory( pws2_32Api, sizeof( WS2_32_API ) );
  1305. pws2_32Api = 0;
  1306. }
  1307. LeaveCriticalSection( g_ApiDLLCritSec );
  1308. return pws2_32Api;
  1309. }