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.

1702 lines
49 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 = NULL;
  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 = GetExceptionCode();
  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 WINSOCK DLL and store entry point to global
  727. // structure. Also calls initialize funtion for WINSOCK.
  728. //
  729. // Parameters: pWSock32 - pointer to a WSOCK32_API structure to
  730. // initialize
  731. //
  732. //
  733. // Return: TRUE if successful
  734. // FALSE if an error occurs
  735. //
  736. //*************************************************************
  737. PWSOCK32_API LoadWSock32 ()
  738. {
  739. BOOL bResult = FALSE;
  740. PWSOCK32_API pWSock32 = &g_WSock32Api;
  741. WORD wVersionRequested;
  742. int err;
  743. LPFN_WSASTARTUP pfnwsastartup;
  744. WSADATA wsaData;
  745. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  746. return NULL;
  747. EnterCriticalSection( g_ApiDLLCritSec );
  748. if ( pWSock32->hInstance ) {
  749. //
  750. // module already loaded and initialized
  751. //
  752. LeaveCriticalSection( g_ApiDLLCritSec );
  753. return pWSock32;
  754. }
  755. pWSock32->hInstance = LoadLibrary (TEXT("wsock32.dll"));
  756. if (!pWSock32->hInstance) {
  757. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to load wsock32 with %d."),
  758. GetLastError()));
  759. goto Exit;
  760. }
  761. pWSock32->pfninet_addr = (LPFN_INET_ADDR) GetProcAddress (pWSock32->hInstance,
  762. "inet_addr");
  763. if (!pWSock32->pfninet_addr) {
  764. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to find inet_addr with %d."),
  765. GetLastError()));
  766. goto Exit;
  767. }
  768. pWSock32->pfngethostbyname = (LPFN_GETHOSTBYNAME) GetProcAddress (pWSock32->hInstance,
  769. "gethostbyname");
  770. if (!pWSock32->pfngethostbyname) {
  771. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to find gethostbyname with %d."),
  772. GetLastError()));
  773. goto Exit;
  774. }
  775. pfnwsastartup = (LPFN_WSASTARTUP) GetProcAddress (pWSock32->hInstance,"WSAStartup");
  776. if (!pfnwsastartup) {
  777. DebugMsg((DM_WARNING, TEXT("LoadWSock32: Failed to find WSAStartup with %d."),
  778. GetLastError()));
  779. goto Exit;
  780. }
  781. wVersionRequested = WINSOCK_VERSION;
  782. err = pfnwsastartup(wVersionRequested, &wsaData);
  783. if (err)
  784. {
  785. SetLastError(err);
  786. DebugMsg((DM_WARNING, TEXT("LoadWSock32: WSAStartup returned 0x%08x"), err));
  787. goto Exit;
  788. }
  789. //
  790. // Success
  791. //
  792. bResult = TRUE;
  793. Exit:
  794. if (!bResult) {
  795. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  796. ev.AddArg(TEXT("wsock32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  797. if ( pWSock32->hInstance ) {
  798. FreeLibrary( pWSock32->hInstance );
  799. }
  800. ZeroMemory( pWSock32, sizeof( WSOCK32_API ) );
  801. pWSock32 = 0;
  802. }
  803. LeaveCriticalSection( g_ApiDLLCritSec );
  804. return pWSock32;
  805. }
  806. //*************************************************************
  807. //
  808. // LoadDSAPI()
  809. //
  810. // Purpose: Loads ntdsapi.dll
  811. //
  812. // Parameters: pDSApi - pointer to a DS_API structure to initialize
  813. //
  814. // Return: TRUE if successful
  815. // FALSE if an error occurs
  816. //
  817. //*************************************************************
  818. PDS_API LoadDSApi()
  819. {
  820. BOOL bResult = FALSE;
  821. PDS_API pDSApi = &g_DsApi;
  822. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  823. return NULL;
  824. EnterCriticalSection( g_ApiDLLCritSec );
  825. if ( pDSApi->hInstance ) {
  826. //
  827. // module already loaded and initialized
  828. //
  829. LeaveCriticalSection( g_ApiDLLCritSec );
  830. return pDSApi;
  831. }
  832. pDSApi->hInstance = LoadLibrary (TEXT("ntdsapi.dll"));
  833. if (!pDSApi->hInstance) {
  834. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to load ntdsapi with %d."),
  835. GetLastError()));
  836. goto Exit;
  837. }
  838. pDSApi->pfnDsCrackNames = (PFN_DSCRACKNAMES) GetProcAddress (pDSApi->hInstance,
  839. #ifdef UNICODE
  840. "DsCrackNamesW");
  841. #else
  842. "DsCrackNamesA");
  843. #endif
  844. if (!pDSApi->pfnDsCrackNames) {
  845. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to find DsCrackNames with %d."),
  846. GetLastError()));
  847. goto Exit;
  848. }
  849. pDSApi->pfnDsFreeNameResult = (PFN_DSFREENAMERESULT) GetProcAddress (pDSApi->hInstance,
  850. #ifdef UNICODE
  851. "DsFreeNameResultW");
  852. #else
  853. "DsFreeNameResultA");
  854. #endif
  855. if (!pDSApi->pfnDsFreeNameResult) {
  856. DebugMsg((DM_WARNING, TEXT("LoadDSApi: Failed to find DsFreeNameResult with %d."),
  857. GetLastError()));
  858. goto Exit;
  859. }
  860. //
  861. // Success
  862. //
  863. bResult = TRUE;
  864. Exit:
  865. if (!bResult) {
  866. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  867. ev.AddArg(TEXT("ntdsapi.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  868. if ( pDSApi->hInstance ) {
  869. FreeLibrary( pDSApi->hInstance );
  870. }
  871. ZeroMemory( pDSApi, sizeof( DS_API ) );
  872. pDSApi = 0;
  873. }
  874. LeaveCriticalSection( g_ApiDLLCritSec );
  875. return pDSApi;
  876. }
  877. //*************************************************************
  878. //
  879. // LoadShell32Api()
  880. //
  881. // Purpose: Loads shell32.dll
  882. //
  883. // Parameters: pointer to hold SHELL32_API
  884. //
  885. // Return: ERROR_SUCCESS if successful
  886. // error code if not successful
  887. //
  888. //*************************************************************
  889. DWORD LoadShell32Api( PSHELL32_API *ppShell32Api )
  890. {
  891. DWORD dwErr;
  892. PSHELL32_API pShell32Api = &g_Shell32Api;
  893. dwErr = InitializeApiDLLsCritSec();
  894. if (dwErr != ERROR_SUCCESS)
  895. return dwErr;
  896. EnterCriticalSection( g_ApiDLLCritSec );
  897. if ( pShell32Api->hInstance ) {
  898. //
  899. // module already loaded and initialized
  900. //
  901. LeaveCriticalSection( g_ApiDLLCritSec );
  902. *ppShell32Api = pShell32Api;
  903. return ERROR_SUCCESS;
  904. }
  905. pShell32Api->hInstance = LoadLibrary (TEXT("shell32.dll"));
  906. if (!pShell32Api->hInstance) {
  907. dwErr = GetLastError();
  908. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to load ntdsapi with %d."),
  909. GetLastError()));
  910. goto Exit;
  911. }
  912. pShell32Api->pfnShChangeNotify = (PFNSHCHANGENOTIFY) GetProcAddress (pShell32Api->hInstance, "SHChangeNotify");
  913. if (!pShell32Api->pfnShChangeNotify) {
  914. dwErr = GetLastError();
  915. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHChangeNotify with %d."),
  916. GetLastError()));
  917. goto Exit;
  918. }
  919. pShell32Api->pfnShGetSpecialFolderPath = (PFNSHGETSPECIALFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  920. #ifdef UNICODE
  921. "SHGetSpecialFolderPathW");
  922. #else
  923. "SHGetSpecialFolderPathA");
  924. #endif
  925. if (!pShell32Api->pfnShGetSpecialFolderPath) {
  926. dwErr = GetLastError();
  927. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHGetSpecialFolderPath with %d."),
  928. GetLastError()));
  929. goto Exit;
  930. }
  931. pShell32Api->pfnShGetFolderPath = (PFNSHGETFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  932. #ifdef UNICODE
  933. "SHGetFolderPathW");
  934. #else
  935. "SHGetFolderPathA");
  936. #endif
  937. if (!pShell32Api->pfnShGetFolderPath) {
  938. dwErr = GetLastError();
  939. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHGetFolderPath with %d."),
  940. GetLastError()));
  941. goto Exit;
  942. }
  943. pShell32Api->pfnShSetFolderPath = (PFNSHSETFOLDERPATH) GetProcAddress (pShell32Api->hInstance,
  944. #ifdef UNICODE
  945. (LPCSTR)SHSetFolderW_Ord);
  946. #else
  947. (LPCSTR)SHSetFolderA_Ord);
  948. #endif
  949. if (!pShell32Api->pfnShSetFolderPath) {
  950. dwErr = GetLastError();
  951. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHSetFolderPath with %d."),
  952. GetLastError()));
  953. goto Exit;
  954. }
  955. pShell32Api->pfnShSetLocalizedName = (PFNSHSETLOCALIZEDNAME)
  956. GetProcAddress (pShell32Api->hInstance, "SHSetLocalizedName");
  957. if (!pShell32Api->pfnShSetLocalizedName) {
  958. dwErr = GetLastError();
  959. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find SHSetLocalizedName with %d."),
  960. GetLastError()));
  961. goto Exit;
  962. }
  963. //
  964. // Success
  965. //
  966. dwErr = ERROR_SUCCESS;
  967. Exit:
  968. if (dwErr != ERROR_SUCCESS) {
  969. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  970. ev.AddArg(TEXT("shell32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  971. if ( pShell32Api->hInstance ) {
  972. FreeLibrary( pShell32Api->hInstance );
  973. }
  974. ZeroMemory( pShell32Api, sizeof( SHELL32_API ) );
  975. pShell32Api = 0;
  976. }
  977. LeaveCriticalSection( g_ApiDLLCritSec );
  978. *ppShell32Api = pShell32Api;
  979. return dwErr;
  980. }
  981. //*************************************************************
  982. //
  983. // LoadShwapiAPI()
  984. //
  985. // Purpose: Loads shlwapi.dll
  986. //
  987. // Parameters: none
  988. //
  989. // Return: pointer to SHLWAPI_API
  990. //
  991. //*************************************************************
  992. PSHLWAPI_API LoadShlwapiApi()
  993. {
  994. BOOL bResult = FALSE;
  995. PSHLWAPI_API pShlwapiApi = &g_ShlwapiApi;
  996. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  997. return NULL;
  998. EnterCriticalSection( g_ApiDLLCritSec );
  999. if ( pShlwapiApi->hInstance ) {
  1000. //
  1001. // module already loaded and initialized
  1002. //
  1003. LeaveCriticalSection( g_ApiDLLCritSec );
  1004. return pShlwapiApi;
  1005. }
  1006. pShlwapiApi->hInstance = LoadLibrary (TEXT("shlwapi.dll"));
  1007. if (!pShlwapiApi->hInstance) {
  1008. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to load ntdsapi with %d."),
  1009. GetLastError()));
  1010. goto Exit;
  1011. }
  1012. pShlwapiApi->pfnPathGetArgs = (PFNPATHGETARGS) GetProcAddress (pShlwapiApi->hInstance,
  1013. #ifdef UNICODE
  1014. "PathGetArgsW");
  1015. #else
  1016. "PathGetArgsA");
  1017. #endif
  1018. if (!pShlwapiApi->pfnPathGetArgs) {
  1019. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find PathGetArgs with %d."),
  1020. GetLastError()));
  1021. goto Exit;
  1022. }
  1023. pShlwapiApi->pfnPathUnquoteSpaces = (PFNPATHUNQUOTESPACES) GetProcAddress (pShlwapiApi->hInstance,
  1024. #ifdef UNICODE
  1025. "PathUnquoteSpacesW");
  1026. #else
  1027. "PathUnquoteSpacesA");
  1028. #endif
  1029. if (!pShlwapiApi->pfnPathUnquoteSpaces) {
  1030. DebugMsg((DM_WARNING, TEXT("LoadShlwapiApi: Failed to find PathUnquoteSpaces with %d."),
  1031. GetLastError()));
  1032. goto Exit;
  1033. }
  1034. //
  1035. // Success
  1036. //
  1037. bResult = TRUE;
  1038. Exit:
  1039. if (!bResult) {
  1040. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1041. ev.AddArg(TEXT("shlwapi.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1042. if ( pShlwapiApi->hInstance ) {
  1043. FreeLibrary( pShlwapiApi->hInstance );
  1044. }
  1045. ZeroMemory( pShlwapiApi, sizeof( SHLWAPI_API ) );
  1046. pShlwapiApi = 0;
  1047. }
  1048. LeaveCriticalSection( g_ApiDLLCritSec );
  1049. return pShlwapiApi;
  1050. }
  1051. //*************************************************************
  1052. //
  1053. // LoadOle32Api()
  1054. //
  1055. // Purpose: Loads ole32.dll
  1056. //
  1057. // Parameters: none
  1058. //
  1059. // Return: pointer to OLE32_API
  1060. //
  1061. //*************************************************************
  1062. POLE32_API LoadOle32Api()
  1063. {
  1064. BOOL bResult = FALSE;
  1065. OLE32_API *pOle32Api = &g_Ole32Api;
  1066. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1067. return NULL;
  1068. EnterCriticalSection( g_ApiDLLCritSec );
  1069. if ( pOle32Api->hInstance ) {
  1070. //
  1071. // module already loaded and initialized
  1072. //
  1073. LeaveCriticalSection( g_ApiDLLCritSec );
  1074. return pOle32Api;
  1075. }
  1076. pOle32Api->hInstance = LoadLibrary (TEXT("ole32.dll"));
  1077. if (!pOle32Api->hInstance) {
  1078. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to load ole32.dll with %d."),
  1079. GetLastError()));
  1080. goto Exit;
  1081. }
  1082. pOle32Api->pfnCoCreateInstance = (PFNCOCREATEINSTANCE) GetProcAddress (pOle32Api->hInstance,
  1083. "CoCreateInstance");
  1084. if (!pOle32Api->pfnCoCreateInstance) {
  1085. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoCreateInstance with %d."),
  1086. GetLastError()));
  1087. goto Exit;
  1088. }
  1089. pOle32Api->pfnCoInitializeEx = (PFNCOINITIALIZEEX) GetProcAddress (pOle32Api->hInstance,
  1090. "CoInitializeEx");
  1091. if (!pOle32Api->pfnCoInitializeEx) {
  1092. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoInitializeEx with %d."),
  1093. GetLastError()));
  1094. goto Exit;
  1095. }
  1096. pOle32Api->pfnCoUnInitialize = (PFNCOUNINITIALIZE) GetProcAddress (pOle32Api->hInstance,
  1097. "CoUninitialize");
  1098. if (!pOle32Api->pfnCoUnInitialize) {
  1099. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoUnInitialize with %d."),
  1100. GetLastError()));
  1101. goto Exit;
  1102. }
  1103. pOle32Api->pfnCoCreateGuid = (PFNCOCREATEGUID) GetProcAddress (pOle32Api->hInstance,
  1104. "CoCreateGuid");
  1105. if (!pOle32Api->pfnCoCreateGuid) {
  1106. DebugMsg((DM_WARNING, TEXT("LoadOle32Api: Failed to find CoCreateGuid with %d."),
  1107. GetLastError()));
  1108. goto Exit;
  1109. }
  1110. //
  1111. // Success
  1112. //
  1113. bResult = TRUE;
  1114. Exit:
  1115. if (!bResult) {
  1116. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1117. ev.AddArg(TEXT("ole32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1118. if ( pOle32Api->hInstance ) {
  1119. FreeLibrary( pOle32Api->hInstance );
  1120. }
  1121. ZeroMemory( pOle32Api, sizeof( OLE32_API ) );
  1122. pOle32Api = 0;
  1123. }
  1124. LeaveCriticalSection( g_ApiDLLCritSec );
  1125. return pOle32Api;
  1126. }
  1127. //*************************************************************
  1128. //
  1129. // LoadGpTextApi()
  1130. //
  1131. // Purpose: Loads gptext.dll
  1132. //
  1133. // Parameters: none
  1134. //
  1135. // Return: pointer to GPTEXT_API
  1136. //
  1137. //*************************************************************
  1138. GPTEXT_API * LoadGpTextApi()
  1139. {
  1140. BOOL bResult = FALSE;
  1141. GPTEXT_API *pGpTextApi = &g_GpTextApi;
  1142. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1143. return NULL;
  1144. EnterCriticalSection( g_ApiDLLCritSec );
  1145. if ( pGpTextApi->hInstance ) {
  1146. //
  1147. // module already loaded and initialized
  1148. //
  1149. LeaveCriticalSection( g_ApiDLLCritSec );
  1150. return pGpTextApi;
  1151. }
  1152. pGpTextApi->hInstance = LoadLibrary (TEXT("gptext.dll"));
  1153. if (!pGpTextApi->hInstance) {
  1154. DebugMsg((DM_WARNING, TEXT("LoadGpTextApi: Failed to load gptext.dll with %d."),
  1155. GetLastError()));
  1156. goto Exit;
  1157. }
  1158. pGpTextApi->pfnScrRegGPOListToWbem = (PFNSCRREGGPOLISTTOWBEM) GetProcAddress (pGpTextApi->hInstance,
  1159. "ScrRegGPOListToWbem");
  1160. if (!pGpTextApi->pfnScrRegGPOListToWbem) {
  1161. DebugMsg((DM_WARNING, TEXT("LoadGpTextApi: Failed to find ScrRegGPOListToWbem with %d."),
  1162. GetLastError()));
  1163. goto Exit;
  1164. }
  1165. //
  1166. // Success
  1167. //
  1168. bResult = TRUE;
  1169. Exit:
  1170. if (!bResult) {
  1171. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1172. ev.AddArg(TEXT("gptext.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1173. if ( pGpTextApi->hInstance ) {
  1174. FreeLibrary( pGpTextApi->hInstance );
  1175. }
  1176. ZeroMemory( pGpTextApi, sizeof( GPTEXT_API ) );
  1177. pGpTextApi = 0;
  1178. }
  1179. LeaveCriticalSection( g_ApiDLLCritSec );
  1180. return pGpTextApi;
  1181. }
  1182. PIPHLPAPI_API LoadIpHlpApi()
  1183. {
  1184. bool bResult = false;
  1185. PIPHLPAPI_API pIpHlpApi = &g_IpHlpApi;
  1186. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1187. return NULL;
  1188. EnterCriticalSection( g_ApiDLLCritSec );
  1189. if ( pIpHlpApi->hInstance )
  1190. {
  1191. //
  1192. // module is already loaded and initialized
  1193. //
  1194. LeaveCriticalSection( g_ApiDLLCritSec );
  1195. return pIpHlpApi;
  1196. }
  1197. pIpHlpApi->hInstance = LoadLibrary( L"iphlpapi.dll" );
  1198. if ( !pIpHlpApi->hInstance )
  1199. {
  1200. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to load iphlpapi with %d.", GetLastError()));
  1201. goto Exit;
  1202. }
  1203. pIpHlpApi->pfnGetBestInterface = (PFNGETBESTINTERFACE) GetProcAddress( pIpHlpApi->hInstance, "GetBestInterface" );
  1204. if ( !pIpHlpApi->pfnGetBestInterface )
  1205. {
  1206. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetBestInterface with %d.", GetLastError()));
  1207. goto Exit;
  1208. }
  1209. pIpHlpApi->pfnGetIfEntry = (PFNGETIFENTRY) GetProcAddress (pIpHlpApi->hInstance, "GetIfEntry" );
  1210. if ( !pIpHlpApi->pfnGetIfEntry )
  1211. {
  1212. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetIfEntry with %d.", GetLastError()));
  1213. goto Exit;
  1214. }
  1215. pIpHlpApi->pfnGetAdapterIndex = (PFNGETADAPTERINDEX) GetProcAddress (pIpHlpApi->hInstance, "GetAdapterIndex" );
  1216. if ( !pIpHlpApi->pfnGetAdapterIndex )
  1217. {
  1218. DebugMsg((DM_WARNING, L"LoadIpHlpApi: Failed to find GetAdapterIndex with %d.", GetLastError()));
  1219. goto Exit;
  1220. }
  1221. bResult = true;
  1222. Exit:
  1223. if ( !bResult )
  1224. {
  1225. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1226. ev.AddArg( L"iphlpapi.dll" );
  1227. ev.AddArgWin32Error( GetLastError() );
  1228. ev.Report();
  1229. if ( pIpHlpApi->hInstance )
  1230. {
  1231. FreeLibrary( pIpHlpApi->hInstance );
  1232. }
  1233. ZeroMemory( pIpHlpApi, sizeof( IPHLPAPI_API ) );
  1234. pIpHlpApi = 0;
  1235. }
  1236. LeaveCriticalSection( g_ApiDLLCritSec );
  1237. return pIpHlpApi;
  1238. }
  1239. //*************************************************************
  1240. //
  1241. // Loadws2_32Api()
  1242. //
  1243. // Purpose: Loads ws2_32.dll
  1244. //
  1245. // Parameters: none
  1246. //
  1247. // Return: pointer to WS2_32_API
  1248. //
  1249. //*************************************************************
  1250. WS2_32_API * Loadws2_32Api()
  1251. {
  1252. BOOL bResult = FALSE;
  1253. WS2_32_API *pws2_32Api = &g_ws2_32Api;
  1254. if (InitializeApiDLLsCritSec() != ERROR_SUCCESS)
  1255. return NULL;
  1256. EnterCriticalSection( g_ApiDLLCritSec );
  1257. if ( pws2_32Api->hInstance ) {
  1258. //
  1259. // module already loaded and initialized
  1260. //
  1261. LeaveCriticalSection( g_ApiDLLCritSec );
  1262. return pws2_32Api;
  1263. }
  1264. pws2_32Api->hInstance = LoadLibrary (TEXT("ws2_32.dll"));
  1265. if (!pws2_32Api->hInstance) {
  1266. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to load ws2_32.dll with %d."),
  1267. GetLastError()));
  1268. goto Exit;
  1269. }
  1270. pws2_32Api->pfnWSALookupServiceBegin = (PFNWSALOOKUPSERVICEBEGIN) GetProcAddress (pws2_32Api->hInstance,
  1271. "WSALookupServiceBeginW");
  1272. if (!pws2_32Api->pfnWSALookupServiceBegin) {
  1273. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceBegin with %d."),
  1274. GetLastError()));
  1275. goto Exit;
  1276. }
  1277. pws2_32Api->pfnWSALookupServiceNext = (PFNWSALOOKUPSERVICENEXT) GetProcAddress (pws2_32Api->hInstance,
  1278. "WSALookupServiceNextW");
  1279. if (!pws2_32Api->pfnWSALookupServiceNext) {
  1280. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceNext with %d."),
  1281. GetLastError()));
  1282. goto Exit;
  1283. }
  1284. pws2_32Api->pfnWSALookupServiceEnd = (PFNWSALOOKUPSERVICEEND) GetProcAddress (pws2_32Api->hInstance,
  1285. "WSALookupServiceEnd");
  1286. if (!pws2_32Api->pfnWSALookupServiceEnd) {
  1287. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSALookupServiceEnd with %d."),
  1288. GetLastError()));
  1289. goto Exit;
  1290. }
  1291. pws2_32Api->pfnWSAStartup = (PFNWSASTARTUP) GetProcAddress (pws2_32Api->hInstance,
  1292. "WSAStartup");
  1293. if (!pws2_32Api->pfnWSAStartup) {
  1294. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSAStartup with %d."),
  1295. GetLastError()));
  1296. goto Exit;
  1297. }
  1298. pws2_32Api->pfnWSACleanup = (PFNWSACLEANUP) GetProcAddress (pws2_32Api->hInstance,
  1299. "WSACleanup");
  1300. if (!pws2_32Api->pfnWSACleanup) {
  1301. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSACleanup with %d."),
  1302. GetLastError()));
  1303. goto Exit;
  1304. }
  1305. pws2_32Api->pfnWSAGetLastError = (PFNWSAGETLASTERROR) GetProcAddress (pws2_32Api->hInstance,
  1306. "WSAGetLastError");
  1307. if (!pws2_32Api->pfnWSAGetLastError) {
  1308. DebugMsg((DM_WARNING, TEXT("Loadws2_32Api: Failed to find WSAGetLastError with %d."),
  1309. GetLastError()));
  1310. goto Exit;
  1311. }
  1312. //
  1313. // Success
  1314. //
  1315. bResult = TRUE;
  1316. Exit:
  1317. if (!bResult) {
  1318. CEvents ev(TRUE, EVENT_LOAD_DLL_FAILED);
  1319. ev.AddArg(TEXT("ws2_32.dll")); ev.AddArgWin32Error(GetLastError()); ev.Report();
  1320. if ( pws2_32Api->hInstance ) {
  1321. FreeLibrary( pws2_32Api->hInstance );
  1322. }
  1323. ZeroMemory( pws2_32Api, sizeof( WS2_32_API ) );
  1324. pws2_32Api = 0;
  1325. }
  1326. LeaveCriticalSection( g_ApiDLLCritSec );
  1327. return pws2_32Api;
  1328. }