merge the formfield patch from ooo-build
[ooovba.git] / sal / osl / w32 / dllentry.c
bloba81dfc32abd950d6ff2adbbe4676b5f2c38b1347
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: dllentry.c,v $
10 * $Revision: 1.34 $
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 #ifdef _MSC_VER
32 #pragma warning(push,1) /* disable warnings within system headers */
33 #endif
34 #include <windows.h>
35 #ifdef _MSC_VER
36 #pragma warning(pop)
37 #endif
38 #include <tlhelp32.h>
39 #include <systools/win32/uwinapi.h>
40 #include <winsock.h>
41 #include <osl/diagnose.h>
42 #include <sal/types.h>
43 #include <float.h>
45 #include <osl/diagnose.h>
46 #include <osl/mutex.h>
47 #include <sal/types.h>
49 //------------------------------------------------------------------------------
50 // externals
51 //------------------------------------------------------------------------------
53 extern DWORD g_dwTLSTextEncodingIndex;
54 extern void SAL_CALL _osl_callThreadKeyCallbackOnThreadDetach(void);
55 extern CRITICAL_SECTION g_ThreadKeyListCS;
56 extern oslMutex g_Mutex;
57 extern oslMutex g_CurrentDirectoryMutex;
59 extern void rtl_locale_fini (void);
60 extern void rtl_memory_fini (void);
61 extern void rtl_cache_fini (void);
62 extern void rtl_arena_fini (void);
64 #ifdef __MINGW32__
66 typedef void (*func_ptr) (void);
67 extern func_ptr __CTOR_LIST__[];
68 extern func_ptr __DTOR_LIST__[];
70 static void do_startup(void);
71 static void do_cleanup(void);
73 #else
75 /*
76 This is needed because DllMain is called after static constructors. A DLL's
77 startup and shutdown sequence looks like this:
79 _pRawDllMain()
80 _CRT_INIT()
81 DllMain()
82 ....
83 DllMain()
84 _CRT_INIT()
85 _pRawDllMain()
89 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved );
90 extern BOOL (WINAPI *_pRawDllMain)(HANDLE, DWORD, LPVOID) = _RawDllMain;
92 #endif
94 //------------------------------------------------------------------------------
95 // globales
96 //------------------------------------------------------------------------------
98 DWORD g_dwPlatformId = VER_PLATFORM_WIN32_WINDOWS; // remember plattform
100 //------------------------------------------------------------------------------
101 // DllMain
102 //------------------------------------------------------------------------------
103 #ifdef _M_IX86
104 int osl_isSingleCPU = 0;
105 #endif
107 #ifdef __MINGW32__
109 void
110 __do_global_dtors (void)
112 static func_ptr *p = __DTOR_LIST__ + 1;
115 * Call each destructor in the destructor list until a null pointer
116 * is encountered.
118 while (*p)
120 (*(p)) ();
121 p++;
125 void
126 __do_global_ctors (void)
128 unsigned long nptrs = (unsigned long) __CTOR_LIST__[0];
129 unsigned i;
132 * If the first entry in the constructor list is -1 then the list
133 * is terminated with a null entry. Otherwise the first entry was
134 * the number of pointers in the list.
136 if (nptrs == -1)
138 for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
143 * Go through the list backwards calling constructors.
145 for (i = nptrs; i >= 1; i--)
147 __CTOR_LIST__[i] ();
151 * Register the destructors for processing on exit.
153 atexit (__do_global_dtors);
156 static int initialized = 0;
158 void
159 __main (void)
161 if (!initialized)
163 initialized = 1;
164 do_startup();
165 __do_global_ctors ();
169 static void do_startup( void )
171 #else
172 static BOOL WINAPI _RawDllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
174 (void)hinstDLL; /* avoid warnings */
175 (void)lpvReserved; /* avoid warnings */
177 switch (fdwReason)
179 case DLL_PROCESS_ATTACH:
181 #endif
182 OSVERSIONINFO aInfo;
184 #ifdef _M_IX86
185 SYSTEM_INFO SystemInfo;
187 GetSystemInfo(&SystemInfo);
189 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system
191 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount()
192 * comes with a cost and is especially expensive on pre HT x86 single processor
193 * systems, where it isn't needed at all.
195 if ( SystemInfo.dwNumberOfProcessors == 1 ) {
196 osl_isSingleCPU = 1;
198 #endif
199 /* Suppress file error messages from system like "Floppy A: not inserted" */
200 SetErrorMode( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS );
202 /* initialize global mutex */
203 g_Mutex = osl_createMutex();
205 /* initialize "current directory" mutex */
206 g_CurrentDirectoryMutex = osl_createMutex();
209 /* initialize Win9x unicode functions */
210 aInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
212 if ( GetVersionEx(&aInfo) )
213 g_dwPlatformId = aInfo.dwPlatformId;
215 g_dwTLSTextEncodingIndex = TlsAlloc();
216 InitializeCriticalSection( &g_ThreadKeyListCS );
218 //We disable floating point exceptions. This is the usual state at program startup
219 //but on Windows 98 and ME this is not always the case.
220 _control87(_MCW_EM, _MCW_EM);
221 #ifdef __MINGW32__
222 atexit(do_cleanup);
225 void do_cleanup( void )
227 #else
228 break;
231 case DLL_PROCESS_DETACH:
232 #endif
234 WSACleanup( );
236 TlsFree( g_dwTLSTextEncodingIndex );
237 DeleteCriticalSection( &g_ThreadKeyListCS );
239 osl_destroyMutex( g_Mutex );
241 osl_destroyMutex( g_CurrentDirectoryMutex );
243 #ifndef __MINGW32__
247 On a product build memory management finalization might
248 cause a crash without assertion (assertions off) if heap is
249 corrupted. But a crash report won't help here because at
250 this point all other threads have been terminated and only
251 ntdll is on the stack. No chance to find the reason for the
252 corrupted heap if so.
254 So annoying the user with a crash report is completly useless.
258 #ifdef PRODUCT
259 __try
260 #endif
262 /* cleanup locale hashtable */
263 rtl_locale_fini();
265 /* finalize memory management */
266 rtl_memory_fini();
267 rtl_cache_fini();
268 rtl_arena_fini();
270 #ifdef PRODUCT
271 __except( EXCEPTION_EXECUTE_HANDLER )
274 #endif
275 break;
278 return TRUE;
279 #endif
282 static DWORD GetParentProcessId()
284 DWORD dwParentProcessId = 0;
285 HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
287 if ( IsValidHandle( hSnapshot ) )
289 PROCESSENTRY32 pe;
290 BOOL fSuccess;
292 ZeroMemory( &pe, sizeof(pe) );
293 pe.dwSize = sizeof(pe);
294 fSuccess = Process32First( hSnapshot, &pe );
296 while( fSuccess )
298 if ( GetCurrentProcessId() == pe.th32ProcessID )
300 dwParentProcessId = pe.th32ParentProcessID;
301 break;
304 fSuccess = Process32Next( hSnapshot, &pe );
307 CloseHandle( hSnapshot );
310 return dwParentProcessId;
313 static DWORD WINAPI ParentMonitorThreadProc( LPVOID lpParam )
315 DWORD dwParentProcessId = (DWORD)lpParam;
317 HANDLE hParentProcess = OpenProcess( SYNCHRONIZE, FALSE, dwParentProcessId );
318 if ( IsValidHandle( hParentProcess ) )
320 if ( WAIT_OBJECT_0 == WaitForSingleObject( hParentProcess, INFINITE ) )
322 TerminateProcess( GetCurrentProcess(), 0 );
324 CloseHandle( hParentProcess );
326 return 0;
329 BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )
331 (void)hinstDLL; /* avoid warning */
332 (void)lpvReserved; /* avoid warning */
333 switch (fdwReason)
335 case DLL_PROCESS_ATTACH:
337 TCHAR szBuffer[64];
339 // This code will attach the process to it's parent process
340 // if the parent process had set the environment variable.
341 // The corresponding code (setting the environment variable)
342 // is is desktop/win32/source/officeloader.cxx
345 DWORD dwResult = GetEnvironmentVariable( "ATTACHED_PARENT_PROCESSID", szBuffer, sizeof(szBuffer) );
347 if ( dwResult && dwResult < sizeof(szBuffer) )
349 DWORD dwThreadId = 0;
351 DWORD dwParentProcessId = (DWORD)atol( szBuffer );
353 if ( dwParentProcessId && GetParentProcessId() == dwParentProcessId )
355 // No error check, it works or it does not
356 // Thread should only be started for headless mode, see desktop/win32/source/officeloader.cxx
357 CreateThread( NULL, 0, ParentMonitorThreadProc, (LPVOID)dwParentProcessId, 0, &dwThreadId ); //
361 return TRUE;
364 case DLL_THREAD_ATTACH:
365 break;
367 case DLL_THREAD_DETACH:
368 _osl_callThreadKeyCallbackOnThreadDetach( );
369 break;
372 return TRUE;