dllmain.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * Microsoft Windows specifics for collation DLL
  8. */
  9. #include "ldap.h"
  10. #ifdef _WIN32
  11. /* Lifted from Q125688
  12. * How to Port a 16-bit DLL to a Win32 DLL
  13. * on the MSVC 4.0 CD
  14. */
  15. BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
  16. {
  17. WSADATA wsadata;
  18. switch (fdwReason)
  19. {
  20. case DLL_PROCESS_ATTACH:
  21. /* Code from LibMain inserted here. Return TRUE to keep the
  22. DLL loaded or return FALSE to fail loading the DLL.
  23. You may have to modify the code in your original LibMain to
  24. account for the fact that it may be called more than once.
  25. You will get one DLL_PROCESS_ATTACH for each process that
  26. loads the DLL. This is different from LibMain which gets
  27. called only once when the DLL is loaded. The only time this
  28. is critical is when you are using shared data sections.
  29. If you are using shared data sections for statically
  30. allocated data, you will need to be careful to initialize it
  31. only once. Check your code carefully.
  32. Certain one-time initializations may now need to be done for
  33. each process that attaches. You may also not need code from
  34. your original LibMain because the operating system may now
  35. be doing it for you.
  36. */
  37. /*
  38. * 16 bit code calls UnlockData()
  39. * which is mapped to UnlockSegment in windows.h
  40. * in 32 bit world UnlockData is not defined anywhere
  41. * UnlockSegment is mapped to GlobalUnfix in winbase.h
  42. * and the docs for both UnlockSegment and GlobalUnfix say
  43. * ".. function is oboslete. Segments have no meaning
  44. * in the 32-bit environment". So we do nothing here.
  45. */
  46. if( errno = WSAStartup(0x0101, &wsadata ) != 0 )
  47. return FALSE;
  48. break;
  49. case DLL_THREAD_ATTACH:
  50. /* Called each time a thread is created in a process that has
  51. already loaded (attached to) this DLL. Does not get called
  52. for each thread that exists in the process before it loaded
  53. the DLL.
  54. Do thread-specific initialization here.
  55. */
  56. break;
  57. case DLL_THREAD_DETACH:
  58. /* Same as above, but called when a thread in the process
  59. exits.
  60. Do thread-specific cleanup here.
  61. */
  62. break;
  63. case DLL_PROCESS_DETACH:
  64. /* Code from _WEP inserted here. This code may (like the
  65. LibMain) not be necessary. Check to make certain that the
  66. operating system is not doing it for you.
  67. */
  68. WSACleanup();
  69. break;
  70. }
  71. /* The return value is only used for DLL_PROCESS_ATTACH; all other
  72. conditions are ignored. */
  73. return TRUE; // successful DLL_PROCESS_ATTACH
  74. }
  75. #else
  76. int CALLBACK
  77. LibMain( HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
  78. {
  79. /*UnlockData( 0 );*/
  80. return( 1 );
  81. }
  82. #endif
  83. #ifdef LDAP_DEBUG
  84. #ifndef _WIN32
  85. #include <stdarg.h>
  86. #include <stdio.h>
  87. void LDAPDebug( int level, char* fmt, ... )
  88. {
  89. static char debugBuf[1024];
  90. if (module_ldap_debug && (*module_ldap_debug & level))
  91. {
  92. va_list ap;
  93. va_start (ap, fmt);
  94. _snprintf (debugBuf, sizeof(debugBuf), fmt, ap);
  95. va_end (ap);
  96. OutputDebugString (debugBuf);
  97. }
  98. }
  99. #endif
  100. #endif
  101. #ifndef _WIN32
  102. /* The 16-bit version of the RTL does not implement perror() */
  103. #include <stdio.h>
  104. void perror( const char *msg )
  105. {
  106. char buf[128];
  107. wsprintf( buf, "%s: error %d\n", msg, WSAGetLastError()) ;
  108. OutputDebugString( buf );
  109. }
  110. #endif