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.

163 lines
3.6 KiB

  1. #include "mslocusr.h"
  2. #include <netspi.h>
  3. #define DECL_CRTFREE
  4. #include <crtfree.h>
  5. /* the following defs will make msluglob.h actually define globals */
  6. #define EXTERN
  7. #define ASSIGN(value) = value
  8. #include "msluglob.h"
  9. HANDLE g_hmtxShell = 0; // Note: Handle is per-instance.
  10. #ifdef DEBUG
  11. BOOL g_fCritical=FALSE;
  12. #endif
  13. HINSTANCE hInstance = NULL;
  14. const char szMutexName[] = "MSLocUsrMutex";
  15. UINT g_cRefThisDll = 0; // Reference count of this DLL.
  16. UINT g_cLocks = 0; // Number of locks on this server.
  17. void LockThisDLL(BOOL fLock)
  18. {
  19. ENTERCRITICAL
  20. {
  21. if (fLock)
  22. g_cLocks++;
  23. else
  24. g_cLocks--;
  25. }
  26. LEAVECRITICAL
  27. }
  28. void RefThisDLL(BOOL fRef)
  29. {
  30. ENTERCRITICAL
  31. {
  32. if (fRef)
  33. g_cRefThisDll++;
  34. else
  35. g_cRefThisDll--;
  36. }
  37. LEAVECRITICAL
  38. }
  39. void Netlib_EnterCriticalSection(void)
  40. {
  41. WaitForSingleObject(g_hmtxShell, INFINITE);
  42. #ifdef DEBUG
  43. g_fCritical=TRUE;
  44. #endif
  45. }
  46. void Netlib_LeaveCriticalSection(void)
  47. {
  48. #ifdef DEBUG
  49. g_fCritical=FALSE;
  50. #endif
  51. ReleaseMutex(g_hmtxShell);
  52. }
  53. void _ProcessAttach()
  54. {
  55. //
  56. // All the per-instance initialization code should come here.
  57. //
  58. // We should not pass TRUE as fInitialOwner, read the CreateMutex
  59. // section of Win32 API help file for detail.
  60. //
  61. ::DisableThreadLibraryCalls(::hInstance);
  62. g_hmtxShell = CreateMutex(NULL, FALSE, ::szMutexName); // per-instance
  63. ::InitStringLibrary();
  64. }
  65. void _ProcessDetach()
  66. {
  67. UnloadShellEntrypoint();
  68. CloseHandle(g_hmtxShell);
  69. }
  70. STDAPI_(BOOL) DllEntryPoint(HINSTANCE hInstDll, DWORD fdwReason, LPVOID reserved)
  71. {
  72. if (fdwReason == DLL_PROCESS_ATTACH)
  73. {
  74. hInstance = hInstDll;
  75. _ProcessAttach();
  76. }
  77. else if (fdwReason == DLL_PROCESS_DETACH)
  78. {
  79. _ProcessDetach();
  80. }
  81. return TRUE;
  82. }
  83. UINT
  84. NPSCopyNLS (
  85. NLS_STR FAR * pnlsSourceString,
  86. LPVOID lpDestBuffer,
  87. LPDWORD lpBufferSize )
  88. {
  89. if ((!lpBufferSize) || (!lpDestBuffer && (*lpBufferSize != 0))) {
  90. return ERROR_INVALID_PARAMETER;
  91. }
  92. if (pnlsSourceString != NULL) {
  93. DWORD dwDestLen = 0; // bytes copied to dest buffer, including NULL
  94. DWORD dwSourceLen = pnlsSourceString->strlen() + 1; // bytes in source buffer, including NULL
  95. if ((lpDestBuffer) && (*lpBufferSize != 0)) {
  96. NLS_STR nlsDestination( STR_OWNERALLOC_CLEAR, (LPSTR)lpDestBuffer, (UINT) *lpBufferSize );
  97. nlsDestination = *pnlsSourceString; /* copy source string to caller's buffer */
  98. dwDestLen = nlsDestination.strlen() + 1;
  99. }
  100. if (dwSourceLen != dwDestLen) {
  101. // Only update buffersize parameter if there is more data,
  102. // and store source string size, counting NULL.
  103. *lpBufferSize = dwSourceLen;
  104. return ERROR_MORE_DATA;
  105. }
  106. else {
  107. return NOERROR;
  108. }
  109. }
  110. else {
  111. if (*lpBufferSize == 0) {
  112. *lpBufferSize = 1;
  113. return ERROR_MORE_DATA;
  114. }
  115. else {
  116. *(LPSTR)lpDestBuffer = NULL; // validated to not be NULL above
  117. return NOERROR;
  118. }
  119. }
  120. }
  121. DWORD
  122. NPSCopyString (
  123. LPCTSTR lpSourceString,
  124. LPVOID lpDestBuffer,
  125. LPDWORD lpBufferSize )
  126. {
  127. if (lpSourceString != NULL) {
  128. NLS_STR nlsSource( STR_OWNERALLOC, (LPTSTR)lpSourceString );
  129. return NPSCopyNLS ( &nlsSource,
  130. lpDestBuffer,
  131. lpBufferSize );
  132. }
  133. else {
  134. return NPSCopyNLS ( NULL,
  135. lpDestBuffer,
  136. lpBufferSize );
  137. }
  138. }