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.

124 lines
3.8 KiB

  1. /*****************************************************************************
  2. *
  3. * ftpinet.cpp - Interfacing to WinINet
  4. *
  5. *****************************************************************************/
  6. #include "priv.h"
  7. #include "ftpinet.h"
  8. #define SHInterlockedCompareExchangePointer SHInterlockedCompareExchange
  9. /*****************************************************************************
  10. *
  11. * Const strings for our Wininet stuff.
  12. *
  13. *****************************************************************************/
  14. HINSTANCE g_hinstWininet = NULL; /* The DLL handle */
  15. HINTERNET g_hint = NULL; /* Shared internet anchor handle */
  16. #define SZ_WININET_AGENT TEXT("Microsoft(r) Windows(tm) FTP Folder")
  17. /*****************************************************************************\
  18. FUNCTION: InitWininet
  19. \*****************************************************************************/
  20. void InitWininet(void)
  21. {
  22. // You can't use a critical section around LoadLibrary().
  23. ASSERTNONCRITICAL;
  24. if (!g_hinstWininet)
  25. {
  26. HINSTANCE hinstTemp = LoadLibrary(TEXT("WININET.DLL"));
  27. if (EVAL(hinstTemp))
  28. {
  29. // Can we successfully put it here?
  30. if (SHInterlockedCompareExchangePointer((void **)&g_hinstWininet, hinstTemp, NULL))
  31. {
  32. // No, someone else beat us there.
  33. ASSERT(g_hinstWininet);
  34. FreeLibrary(hinstTemp);
  35. }
  36. }
  37. }
  38. if (EVAL(g_hinstWininet))
  39. {
  40. if (!g_hint)
  41. {
  42. HINTERNET hinternetTemp;
  43. EVAL(SUCCEEDED(InternetOpenWrap(TRUE, SZ_WININET_AGENT, PRE_CONFIG_INTERNET_ACCESS, 0, 0, 0, &hinternetTemp)));
  44. if (EVAL(hinternetTemp))
  45. {
  46. // Can we successfully put it here?
  47. if (SHInterlockedCompareExchangePointer((void **)&g_hint, hinternetTemp, NULL))
  48. {
  49. // No, someone else beat us there.
  50. ASSERT(g_hint);
  51. InternetCloseHandle(hinternetTemp);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. /*****************************************************************************\
  58. FUNCTION: UnloadWininet
  59. \*****************************************************************************/
  60. void UnloadWininet(void)
  61. {
  62. // You can't use a critical section around FreeLibrary() (I think).
  63. ASSERTNONCRITICAL;
  64. if (g_hint)
  65. {
  66. HINTERNET hinternetTemp = InterlockedExchangePointer(&g_hint, NULL);
  67. if (hinternetTemp)
  68. {
  69. InternetCloseHandle(hinternetTemp);
  70. }
  71. }
  72. /************************
  73. // I want to unload wininet, I really do. But this function is called
  74. // during process un-attach and it's better to leak wininet than to
  75. // call FreeLibrary() during process unattach.
  76. if (g_hinstWininet)
  77. {
  78. HINSTANCE hinstTemp = (HINSTANCE)InterlockedExchangePointer((void **) &g_hinstWininet, NULL);
  79. if (hinstTemp)
  80. {
  81. FreeLibrary(hinstTemp);
  82. }
  83. }
  84. *********************/
  85. }
  86. /*****************************************************************************\
  87. * hintShared
  88. *
  89. * Obtain the shared internet handle that we use for all our stuff.
  90. * We load WinINet only on demand, so that quick things will be quick.
  91. * If this procedure fails, the reason can be obtained via GetLastError().
  92. * (Note that this assumes that we always try to InitWininet().)
  93. \*****************************************************************************/
  94. HINTERNET GetWininetSessionHandle(void)
  95. {
  96. // Avoid taking the critical section unless you really need to.
  97. if (!g_hint)
  98. {
  99. InitWininet();
  100. ASSERT(g_hint);
  101. }
  102. return g_hint;
  103. }