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: module.c,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 ************************************************************************/
34 #include <osl/module.h>
35 #include <osl/diagnose.h>
37 #include <osl/thread.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 /*****************************************************************************/
50 /*****************************************************************************/
52 ULONG APIENTRY
_DosLoadModule (PSZ pszObject
, ULONG uObjectLen
, PCSZ pszModule
,
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
);
63 oslModule SAL_CALL
osl_loadModule(rtl_uString
*ustrModuleName
, sal_Int32 nRtldMode
)
66 BYTE szErrorMessage
[256];
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
);
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
];
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
89 _splitpath (buffer
, drive
, dir
, fname
, ext
);
91 fname
[8] = 0; // truncate to 8.3
92 dot
= strchr( fname
, '.');
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] == '\\')
101 _makepath( buffer
, drive
, dir
, fname
, ext
);
103 rc
= _DosLoadModule( szErrorMessage
, sizeof( szErrorMessage
), (PCSZ
)buffer
, &hModule
);
105 pModule
= (oslModule
)hModule
;
109 pModule
= (oslModule
)hModule
;
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
);
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
);
130 rtl_uString_release( ustrTmp
);
135 /*****************************************************************************/
136 /* osl_getModuleHandle */
137 /*****************************************************************************/
140 osl_getModuleHandle(rtl_uString
*pModuleName
, oslModule
*pResult
)
144 rc
= DosQueryModuleHandle(pModuleName
->buffer
, &hmod
);
147 *pResult
= (oslModule
) hmod
;
154 /*****************************************************************************/
155 /* osl_unloadModule */
156 /*****************************************************************************/
157 void SAL_CALL
osl_unloadModule(oslModule Module
)
159 #if OSL_DEBUG_LEVEL>0
161 fprintf( stderr
, "osl_unloadModule NULL HANDLE.\n");
164 DosFreeModule((HMODULE
)Module
);
167 /*****************************************************************************/
169 /*****************************************************************************/
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
;
185 OSL_ASSERT(strSymbolName
);
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
);
201 /*****************************************************************************/
202 /* osl_getAsciiFunctionSymbol */
203 /*****************************************************************************/
204 oslGenericFunction SAL_CALL
205 osl_getAsciiFunctionSymbol( oslModule Module
, const sal_Char
*pSymbol
)
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
);
220 pHandle
= (void*)pFunction
;
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
);
230 pHandle
= (void*)pFunction
;
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)
247 CHAR Buff
[2*_MAX_PATH
];
251 // get module handle (and name)
252 rc
= DosQueryModFromEIP( &hMod
, &ObjNum
, sizeof( Buff
), Buff
, &Offset
, (ULONG
)addr
);
256 // get module full path
257 rc
= DosQueryModuleName( hMod
, sizeof( Buff
), Buff
);
261 #if OSL_DEBUG_LEVEL > 1
262 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff
);
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
);
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 /*****************************************************************************/