merge the formfield patch from ooo-build
[ooovba.git] / sal / systools / win32 / uwinapi / DllMain.cpp
blobd64a8bb6a3ba9dbbefdcc0e8b60197e94a540a2e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DllMain.cpp,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #define WIN32_LEAN_AND_MEAN
32 #ifdef _MSC_VER
33 #pragma warning(push,1) // disable warnings within system headers
34 #endif
35 #include <windows.h>
36 #ifdef _MSC_VER
37 #pragma warning(pop)
38 #endif
39 #include <malloc.h>
40 #define _MBCS
41 #include <tchar.h>
44 HMODULE UWINAPI_BaseAddress = NULL;
45 const CHAR szUnicowsModuleName[] = "UNICOWS.DLL";
47 static HMODULE WINAPI _LoadUnicowsLibrary(VOID)
49 CHAR szModulePath[MAX_PATH];
50 HMODULE hModuleUnicows = NULL;
52 // First search in the same directory as UWINAPI.DLL was loaded from. This is because
53 // UWINAPI.DLL not always resides in the same directory as the actual application.
55 if ( UWINAPI_BaseAddress && GetModuleFileNameA( UWINAPI_BaseAddress, szModulePath, MAX_PATH ) )
57 char *lpLastBkSlash = _tcsrchr( szModulePath, '\\' );
59 if ( lpLastBkSlash )
61 size_t nParentDirSize = (size_t) (_tcsinc( lpLastBkSlash ) - szModulePath);
62 LPSTR lpUnicowsModulePath = (LPTSTR)_alloca( nParentDirSize + sizeof(szUnicowsModuleName) );
64 if ( lpUnicowsModulePath )
66 _tcsncpy( lpUnicowsModulePath, szModulePath, nParentDirSize );
67 _tcscpy( lpUnicowsModulePath + nParentDirSize, szUnicowsModuleName );
69 hModuleUnicows = LoadLibraryA( lpUnicowsModulePath );
74 // Search at the common places
76 if ( !hModuleUnicows )
77 hModuleUnicows = LoadLibraryA(szUnicowsModuleName);
79 return hModuleUnicows;
82 static HMODULE WINAPI LoadUnicowsLibrary(VOID)
84 HMODULE hModuleUnicows;
85 int idMsg = IDOK;
89 hModuleUnicows = _LoadUnicowsLibrary();
91 if ( !hModuleUnicows )
93 LPVOID lpMsgBuf;
95 FormatMessageA(
96 FORMAT_MESSAGE_ALLOCATE_BUFFER |
97 FORMAT_MESSAGE_FROM_SYSTEM |
98 FORMAT_MESSAGE_IGNORE_INSERTS,
99 NULL,
100 ERROR_DLL_NOT_FOUND /* GetLastError() */,
101 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
102 (LPSTR)&lpMsgBuf,
104 NULL
106 // Process any inserts in lpMsgBuf.
107 CHAR szModuleFileName[MAX_PATH];
109 GetModuleFileNameA( NULL, szModuleFileName, sizeof(szModuleFileName) );
110 LPSTR lpMessage = (LPSTR)_alloca( strlen( (LPCSTR)lpMsgBuf ) + sizeof(szUnicowsModuleName) + 1 );
111 strcpy( lpMessage, (LPCSTR)lpMsgBuf );
112 strcat( lpMessage, "\n" );
113 strcat( lpMessage, szUnicowsModuleName );
114 // Free the buffer.
115 LocalFree( lpMsgBuf );
116 // Display the string.
117 idMsg = MessageBoxA( NULL, lpMessage,
118 szModuleFileName, MB_ABORTRETRYIGNORE | MB_ICONERROR | MB_TASKMODAL );
120 if ( IDABORT == idMsg )
121 TerminateProcess( GetCurrentProcess(), 255 );
123 } while ( !hModuleUnicows && IDRETRY == idMsg );
125 return hModuleUnicows;
128 extern "C" {
129 FARPROC _PfnLoadUnicows = (FARPROC)LoadUnicowsLibrary;
132 #ifdef __MINGW32__
134 extern "C" {
136 typedef void (*func_ptr) (void);
137 extern func_ptr __CTOR_LIST__[];
138 extern func_ptr __DTOR_LIST__[];
140 static void do_startup(void);
141 static void do_cleanup(void);
143 HMODULE hModuleUnicowsDLL;
145 void
146 __do_global_dtors (void)
148 static func_ptr *p = __DTOR_LIST__ + 1;
151 * Call each destructor in the destructor list until a null pointer
152 * is encountered.
154 while (*p)
156 (*(p)) ();
157 p++;
161 void
162 __do_global_ctors (void)
164 unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
165 unsigned i;
168 * If the first entry in the constructor list is -1 then the list
169 * is terminated with a null entry. Otherwise the first entry was
170 * the number of pointers in the list.
172 if (nptrs == static_cast<unsigned long>(-1))
174 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
179 * Go through the list backwards calling constructors.
181 for (i = nptrs; i >= 1; i--)
183 __CTOR_LIST__[i] ();
187 * Register the destructors for processing on exit.
189 atexit (__do_global_dtors);
192 static int initialized = 0;
194 void
195 __main (void)
197 if (!initialized)
199 initialized = 1;
200 do_startup();
201 __do_global_ctors ();
205 static void do_startup( void )
207 if (((LONG)GetVersion()&0x800000ff) == 0x80000004)
209 hModuleUnicowsDLL = LoadUnicowsLibrary();
210 if (hModuleUnicowsDLL)
211 atexit(do_cleanup);
215 void do_cleanup( void )
217 FreeLibrary(hModuleUnicowsDLL);
221 #endif
223 extern "C" BOOL WINAPI DllMain( HMODULE hModule, DWORD dwReason, LPVOID )
225 switch ( dwReason )
227 case DLL_PROCESS_ATTACH:
228 UWINAPI_BaseAddress = hModule;
229 return DisableThreadLibraryCalls( hModule );
230 default:
231 return TRUE;