merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / os2 / module.c
blob421b78195d83102a0351390179bf5a07c2c5038a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 #include "system.h"
31 #include <osl/module.h>
32 #include <osl/diagnose.h>
33 #include <osl/file.h>
34 #include <osl/thread.h>
36 #include <stdlib.h>
38 int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
40 // static data for holding SAL dll module and full path
41 static HMODULE hModSal;
42 static char szSalDir[ _MAX_PATH];
43 static char szSalDrive[ _MAX_PATH];
45 /*****************************************************************************/
46 /* osl_loadModule */
47 /*****************************************************************************/
49 ULONG APIENTRY _DosLoadModule (PSZ pszObject, ULONG uObjectLen, PCSZ pszModule,
50 PHMODULE phmod)
52 APIRET rc;
53 rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
54 // YD 22/05/06 issue again if first call fails (why?)
55 if (rc == ERROR_INVALID_PARAMETER)
56 rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
57 return rc;
60 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
62 HMODULE hModule;
63 BYTE szErrorMessage[256];
64 APIRET rc;
65 oslModule pModule=0;
66 rtl_uString* ustrTmp = NULL;
68 OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
70 /* ensure ustrTmp hold valid string */
71 if( osl_File_E_None != osl_getSystemPathFromFileURL( ustrModuleName, &ustrTmp ) )
72 rtl_uString_assign( &ustrTmp, ustrModuleName );
74 if( ustrTmp )
76 char buffer[PATH_MAX];
78 if( UnicodeToText( buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length ) )
80 char drive[_MAX_DRIVE], dir[_MAX_DIR];
81 char fname[_MAX_FNAME], ext[_MAX_EXT];
82 char* dot;
83 // 21/02/2006 YD dll names must be 8.3: since .uno.dll files
84 // have hardcoded names, I'm truncating names here and also in
85 // the build system
86 _splitpath (buffer, drive, dir, fname, ext);
87 if (strlen(fname)>8)
88 fname[8] = 0; // truncate to 8.3
89 dot = strchr( fname, '.');
90 if (dot)
91 *dot = '\0'; // truncate on dot
92 // if drive is not specified, remove starting \ from dir name
93 // so dll is loaded from LIBPATH
94 if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
95 while( dir[0] == '\\')
96 strcpy( dir, dir+1);
98 _makepath( buffer, drive, dir, fname, ext);
100 rc = _DosLoadModule( szErrorMessage, sizeof( szErrorMessage), (PCSZ)buffer, &hModule);
101 if (rc == NO_ERROR )
102 pModule = (oslModule)hModule;
103 else
105 if (rc == NO_ERROR )
106 pModule = (oslModule)hModule;
107 else
109 sal_Char szError[ PATH_MAX*2 ];
110 sprintf( szError, "Module: %s; rc: %d;\nReason: %s;\n"
111 "Please contact technical support and report above informations.\n\n",
112 buffer, rc, szErrorMessage );
113 #if OSL_DEBUG_LEVEL>0
114 fprintf( stderr, szError);
115 #endif
116 //OSL_TRACE(szError);
117 #ifndef OSL_DEBUG_LEVEL
118 WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
119 szError, "Critical error: DosLoadModule failed",
120 0, MB_ERROR | MB_OK | MB_MOVEABLE);
121 #endif
127 rtl_uString_release( ustrTmp );
129 return pModule;
132 /*****************************************************************************/
133 /* osl_getModuleHandle */
134 /*****************************************************************************/
136 sal_Bool SAL_CALL
137 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
139 HMODULE hmod;
140 APIRET rc;
141 rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
142 if( rc == NO_ERROR)
144 *pResult = (oslModule) hmod;
145 return sal_True;
148 return sal_False;
151 /*****************************************************************************/
152 /* osl_unloadModule */
153 /*****************************************************************************/
154 void SAL_CALL osl_unloadModule(oslModule Module)
156 #if OSL_DEBUG_LEVEL>0
157 if (!Module)
158 fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
159 #endif
161 DosFreeModule((HMODULE)Module);
164 /*****************************************************************************/
165 /* osl_getSymbol */
166 /*****************************************************************************/
167 void* SAL_CALL
168 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
170 return (void *) osl_getFunctionSymbol(Module, pSymbolName);
173 /*****************************************************************************/
174 /* osl_getFunctionSymbol */
175 /*****************************************************************************/
176 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
178 rtl_String *symbolName = NULL;
179 oslGenericFunction address;
181 OSL_ASSERT(Module);
182 OSL_ASSERT(strSymbolName);
184 rtl_uString2String(
185 &symbolName,
186 strSymbolName->buffer,
187 strSymbolName->length,
188 RTL_TEXTENCODING_UTF8,
189 OUSTRING_TO_OSTRING_CVTFLAGS
192 address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
193 rtl_string_release(symbolName);
195 return address;
198 /*****************************************************************************/
199 /* osl_getAsciiFunctionSymbol */
200 /*****************************************************************************/
201 oslGenericFunction SAL_CALL
202 osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
204 PFN pFunction;
205 APIRET rc;
206 void* pHandle=0;
208 OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
209 OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
211 if ( Module!= 0 && pSymbol != 0 )
214 rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
215 if( rc == NO_ERROR )
217 pHandle = (void*)pFunction;
219 else
221 // YD try again adding the '_' prefix
222 char _pszSymbolName[255];
223 strcpy( _pszSymbolName, "_");
224 strcat( _pszSymbolName, pSymbol);
225 rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
226 if( rc == NO_ERROR )
227 pHandle = (void*)pFunction;
232 return pHandle;
235 /*****************************************************************************/
236 /* osl_getModuleURLFromAddress */
237 /*****************************************************************************/
238 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
240 //APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
241 // ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
242 HMODULE hMod;
243 ULONG ObjNum;
244 CHAR Buff[2*_MAX_PATH];
245 ULONG Offset;
246 APIRET rc;
248 // get module handle (and name)
249 rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
250 if (rc)
251 return sal_False;
253 // get module full path
254 rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
255 if (rc)
256 return sal_False;
258 #if OSL_DEBUG_LEVEL > 1
259 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
260 #endif
262 // convert to URL
263 rtl_uString *ustrSysPath = NULL;
264 rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
265 OSL_ASSERT(ustrSysPath != NULL);
266 osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
267 rtl_uString_release( ustrSysPath );
269 return sal_True;
272 /*****************************************************************************/
273 /* osl_getModuleURLFromFunctionAddress */
274 /*****************************************************************************/
275 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
277 return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
280 /*****************************************************************************/