1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DllMain.cpp,v $
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
33 #pragma warning(push,1) // disable warnings within system headers
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
, '\\' );
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
;
89 hModuleUnicows
= _LoadUnicowsLibrary();
91 if ( !hModuleUnicows
)
96 FORMAT_MESSAGE_ALLOCATE_BUFFER
|
97 FORMAT_MESSAGE_FROM_SYSTEM
|
98 FORMAT_MESSAGE_IGNORE_INSERTS
,
100 ERROR_DLL_NOT_FOUND
/* GetLastError() */,
101 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), // Default language
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
);
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
;
129 FARPROC _PfnLoadUnicows
= (FARPROC
)LoadUnicowsLibrary
;
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
;
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
162 __do_global_ctors (void)
164 unsigned long nptrs
= (unsigned long) __CTOR_LIST__
[0];
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
--)
187 * Register the destructors for processing on exit.
189 atexit (__do_global_dtors
);
192 static int initialized
= 0;
201 __do_global_ctors ();
205 static void do_startup( void )
207 if (((LONG
)GetVersion()&0x800000ff) == 0x80000004)
209 hModuleUnicowsDLL
= LoadUnicowsLibrary();
210 if (hModuleUnicowsDLL
)
215 void do_cleanup( void )
217 FreeLibrary(hModuleUnicowsDLL
);
223 extern "C" BOOL WINAPI
DllMain( HMODULE hModule
, DWORD dwReason
, LPVOID
)
227 case DLL_PROCESS_ATTACH
:
228 UWINAPI_BaseAddress
= hModule
;
229 return DisableThreadLibraryCalls( hModule
);