update dev300-m58
[ooovba.git] / sal / osl / os2 / module.c
blobdfc188765b6fc530c7d91ef14e6a830475724f0a
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: module.c,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
32 #include "system.h"
34 #include <osl/module.h>
35 #include <osl/diagnose.h>
36 #include <osl/file.h>
37 #include <osl/thread.h>
39 #include <stdlib.h>
41 int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32);
43 // static data for holding SAL dll module and full path
44 static HMODULE hModSal;
45 static char szSalDir[ _MAX_PATH];
46 static char szSalDrive[ _MAX_PATH];
48 /*****************************************************************************/
49 /* osl_loadModule */
50 /*****************************************************************************/
52 ULONG APIENTRY _DosLoadModule (PSZ pszObject, ULONG uObjectLen, PCSZ pszModule,
53 PHMODULE phmod)
55 APIRET rc;
56 rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
57 // YD 22/05/06 issue again if first call fails (why?)
58 if (rc == ERROR_INVALID_PARAMETER)
59 rc = DosLoadModule( pszObject, uObjectLen, pszModule, phmod);
60 return rc;
63 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode)
65 HMODULE hModule;
66 BYTE szErrorMessage[256];
67 APIRET rc;
68 oslModule pModule=0;
69 rtl_uString* ustrTmp = NULL;
71 OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid");
73 /* ensure ustrTmp hold valid string */
74 if( osl_File_E_None != osl_getSystemPathFromFileURL( ustrModuleName, &ustrTmp ) )
75 rtl_uString_assign( &ustrTmp, ustrModuleName );
77 if( ustrTmp )
79 char buffer[PATH_MAX];
81 if( UnicodeToText( buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length ) )
83 char drive[_MAX_DRIVE], dir[_MAX_DIR];
84 char fname[_MAX_FNAME], ext[_MAX_EXT];
85 char* dot;
86 // 21/02/2006 YD dll names must be 8.3: since .uno.dll files
87 // have hardcoded names, I'm truncating names here and also in
88 // the build system
89 _splitpath (buffer, drive, dir, fname, ext);
90 if (strlen(fname)>8)
91 fname[8] = 0; // truncate to 8.3
92 dot = strchr( fname, '.');
93 if (dot)
94 *dot = '\0'; // truncate on dot
95 // if drive is not specified, remove starting \ from dir name
96 // so dll is loaded from LIBPATH
97 if (drive[0] == 0 && dir[0] == '\\' && dir[1] == '\\') {
98 while( dir[0] == '\\')
99 strcpy( dir, dir+1);
101 _makepath( buffer, drive, dir, fname, ext);
103 rc = _DosLoadModule( szErrorMessage, sizeof( szErrorMessage), (PCSZ)buffer, &hModule);
104 if (rc == NO_ERROR )
105 pModule = (oslModule)hModule;
106 else
108 if (rc == NO_ERROR )
109 pModule = (oslModule)hModule;
110 else
112 sal_Char szError[ 120 ];
113 sprintf( szError, "Module: %s; rc: %d;\nReason: %s;\n"
114 "Please contact technical support and report above informations.\n\n",
115 buffer, rc, szErrorMessage );
116 #if OSL_DEBUG_LEVEL>0
117 fprintf( stderr, szError);
118 #endif
119 //OSL_TRACE(szError);
120 #ifndef OSL_DEBUG_LEVEL
121 WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,
122 szError, "Critical error: DosLoadModule failed",
123 0, MB_ERROR | MB_OK | MB_MOVEABLE);
124 #endif
130 rtl_uString_release( ustrTmp );
132 return pModule;
135 /*****************************************************************************/
136 /* osl_getModuleHandle */
137 /*****************************************************************************/
139 sal_Bool SAL_CALL
140 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult)
142 HMODULE hmod;
143 APIRET rc;
144 rc = DosQueryModuleHandle(pModuleName->buffer, &hmod);
145 if( rc == NO_ERROR)
147 *pResult = (oslModule) hmod;
148 return sal_True;
151 return sal_False;
154 /*****************************************************************************/
155 /* osl_unloadModule */
156 /*****************************************************************************/
157 void SAL_CALL osl_unloadModule(oslModule Module)
159 #if OSL_DEBUG_LEVEL>0
160 if (!Module)
161 fprintf( stderr, "osl_unloadModule NULL HANDLE.\n");
162 #endif
164 DosFreeModule((HMODULE)Module);
167 /*****************************************************************************/
168 /* osl_getSymbol */
169 /*****************************************************************************/
170 void* SAL_CALL
171 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName)
173 return (void *) osl_getFunctionSymbol(Module, pSymbolName);
176 /*****************************************************************************/
177 /* osl_getFunctionSymbol */
178 /*****************************************************************************/
179 oslGenericFunction SAL_CALL osl_getFunctionSymbol( oslModule Module, rtl_uString *strSymbolName )
181 rtl_String *symbolName = NULL;
182 oslGenericFunction address;
184 OSL_ASSERT(Module);
185 OSL_ASSERT(strSymbolName);
187 rtl_uString2String(
188 &symbolName,
189 strSymbolName->buffer,
190 strSymbolName->length,
191 RTL_TEXTENCODING_UTF8,
192 OUSTRING_TO_OSTRING_CVTFLAGS
195 address=osl_getAsciiFunctionSymbol(Module, rtl_string_getStr(symbolName));
196 rtl_string_release(symbolName);
198 return address;
201 /*****************************************************************************/
202 /* osl_getAsciiFunctionSymbol */
203 /*****************************************************************************/
204 oslGenericFunction SAL_CALL
205 osl_getAsciiFunctionSymbol( oslModule Module, const sal_Char *pSymbol )
207 PFN pFunction;
208 APIRET rc;
209 void* pHandle=0;
211 OSL_ENSURE(Module,"osl_getSymbol : module handle is not valid");
212 OSL_ENSURE(Module,"osl_getSymbol : ustrSymbolName");
214 if ( Module!= 0 && pSymbol != 0 )
217 rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)pSymbol, &pFunction );
218 if( rc == NO_ERROR )
220 pHandle = (void*)pFunction;
222 else
224 // YD try again adding the '_' prefix
225 char _pszSymbolName[255];
226 strcpy( _pszSymbolName, "_");
227 strcat( _pszSymbolName, pSymbol);
228 rc = DosQueryProcAddr( (HMODULE) Module, 0, (PCSZ)_pszSymbolName, &pFunction );
229 if( rc == NO_ERROR )
230 pHandle = (void*)pFunction;
235 return pHandle;
238 /*****************************************************************************/
239 /* osl_getModuleURLFromAddress */
240 /*****************************************************************************/
241 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl)
243 //APIRET APIENTRY DosQueryModFromEIP (HMODULE *phMod, ULONG *pObjNum,
244 // ULONG BuffLen, PCHAR pBuff, ULONG *pOffset, ULONG Address)
245 HMODULE hMod;
246 ULONG ObjNum;
247 CHAR Buff[2*_MAX_PATH];
248 ULONG Offset;
249 APIRET rc;
251 // get module handle (and name)
252 rc = DosQueryModFromEIP( &hMod, &ObjNum, sizeof( Buff), Buff, &Offset, (ULONG)addr);
253 if (rc)
254 return sal_False;
256 // get module full path
257 rc = DosQueryModuleName( hMod, sizeof( Buff), Buff);
258 if (rc)
259 return sal_False;
261 #if OSL_DEBUG_LEVEL > 1
262 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff);
263 #endif
265 // convert to URL
266 rtl_uString *ustrSysPath = NULL;
267 rtl_string2UString( &ustrSysPath, Buff, strlen(Buff), osl_getThreadTextEncoding(), OSTRING_TO_OUSTRING_CVTFLAGS );
268 OSL_ASSERT(ustrSysPath != NULL);
269 osl_getFileURLFromSystemPath( ustrSysPath, ppLibraryUrl );
270 rtl_uString_release( ustrSysPath );
272 return sal_True;
275 /*****************************************************************************/
276 /* osl_getModuleURLFromFunctionAddress */
277 /*****************************************************************************/
278 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress( oslGenericFunction addr, rtl_uString ** ppLibraryUrl )
280 return osl_getModuleURLFromAddress( ( void * )addr, ppLibraryUrl );
283 /*****************************************************************************/