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 ************************************************************************/
31 #include <osl/module.h>
32 #include <osl/diagnose.h>
34 #include <osl/thread.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 /*****************************************************************************/
47 /*****************************************************************************/
49 ULONG APIENTRY
_DosLoadModule (PSZ pszObject
, ULONG uObjectLen
, PCSZ pszModule
,
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
);
60 oslModule SAL_CALL
osl_loadModule(rtl_uString
*ustrModuleName
, sal_Int32 nRtldMode
)
63 BYTE szErrorMessage
[256];
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
);
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
];
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
86 _splitpath (buffer
, drive
, dir
, fname
, ext
);
88 fname
[8] = 0; // truncate to 8.3
89 dot
= strchr( fname
, '.');
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] == '\\')
98 _makepath( buffer
, drive
, dir
, fname
, ext
);
100 rc
= _DosLoadModule( szErrorMessage
, sizeof( szErrorMessage
), (PCSZ
)buffer
, &hModule
);
102 pModule
= (oslModule
)hModule
;
106 pModule
= (oslModule
)hModule
;
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
);
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
);
127 rtl_uString_release( ustrTmp
);
132 /*****************************************************************************/
133 /* osl_getModuleHandle */
134 /*****************************************************************************/
137 osl_getModuleHandle(rtl_uString
*pModuleName
, oslModule
*pResult
)
141 rc
= DosQueryModuleHandle(pModuleName
->buffer
, &hmod
);
144 *pResult
= (oslModule
) hmod
;
151 /*****************************************************************************/
152 /* osl_unloadModule */
153 /*****************************************************************************/
154 void SAL_CALL
osl_unloadModule(oslModule Module
)
156 #if OSL_DEBUG_LEVEL>0
158 fprintf( stderr
, "osl_unloadModule NULL HANDLE.\n");
161 DosFreeModule((HMODULE
)Module
);
164 /*****************************************************************************/
166 /*****************************************************************************/
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
;
182 OSL_ASSERT(strSymbolName
);
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
);
198 /*****************************************************************************/
199 /* osl_getAsciiFunctionSymbol */
200 /*****************************************************************************/
201 oslGenericFunction SAL_CALL
202 osl_getAsciiFunctionSymbol( oslModule Module
, const sal_Char
*pSymbol
)
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
);
217 pHandle
= (void*)pFunction
;
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
);
227 pHandle
= (void*)pFunction
;
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)
244 CHAR Buff
[2*_MAX_PATH
];
248 // get module handle (and name)
249 rc
= DosQueryModFromEIP( &hMod
, &ObjNum
, sizeof( Buff
), Buff
, &Offset
, (ULONG
)addr
);
253 // get module full path
254 rc
= DosQueryModuleName( hMod
, sizeof( Buff
), Buff
);
258 #if OSL_DEBUG_LEVEL > 1
259 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", Buff
);
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
);
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 /*****************************************************************************/