Back to index

lightning-sunbird  0.9+nobinonly
dllmain.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  * Microsoft Windows specifics for LIBLDAP DLL
00039  */
00040 #include "ldap.h"
00041 #include "lber.h"
00042 
00043 
00044 #ifdef _WIN32
00045 /* Lifted from Q125688
00046  * How to Port a 16-bit DLL to a Win32 DLL
00047  * on the MSVC 4.0 CD
00048  */
00049 BOOL WINAPI DllMain (HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
00050 {
00051        switch (fdwReason)
00052        {
00053        case DLL_PROCESS_ATTACH:
00054                /* Code from LibMain inserted here.  Return TRUE to keep the
00055                   DLL loaded or return FALSE to fail loading the DLL.
00056 
00057                   You may have to modify the code in your original LibMain to
00058                   account for the fact that it may be called more than once.
00059                   You will get one DLL_PROCESS_ATTACH for each process that
00060                   loads the DLL. This is different from LibMain which gets
00061                   called only once when the DLL is loaded. The only time this
00062                   is critical is when you are using shared data sections.
00063                   If you are using shared data sections for statically
00064                   allocated data, you will need to be careful to initialize it
00065                   only once. Check your code carefully.
00066 
00067                   Certain one-time initializations may now need to be done for
00068                   each process that attaches. You may also not need code from
00069                   your original LibMain because the operating system may now
00070                   be doing it for you.
00071                */
00072               /*
00073                * 16 bit code calls UnlockData()
00074                * which is mapped to UnlockSegment in windows.h
00075                * in 32 bit world UnlockData is not defined anywhere
00076                * UnlockSegment is mapped to GlobalUnfix in winbase.h
00077                * and the docs for both UnlockSegment and GlobalUnfix say 
00078                * ".. function is oboslete.  Segments have no meaning 
00079                *  in the 32-bit environment".  So we do nothing here.
00080                */
00081               /* If we are building a version that includes the security libraries,
00082                * we have to initialize Winsock here. If not, we can defer until the
00083                * first real socket call is made (in mozock.c).
00084                */
00085 #ifdef LINK_SSL
00086        {
00087               WSADATA wsaData;
00088               WSAStartup(0x0101, &wsaData);
00089        }
00090 #endif
00091 
00092               break;
00093 
00094        case DLL_THREAD_ATTACH:
00095               /* Called each time a thread is created in a process that has
00096                  already loaded (attached to) this DLL. Does not get called
00097                  for each thread that exists in the process before it loaded
00098                  the DLL.
00099 
00100                  Do thread-specific initialization here.
00101               */
00102               break;
00103 
00104        case DLL_THREAD_DETACH:
00105               /* Same as above, but called when a thread in the process
00106                  exits.
00107 
00108                  Do thread-specific cleanup here.
00109               */
00110               break;
00111 
00112        case DLL_PROCESS_DETACH:
00113               /* Code from _WEP inserted here.  This code may (like the
00114                  LibMain) not be necessary.  Check to make certain that the
00115                  operating system is not doing it for you.
00116               */
00117 #ifdef LINK_SSL
00118               WSACleanup();
00119 #endif
00120 
00121               break;
00122        }
00123        /* The return value is only used for DLL_PROCESS_ATTACH; all other
00124        conditions are ignored.  */
00125        return TRUE;   // successful DLL_PROCESS_ATTACH
00126 }
00127 #else
00128 int CALLBACK
00129 LibMain( HINSTANCE hinst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine )
00130 {
00131        /*UnlockData( 0 );*/
00132        return( 1 );
00133 }
00134 
00135 BOOL CALLBACK __loadds WEP(BOOL fSystemExit)
00136 {
00137        WSACleanup();
00138     return TRUE;
00139 }
00140 
00141 #endif
00142 
00143 #ifdef LDAP_DEBUG
00144 #ifndef _WIN32
00145 #include <stdarg.h>
00146 #include <stdio.h>
00147 
00148 void LDAP_C LDAPDebug( int level, char* fmt, ... )
00149 {
00150        static char debugBuf[1024];
00151 
00152        if (ldap_debug & level)
00153        {
00154               va_list ap;
00155               va_start (ap, fmt);
00156               _snprintf (debugBuf, sizeof(debugBuf), fmt, ap);
00157               va_end (ap);
00158 
00159               OutputDebugString (debugBuf);
00160        }
00161 }
00162 #endif
00163 #endif
00164 
00165 #ifndef _WIN32
00166 
00167 /* The 16-bit version of the RTL does not implement perror() */
00168 
00169 #include <stdio.h>
00170 
00171 void perror( const char *msg )
00172 {
00173        char buf[128];
00174        wsprintf( buf, "%s: error %d\n", msg, WSAGetLastError()) ;
00175        OutputDebugString( buf );
00176 }
00177 
00178 #endif