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