dllmain.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * Microsoft Windows specifics for syntax-plugin DLL
  43. */
  44. #include "ldap.h"
  45. #include "syntax.h"
  46. #ifdef _WIN32
  47. /* Lifted from Q125688
  48. * How to Port a 16-bit DLL to a Win32 DLL
  49. * on the MSVC 4.0 CD
  50. */
  51. BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
  52. {
  53. WSADATA wsadata;
  54. switch (fdwReason)
  55. {
  56. case DLL_PROCESS_ATTACH:
  57. /* Code from LibMain inserted here. Return TRUE to keep the
  58. DLL loaded or return FALSE to fail loading the DLL.
  59. You may have to modify the code in your original LibMain to
  60. account for the fact that it may be called more than once.
  61. You will get one DLL_PROCESS_ATTACH for each process that
  62. loads the DLL. This is different from LibMain which gets
  63. called only once when the DLL is loaded. The only time this
  64. is critical is when you are using shared data sections.
  65. If you are using shared data sections for statically
  66. allocated data, you will need to be careful to initialize it
  67. only once. Check your code carefully.
  68. Certain one-time initializations may now need to be done for
  69. each process that attaches. You may also not need code from
  70. your original LibMain because the operating system may now
  71. be doing it for you.
  72. */
  73. /*
  74. * 16 bit code calls UnlockData()
  75. * which is mapped to UnlockSegment in windows.h
  76. * in 32 bit world UnlockData is not defined anywhere
  77. * UnlockSegment is mapped to GlobalUnfix in winbase.h
  78. * and the docs for both UnlockSegment and GlobalUnfix say
  79. * ".. function is oboslete. Segments have no meaning
  80. * in the 32-bit environment". So we do nothing here.
  81. */
  82. if( errno = WSAStartup(0x0101, &wsadata ) != 0 )
  83. return FALSE;
  84. break;
  85. case DLL_THREAD_ATTACH:
  86. /* Called each time a thread is created in a process that has
  87. already loaded (attached to) this DLL. Does not get called
  88. for each thread that exists in the process before it loaded
  89. the DLL.
  90. Do thread-specific initialization here.
  91. */
  92. break;
  93. case DLL_THREAD_DETACH:
  94. /* Same as above, but called when a thread in the process
  95. exits.
  96. Do thread-specific cleanup here.
  97. */
  98. break;
  99. case DLL_PROCESS_DETACH:
  100. /* Code from _WEP inserted here. This code may (like the
  101. LibMain) not be necessary. Check to make certain that the
  102. operating system is not doing it for you.
  103. */
  104. WSACleanup();
  105. break;
  106. }
  107. /* The return value is only used for DLL_PROCESS_ATTACH; all other
  108. conditions are ignored. */
  109. return TRUE; // successful DLL_PROCESS_ATTACH
  110. }
  111. #else
  112. int CALLBACK
  113. LibMain( HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
  114. {
  115. /*UnlockData( 0 );*/
  116. return( 1 );
  117. }
  118. #endif
  119. #ifdef LDAP_DEBUG
  120. #ifndef _WIN32
  121. #include <stdarg.h>
  122. #include <stdio.h>
  123. void LDAPDebug( int level, char* fmt, ... )
  124. {
  125. static char debugBuf[1024];
  126. if (module_ldap_debug && (*module_ldap_debug & level))
  127. {
  128. va_list ap;
  129. va_start (ap, fmt);
  130. _snprintf (debugBuf, sizeof(debugBuf), fmt, ap);
  131. debugBuf[sizeof(debugBuf)-1] = 0;
  132. va_end (ap);
  133. OutputDebugString (debugBuf);
  134. }
  135. }
  136. #endif
  137. #endif
  138. #ifndef _WIN32
  139. /* The 16-bit version of the RTL does not implement perror() */
  140. #include <stdio.h>
  141. void perror( const char *msg )
  142. {
  143. char buf[128];
  144. wsprintf( buf, "%s: error %d\n", msg, WSAGetLastError()) ;
  145. OutputDebugString( buf );
  146. }
  147. #endif