xref: /aoo41x/main/sal/osl/w32/module.cxx (revision 79e556ee)
187d2adbcSAndrew Rist /**************************************************************
287d2adbcSAndrew Rist  *
387d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
487d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
587d2adbcSAndrew Rist  * distributed with this work for additional information
687d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
787d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
887d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
987d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
1087d2adbcSAndrew Rist  *
1187d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1287d2adbcSAndrew Rist  *
1387d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
1487d2adbcSAndrew Rist  * software distributed under the License is distributed on an
1587d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1687d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
1787d2adbcSAndrew Rist  * specific language governing permissions and limitations
1887d2adbcSAndrew Rist  * under the License.
1987d2adbcSAndrew Rist  *
2087d2adbcSAndrew Rist  *************************************************************/
2187d2adbcSAndrew Rist 
2287d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "system.h"
25cdf0e10cSrcweir #include <tlhelp32.h>
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "file_url.h"
28cdf0e10cSrcweir #include "path_helper.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <osl/module.h>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir #include <osl/thread.h>
33cdf0e10cSrcweir #include <osl/file.h>
34cdf0e10cSrcweir #include <rtl/logfile.h>
35cdf0e10cSrcweir #include <vector>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /*
38cdf0e10cSrcweir 	under WIN32, we use the void* oslModule
39cdf0e10cSrcweir 	as a WIN32 HANDLE (which is also a 32-bit value)
40cdf0e10cSrcweir */
41cdf0e10cSrcweir 
42cdf0e10cSrcweir /*****************************************************************************/
43cdf0e10cSrcweir /* osl_loadModule */
44cdf0e10cSrcweir /*****************************************************************************/
osl_loadModule(rtl_uString * strModuleName,sal_Int32 nRtldMode)45cdf0e10cSrcweir oslModule SAL_CALL osl_loadModule(rtl_uString *strModuleName, sal_Int32 nRtldMode )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir     HINSTANCE hInstance;
48cdf0e10cSrcweir 	UINT errorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
49cdf0e10cSrcweir 	rtl_uString* Module = NULL;
50cdf0e10cSrcweir 	oslModule ret = 0;
51cdf0e10cSrcweir 	oslFileError	nError;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     RTL_LOGFILE_TRACE1( "{ osl_loadModule start: %S", (LPTSTR)&strModuleName->buffer );
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 	OSL_ASSERT(strModuleName);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     nRtldMode = nRtldMode; /* avoid warnings */
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	nError = osl_getSystemPathFromFileURL(strModuleName, &Module);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	if ( osl_File_E_None != nError )
62cdf0e10cSrcweir 		rtl_uString_assign(&Module, strModuleName);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	hInstance = LoadLibraryW(reinterpret_cast<LPCWSTR>(Module->buffer));
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     if (hInstance == NULL)
67cdf0e10cSrcweir         hInstance = LoadLibraryExW(reinterpret_cast<LPCWSTR>(Module->buffer), NULL,
68cdf0e10cSrcweir                                   LOAD_WITH_ALTERED_SEARCH_PATH);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	//In case of long path names (\\?\c:\...) try to shorten the filename.
71cdf0e10cSrcweir 	//LoadLibrary cannot handle file names which exceed 260 letters.
72cdf0e10cSrcweir     //In case the path is to long, the function will fail. However, the error
73cdf0e10cSrcweir     //code can be different. For example, it returned  ERROR_FILENAME_EXCED_RANGE
74cdf0e10cSrcweir     //on Windows XP and ERROR_INSUFFICIENT_BUFFER on Windows 7 (64bit)
75cdf0e10cSrcweir 	if (hInstance == NULL && Module->length > 260)
76cdf0e10cSrcweir 	{
77*79e556eeSJuergen Schmidt 		std::vector<WCHAR> vec(Module->length + 1);
78cdf0e10cSrcweir 		DWORD len = GetShortPathNameW(reinterpret_cast<LPCWSTR>(Module->buffer),
79cdf0e10cSrcweir                                       &vec[0], Module->length + 1);
80cdf0e10cSrcweir 		if (len )
81cdf0e10cSrcweir 		{
82cdf0e10cSrcweir 			hInstance = LoadLibraryW(&vec[0]);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 			if (hInstance == NULL)
85cdf0e10cSrcweir 				hInstance = LoadLibraryExW(&vec[0], NULL,
86cdf0e10cSrcweir                                   LOAD_WITH_ALTERED_SEARCH_PATH);
87cdf0e10cSrcweir 		}
88cdf0e10cSrcweir 	}
89cdf0e10cSrcweir 
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	if (hInstance <= (HINSTANCE)HINSTANCE_ERROR)
92cdf0e10cSrcweir 		hInstance = 0;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	ret = (oslModule) hInstance;
95cdf0e10cSrcweir 	rtl_uString_release(Module);
96cdf0e10cSrcweir 	SetErrorMode(errorMode);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     RTL_LOGFILE_TRACE1( "} osl_loadModule end: %S", (LPTSTR)&strModuleName->buffer );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	return ret;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103ca2659a9SHerbert Dürr /*****************************************************************************/
104ca2659a9SHerbert Dürr /* osl_loadAsciiModule */
105ca2659a9SHerbert Dürr /*****************************************************************************/
osl_loadAsciiModule(const sal_Char * pModuleName,sal_Int32 nRtldMode)106ca2659a9SHerbert Dürr oslModule SAL_CALL osl_loadAsciiModule( const sal_Char* pModuleName, sal_Int32 nRtldMode )
107ca2659a9SHerbert Dürr {
108ca2659a9SHerbert Dürr 	rtl_uString* pUniName = NULL;
109ca2659a9SHerbert Dürr 	rtl_uString_newFromAscii( &pUniName, pModuleName );
110ca2659a9SHerbert Dürr 	oslModule aModule = osl_loadModule( pUniName, nRtldMode );
111ca2659a9SHerbert Dürr 	rtl_uString_release( pUniName );
112ca2659a9SHerbert Dürr 	return aModule;
113ca2659a9SHerbert Dürr }
114ca2659a9SHerbert Dürr 
115cdf0e10cSrcweir /*****************************************************************************/
116cdf0e10cSrcweir /* osl_getModuleHandle */
117cdf0e10cSrcweir /*****************************************************************************/
118cdf0e10cSrcweir 
119cdf0e10cSrcweir sal_Bool SAL_CALL
osl_getModuleHandle(rtl_uString * pModuleName,oslModule * pResult)120cdf0e10cSrcweir osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     HINSTANCE hInstance = GetModuleHandleW(reinterpret_cast<LPCWSTR>(pModuleName->buffer));
123cdf0e10cSrcweir     if( hInstance )
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         *pResult = (oslModule) hInstance;
126cdf0e10cSrcweir         return sal_True;
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir     return sal_False;
130cdf0e10cSrcweir }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir /*****************************************************************************/
133cdf0e10cSrcweir /* osl_unloadModule */
134cdf0e10cSrcweir /*****************************************************************************/
osl_unloadModule(oslModule Module)135cdf0e10cSrcweir void SAL_CALL osl_unloadModule(oslModule Module)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir 	FreeLibrary((HINSTANCE)Module);
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir /*****************************************************************************/
141cdf0e10cSrcweir /* osl_getSymbol */
142cdf0e10cSrcweir /*****************************************************************************/
osl_getSymbol(oslModule Module,rtl_uString * strSymbolName)143cdf0e10cSrcweir void* SAL_CALL osl_getSymbol(oslModule Module, rtl_uString *strSymbolName)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir     /* casting from a function pointer to a data pointer is invalid
146cdf0e10cSrcweir        be in this case unavoidable because the API has to stay
147cdf0e10cSrcweir        compitable we need to keep this function which returns a
148cdf0e10cSrcweir        void* by definition */
149cdf0e10cSrcweir #ifdef _MSC_VER
150cdf0e10cSrcweir #pragma warning(push)
151cdf0e10cSrcweir #pragma warning(disable:4054)
152cdf0e10cSrcweir #endif
153cdf0e10cSrcweir     return (void*)(osl_getFunctionSymbol(Module, strSymbolName));
154cdf0e10cSrcweir #ifdef _MSC_VER
155cdf0e10cSrcweir #pragma warning(pop)
156cdf0e10cSrcweir #endif
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir /*****************************************************************************/
160cdf0e10cSrcweir /* osl_getFunctionSymbol */
161cdf0e10cSrcweir /*****************************************************************************/
osl_getFunctionSymbol(oslModule Module,rtl_uString * strSymbolName)162cdf0e10cSrcweir oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     rtl_String *symbolName = NULL;
165cdf0e10cSrcweir 	oslGenericFunction address;
166cdf0e10cSrcweir 
167cdf0e10cSrcweir 	OSL_ASSERT(Module);
168cdf0e10cSrcweir 	OSL_ASSERT(strSymbolName);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	rtl_uString2String(
171cdf0e10cSrcweir 		&symbolName,
172cdf0e10cSrcweir 		strSymbolName->buffer,
173cdf0e10cSrcweir 		strSymbolName->length,
174cdf0e10cSrcweir 		RTL_TEXTENCODING_UTF8,
175cdf0e10cSrcweir 		OUSTRING_TO_OSTRING_CVTFLAGS
176cdf0e10cSrcweir 	);
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
179cdf0e10cSrcweir 	rtl_string_release(symbolName);
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     return address;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir /*****************************************************************************/
185cdf0e10cSrcweir /* osl_getAsciiFunctionSymbol */
186cdf0e10cSrcweir /*****************************************************************************/
187cdf0e10cSrcweir oslGenericFunction SAL_CALL
osl_getAsciiFunctionSymbol(oslModule Module,const sal_Char * pSymbol)188cdf0e10cSrcweir osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
189cdf0e10cSrcweir {
190cdf0e10cSrcweir 	oslGenericFunction fncAddr = NULL;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     if( pSymbol )
193cdf0e10cSrcweir         fncAddr=(oslGenericFunction)GetProcAddress((HINSTANCE) Module, pSymbol);
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     return fncAddr;
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 
200cdf0e10cSrcweir /*****************************************************************************/
201cdf0e10cSrcweir /* osl_addressGetModuleURL */
202cdf0e10cSrcweir /*****************************************************************************/
203cdf0e10cSrcweir 
204cdf0e10cSrcweir /*****************************************************************************/
205cdf0e10cSrcweir /* Implementation for Windows 95, 98 and Me */
206cdf0e10cSrcweir /*****************************************************************************/
207cdf0e10cSrcweir 
208cdf0e10cSrcweir /* Undefine because there is no explicit "A" definition */
209cdf0e10cSrcweir 
210cdf0e10cSrcweir #ifdef MODULEENTRY32
211cdf0e10cSrcweir #undef MODULEENTRY32
212cdf0e10cSrcweir #endif
213cdf0e10cSrcweir 
214cdf0e10cSrcweir #ifdef LPMODULEENTRY32
215cdf0e10cSrcweir #undef LPMODULEENTRY32
216cdf0e10cSrcweir #endif
217cdf0e10cSrcweir 
218cdf0e10cSrcweir typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_PROC)( DWORD dwFlags, DWORD th32ProcessID );
219cdf0e10cSrcweir typedef BOOL (WINAPI *Module32First_PROC)( HANDLE	hSnapshot, LPMODULEENTRY32 lpme32 );
220cdf0e10cSrcweir typedef BOOL (WINAPI *Module32Next_PROC)( HANDLE	hSnapshot, LPMODULEENTRY32 lpme32 );
221cdf0e10cSrcweir 
_osl_addressGetModuleURL_Windows(void * pv,rtl_uString ** pustrURL)222cdf0e10cSrcweir static sal_Bool SAL_CALL _osl_addressGetModuleURL_Windows( void *pv, rtl_uString **pustrURL )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir 	sal_Bool	bSuccess		= sal_False;	/* Assume failure */
225cdf0e10cSrcweir 	HMODULE		hModKernel32	= GetModuleHandleA( "KERNEL32.DLL" );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	if ( hModKernel32 )
228cdf0e10cSrcweir 	{
229cdf0e10cSrcweir 		CreateToolhelp32Snapshot_PROC	lpfnCreateToolhelp32Snapshot = (CreateToolhelp32Snapshot_PROC)GetProcAddress( hModKernel32, "CreateToolhelp32Snapshot" );
230cdf0e10cSrcweir 		Module32First_PROC				lpfnModule32First = (Module32First_PROC)GetProcAddress( hModKernel32, "Module32First" );
231cdf0e10cSrcweir 		Module32Next_PROC				lpfnModule32Next = (Module32Next_PROC)GetProcAddress( hModKernel32, "Module32Next" );
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 		if ( lpfnCreateToolhelp32Snapshot && lpfnModule32First && lpfnModule32Next )
234cdf0e10cSrcweir 		{
235cdf0e10cSrcweir 			HANDLE	hModuleSnap	= NULL;
236cdf0e10cSrcweir 			DWORD	dwProcessId = GetCurrentProcessId();
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 			// Take a snapshot of all modules in the specified process.
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 			hModuleSnap = lpfnCreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwProcessId );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir 			if ( INVALID_HANDLE_VALUE != hModuleSnap )
243cdf0e10cSrcweir 			{
244cdf0e10cSrcweir 				MODULEENTRY32	me32	= {0};
245cdf0e10cSrcweir 
246cdf0e10cSrcweir 				// Fill the size of the structure before using it.
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 				me32.dwSize = sizeof(MODULEENTRY32);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir 				// Walk the module list of the process, and find the module of
251cdf0e10cSrcweir 				// interest. Then copy the information to the buffer pointed
252cdf0e10cSrcweir 				// to by lpMe32 so that it can be returned to the caller.
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 				if ( lpfnModule32First(hModuleSnap, &me32) )
255cdf0e10cSrcweir 				{
256cdf0e10cSrcweir 					do
257cdf0e10cSrcweir 					{
258cdf0e10cSrcweir 						if ( (BYTE *)pv >= (BYTE *)me32.hModule && (BYTE *)pv < (BYTE *)me32.hModule + me32.modBaseSize )
259cdf0e10cSrcweir 						{
260cdf0e10cSrcweir 							rtl_uString	*ustrSysPath = NULL;
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 							rtl_string2UString( &ustrSysPath, me32.szExePath, strlen(me32.szExePath), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
263cdf0e10cSrcweir                             OSL_ASSERT(ustrSysPath != NULL);
264cdf0e10cSrcweir 							osl_getFileURLFromSystemPath( ustrSysPath, pustrURL );
265cdf0e10cSrcweir 							rtl_uString_release( ustrSysPath );
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 							bSuccess = sal_True;
268cdf0e10cSrcweir 						}
269cdf0e10cSrcweir 
270cdf0e10cSrcweir 					} while ( !bSuccess && lpfnModule32Next( hModuleSnap, &me32 ) );
271cdf0e10cSrcweir 				}
272cdf0e10cSrcweir 
273cdf0e10cSrcweir 
274cdf0e10cSrcweir 				// Do not forget to clean up the snapshot object.
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 				CloseHandle (hModuleSnap);
277cdf0e10cSrcweir 			}
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 		}
280cdf0e10cSrcweir 	}
281cdf0e10cSrcweir 
282cdf0e10cSrcweir 	return	bSuccess;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir /***************************************************************************************/
286cdf0e10cSrcweir /* Implementation for Windows NT, 2K and XP (2K and XP could use the above method too) */
287cdf0e10cSrcweir /***************************************************************************************/
288cdf0e10cSrcweir 
289cdf0e10cSrcweir #ifdef _MSC_VER
290cdf0e10cSrcweir #pragma warning(push,1) /* disable warnings within system headers */
291cdf0e10cSrcweir #endif
292cdf0e10cSrcweir #include <imagehlp.h>
293cdf0e10cSrcweir #ifdef _MSC_VER
294cdf0e10cSrcweir #pragma warning(pop)
295cdf0e10cSrcweir #endif
296cdf0e10cSrcweir 
297cdf0e10cSrcweir typedef BOOL (WINAPI *SymInitialize_PROC)(
298cdf0e10cSrcweir     HANDLE   hProcess,
299cdf0e10cSrcweir     LPSTR    UserSearchPath,
300cdf0e10cSrcweir     BOOL     fInvadeProcess
301cdf0e10cSrcweir     );
302cdf0e10cSrcweir 
303cdf0e10cSrcweir typedef BOOL (WINAPI *SymCleanup_PROC)(
304cdf0e10cSrcweir     HANDLE hProcess
305cdf0e10cSrcweir 	);
306cdf0e10cSrcweir 
307cdf0e10cSrcweir typedef BOOL (WINAPI *SymGetModuleInfo_PROC)(
308cdf0e10cSrcweir     HANDLE              hProcess,
309cdf0e10cSrcweir     DWORD               dwAddr,
310cdf0e10cSrcweir     PIMAGEHLP_MODULE  ModuleInfo
311cdf0e10cSrcweir     );
312cdf0e10cSrcweir 
313cdf0e10cSrcweir /* Seems that IMAGEHLP.DLL is always availiable on NT 4. But MSDN from Platform SDK says Win 2K is required. MSDN from VS 6.0a says
314cdf0e10cSrcweir 	it's O.K on NT 4 ???!!!
315cdf0e10cSrcweir 	BTW: We are using ANSI function because not all version of IMAGEHLP.DLL contain Unicode support
316cdf0e10cSrcweir */
317cdf0e10cSrcweir 
_osl_addressGetModuleURL_NT4(void * pv,rtl_uString ** pustrURL)318cdf0e10cSrcweir static sal_Bool SAL_CALL _osl_addressGetModuleURL_NT4( void *pv, rtl_uString **pustrURL )
319cdf0e10cSrcweir {
320cdf0e10cSrcweir 	sal_Bool	bSuccess	= sal_False;	/* Assume failure */
321cdf0e10cSrcweir 
322cdf0e10cSrcweir 	/*	IMAGEHELP.DLL has a bug that it recursivly scans subdirectories of
323cdf0e10cSrcweir 		the root when calling SymInitialize(), so we preferr DBGHELP.DLL
324cdf0e10cSrcweir 		which exports the same symbols and is shipped with OOo */
325cdf0e10cSrcweir 
326cdf0e10cSrcweir 	HMODULE		hModImageHelp = LoadLibrary( "DBGHELP.DLL" );
327cdf0e10cSrcweir 
328cdf0e10cSrcweir 	if ( !hModImageHelp )
329cdf0e10cSrcweir 		hModImageHelp = LoadLibrary( "IMAGEHLP.DLL" );
330cdf0e10cSrcweir 
331cdf0e10cSrcweir 	if ( hModImageHelp )
332cdf0e10cSrcweir 	{
333cdf0e10cSrcweir 		SymGetModuleInfo_PROC	lpfnSymGetModuleInfo;
334cdf0e10cSrcweir 		SymInitialize_PROC		lpfnSymInitialize;
335cdf0e10cSrcweir 		SymCleanup_PROC			lpfnSymCleanup;
336cdf0e10cSrcweir 
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 		lpfnSymInitialize = (SymInitialize_PROC)GetProcAddress( hModImageHelp, "SymInitialize" );
339cdf0e10cSrcweir 		lpfnSymCleanup = (SymCleanup_PROC)GetProcAddress( hModImageHelp, "SymCleanup" );
340cdf0e10cSrcweir 		lpfnSymGetModuleInfo = (SymGetModuleInfo_PROC)GetProcAddress( hModImageHelp, "SymGetModuleInfo" );
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 		if ( lpfnSymInitialize && lpfnSymCleanup && lpfnSymGetModuleInfo )
344cdf0e10cSrcweir 		{
345cdf0e10cSrcweir 			IMAGEHLP_MODULE	ModuleInfo;
346cdf0e10cSrcweir 			::osl::LongPathBuffer< sal_Char > aModuleFileName( MAX_LONG_PATH );
347cdf0e10cSrcweir 			LPSTR	lpSearchPath = NULL;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 			if ( GetModuleFileNameA( NULL, aModuleFileName, aModuleFileName.getBufSizeInSymbols() ) )
350cdf0e10cSrcweir 			{
351cdf0e10cSrcweir 				char *pLastBkSlash = strrchr( aModuleFileName, '\\' );
352cdf0e10cSrcweir 
353cdf0e10cSrcweir 				if (
354cdf0e10cSrcweir 					pLastBkSlash &&
355cdf0e10cSrcweir 					pLastBkSlash > (sal_Char*)aModuleFileName
356cdf0e10cSrcweir 					&& *(pLastBkSlash - 1) != ':'
357cdf0e10cSrcweir 					&& *(pLastBkSlash - 1) != '\\'
358cdf0e10cSrcweir 					)
359cdf0e10cSrcweir 				{
360cdf0e10cSrcweir 					*pLastBkSlash = 0;
361cdf0e10cSrcweir 					lpSearchPath = aModuleFileName;
362cdf0e10cSrcweir 				}
363cdf0e10cSrcweir 			}
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 			lpfnSymInitialize( GetCurrentProcess(), lpSearchPath, TRUE );
366cdf0e10cSrcweir 
367cdf0e10cSrcweir 			ZeroMemory( &ModuleInfo, sizeof(ModuleInfo) );
368cdf0e10cSrcweir 			ModuleInfo.SizeOfStruct = sizeof(ModuleInfo);
369cdf0e10cSrcweir 
370cdf0e10cSrcweir 			bSuccess = (sal_Bool)(!!lpfnSymGetModuleInfo( GetCurrentProcess(), (DWORD)pv, &ModuleInfo ));
371cdf0e10cSrcweir 
372cdf0e10cSrcweir 			if ( bSuccess )
373cdf0e10cSrcweir 			{
374cdf0e10cSrcweir 				/*	#99182 On localized (non-english) NT4 and XP (!!!) for some libraries the LoadedImageName member of ModuleInfo isn't filled. Because
375cdf0e10cSrcweir 					other members ModuleName and ImageName do not contain the full path we can cast the Member
376cdf0e10cSrcweir 					BaseOfImage to a HMODULE (on NT it's the same) and use GetModuleFileName to retrieve the full
377cdf0e10cSrcweir 					path of the loaded image */
378cdf0e10cSrcweir 
379cdf0e10cSrcweir 				if ( ModuleInfo.LoadedImageName[0] || GetModuleFileNameA( (HMODULE)ModuleInfo.BaseOfImage, ModuleInfo.LoadedImageName, sizeof(ModuleInfo.LoadedImageName) ) )
380cdf0e10cSrcweir 				{
381cdf0e10cSrcweir 					rtl_uString	*ustrSysPath = NULL;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 					rtl_string2UString( &ustrSysPath, ModuleInfo.LoadedImageName, strlen(ModuleInfo.LoadedImageName), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
384cdf0e10cSrcweir                     OSL_ASSERT(ustrSysPath != NULL);
385cdf0e10cSrcweir 					osl_getFileURLFromSystemPath( ustrSysPath, pustrURL );
386cdf0e10cSrcweir 					rtl_uString_release( ustrSysPath );
387cdf0e10cSrcweir 				}
388cdf0e10cSrcweir 				else
389cdf0e10cSrcweir 					bSuccess = sal_False;
390cdf0e10cSrcweir 			}
391cdf0e10cSrcweir 
392cdf0e10cSrcweir 			lpfnSymCleanup( GetCurrentProcess() );
393cdf0e10cSrcweir 		}
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 		FreeLibrary( hModImageHelp );
396cdf0e10cSrcweir 	}
397cdf0e10cSrcweir 
398cdf0e10cSrcweir 	return bSuccess;
399cdf0e10cSrcweir }
400cdf0e10cSrcweir 
401cdf0e10cSrcweir 
402cdf0e10cSrcweir typedef struct _MODULEINFO {
403cdf0e10cSrcweir     LPVOID lpBaseOfDll;
404cdf0e10cSrcweir     DWORD SizeOfImage;
405cdf0e10cSrcweir     LPVOID EntryPoint;
406cdf0e10cSrcweir } MODULEINFO, *LPMODULEINFO;
407cdf0e10cSrcweir 
408cdf0e10cSrcweir typedef BOOL (WINAPI *EnumProcessModules_PROC)(
409cdf0e10cSrcweir   HANDLE hProcess,      // handle to the process
410cdf0e10cSrcweir   HMODULE * lphModule,  // array to receive the module handles
411cdf0e10cSrcweir   DWORD cb,             // size of the array
412cdf0e10cSrcweir   LPDWORD lpcbNeeded    // receives the number of bytes returned
413cdf0e10cSrcweir );
414cdf0e10cSrcweir 
415cdf0e10cSrcweir typedef BOOL (WINAPI *GetModuleInformation_PROC)(
416cdf0e10cSrcweir   HANDLE hProcess,         // handle to the process
417cdf0e10cSrcweir   HMODULE hModule,         // handle to the module
418cdf0e10cSrcweir   LPMODULEINFO lpmodinfo,  // structure that receives information
419cdf0e10cSrcweir   DWORD cb                 // size of the structure
420cdf0e10cSrcweir );
421cdf0e10cSrcweir 
422cdf0e10cSrcweir #define bufsizeof(buffer) (sizeof(buffer) / sizeof((buffer)[0]))
423cdf0e10cSrcweir 
424cdf0e10cSrcweir /* This version can fail because PSAPI.DLL is not always part of NT 4 despite MSDN Libary 6.0a say so */
425cdf0e10cSrcweir 
_osl_addressGetModuleURL_NT(void * pv,rtl_uString ** pustrURL)426cdf0e10cSrcweir static sal_Bool SAL_CALL _osl_addressGetModuleURL_NT( void *pv, rtl_uString **pustrURL )
427cdf0e10cSrcweir {
428cdf0e10cSrcweir 	sal_Bool	bSuccess	= sal_False;	/* Assume failure */
429cdf0e10cSrcweir 	static HMODULE		hModPsapi = NULL;
430cdf0e10cSrcweir 
431cdf0e10cSrcweir 	if ( !hModPsapi )
432cdf0e10cSrcweir 		hModPsapi = LoadLibrary( "PSAPI.DLL" );
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 	if ( hModPsapi )
435cdf0e10cSrcweir 	{
436cdf0e10cSrcweir 		EnumProcessModules_PROC		lpfnEnumProcessModules		= (EnumProcessModules_PROC)GetProcAddress( hModPsapi, "EnumProcessModules" );
437cdf0e10cSrcweir 		GetModuleInformation_PROC	lpfnGetModuleInformation	= (GetModuleInformation_PROC)GetProcAddress( hModPsapi, "GetModuleInformation" );
438cdf0e10cSrcweir 
439cdf0e10cSrcweir 		if ( lpfnEnumProcessModules && lpfnGetModuleInformation )
440cdf0e10cSrcweir 		{
441cdf0e10cSrcweir 			DWORD		cbNeeded = 0;
442cdf0e10cSrcweir 			HMODULE		*lpModules = NULL;
443cdf0e10cSrcweir 			DWORD		nModules = 0;
444cdf0e10cSrcweir 			UINT		iModule = 0;
445cdf0e10cSrcweir 			MODULEINFO	modinfo;
446cdf0e10cSrcweir 
447cdf0e10cSrcweir 			lpfnEnumProcessModules( GetCurrentProcess(), NULL, 0, &cbNeeded );
448cdf0e10cSrcweir 
449cdf0e10cSrcweir 			lpModules = (HMODULE *)_alloca( cbNeeded );
450cdf0e10cSrcweir 			lpfnEnumProcessModules( GetCurrentProcess(), lpModules, cbNeeded, &cbNeeded );
451cdf0e10cSrcweir 
452cdf0e10cSrcweir 			nModules = cbNeeded / sizeof(HMODULE);
453cdf0e10cSrcweir 
454cdf0e10cSrcweir 			for ( iModule = 0; !bSuccess && iModule < nModules; iModule++ )
455cdf0e10cSrcweir 			{
456cdf0e10cSrcweir 				lpfnGetModuleInformation( GetCurrentProcess(), lpModules[iModule], &modinfo, sizeof(modinfo) );
457cdf0e10cSrcweir 
458cdf0e10cSrcweir 				if ( (BYTE *)pv >= (BYTE *)modinfo.lpBaseOfDll && (BYTE *)pv < (BYTE *)modinfo.lpBaseOfDll + modinfo.SizeOfImage )
459cdf0e10cSrcweir 				{
460cdf0e10cSrcweir                     ::osl::LongPathBuffer< sal_Unicode > aBuffer( MAX_LONG_PATH );
461cdf0e10cSrcweir 					rtl_uString	*ustrSysPath = NULL;
462cdf0e10cSrcweir 
463cdf0e10cSrcweir 					GetModuleFileNameW( lpModules[iModule], ::osl::mingw_reinterpret_cast<LPWSTR>(aBuffer), aBuffer.getBufSizeInSymbols() );
464cdf0e10cSrcweir 
465cdf0e10cSrcweir 					rtl_uString_newFromStr( &ustrSysPath, aBuffer );
466cdf0e10cSrcweir 					osl_getFileURLFromSystemPath( ustrSysPath, pustrURL );
467cdf0e10cSrcweir 					rtl_uString_release( ustrSysPath );
468cdf0e10cSrcweir 
469cdf0e10cSrcweir 					bSuccess = sal_True;
470cdf0e10cSrcweir 				}
471cdf0e10cSrcweir 			}
472cdf0e10cSrcweir 		}
473cdf0e10cSrcweir 
474cdf0e10cSrcweir 	}
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 	return bSuccess;
477cdf0e10cSrcweir }
478cdf0e10cSrcweir 
479cdf0e10cSrcweir /*****************************************************************************/
480cdf0e10cSrcweir /* Dispatcher for osl_osl_addressGetModuleURL */
481cdf0e10cSrcweir /*****************************************************************************/
482cdf0e10cSrcweir 
osl_getModuleURLFromAddress(void * pv,rtl_uString ** pustrURL)483cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromAddress( void *pv, rtl_uString **pustrURL )
484cdf0e10cSrcweir {
485cdf0e10cSrcweir 	/* Use ..._NT first because ..._NT4 is much slower */
486cdf0e10cSrcweir 	if ( IS_NT )
487cdf0e10cSrcweir 		return _osl_addressGetModuleURL_NT( pv, pustrURL ) || _osl_addressGetModuleURL_NT4( pv, pustrURL );
488cdf0e10cSrcweir 	else
489cdf0e10cSrcweir 		return _osl_addressGetModuleURL_Windows( pv, pustrURL );
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir /*****************************************************************************/
493cdf0e10cSrcweir /* osl_getModuleURLFromFunctionAddress */
494cdf0e10cSrcweir /*****************************************************************************/
osl_getModuleURLFromFunctionAddress(oslGenericFunction addr,rtl_uString ** ppLibraryUrl)495cdf0e10cSrcweir sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
496cdf0e10cSrcweir {
497cdf0e10cSrcweir     /* casting a function pointer to a data pointer (void*) is
498cdf0e10cSrcweir        not allowed according to the C/C++ standards. In this case
499cdf0e10cSrcweir        it is unavoidable because we have to stay compatible we
500cdf0e10cSrcweir        cannot remove any function. */
501cdf0e10cSrcweir #ifdef _MSC_VER
502cdf0e10cSrcweir #pragma warning(push)
503cdf0e10cSrcweir #pragma warning(disable:4054)
504cdf0e10cSrcweir #endif
505cdf0e10cSrcweir     return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl);
506cdf0e10cSrcweir #ifdef _MSC_VER
507cdf0e10cSrcweir #pragma warning(pop)
508cdf0e10cSrcweir #endif
509cdf0e10cSrcweir }
510cdf0e10cSrcweir 
511cdf0e10cSrcweir 
512