symport/symuser/loader.cpp
changeset 1 0a7b44b10206
child 2 806186ab5e14
equal deleted inserted replaced
0:c55016431358 1:0a7b44b10206
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Symbian Foundation License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <stdio.h>
       
    17 
       
    18 #include <e32ldr.h>
       
    19 #include <u32std.h>
       
    20 #include <e32std.h>
       
    21 
       
    22 #ifndef __linux__
       
    23 #include <windows.h>
       
    24 #include <winbase.h>
       
    25 
       
    26 // Windows version of the function
       
    27 TInt RLoader::LoadLocale(const TDesC& aLocaleDllName, TLibraryFunction* aExportList)
       
    28 	{
       
    29 	typedef int (__cdecl *MYPROC)(LPWSTR);
       
    30 	MYPROC myFunc;
       
    31 	TBuf<256> nameBuf;
       
    32 	nameBuf.Copy(aLocaleDllName);
       
    33 	nameBuf.ZeroTerminate();
       
    34 
       
    35 	iHandle = (TAny*)LoadLibraryEx((const WCHAR*)(nameBuf.Ptr()), NULL, DONT_RESOLVE_DLL_REFERENCES|LOAD_WITH_ALTERED_SEARCH_PATH);
       
    36 	if ( iHandle == NULL )
       
    37 		{
       
    38 		DWORD err = GetLastError();
       
    39 		printf("Error loading locale: Win system error %lu", (unsigned long)err);
       
    40 		return KErrGeneral;
       
    41 		}
       
    42 
       
    43 	for ( TInt i = 1 ; i < KNumLocaleExports ; i ++ )
       
    44 		{
       
    45 		myFunc = (MYPROC) GetProcAddress((HMODULE)iHandle, (LPCSTR)i);
       
    46 		if ( myFunc != NULL )
       
    47 			{
       
    48 			aExportList[i] = (TLibraryFunction)myFunc;
       
    49 			}
       
    50 		else
       
    51 			{
       
    52 			DWORD err = GetLastError();
       
    53 			printf("Error loading locale function: Win system error %lu", (unsigned long)err);
       
    54 			FreeLibrary((HMODULE)iHandle);
       
    55 			return KErrGeneral;
       
    56 			}
       
    57 		}
       
    58 	return KErrNone;
       
    59 	}
       
    60 
       
    61 void RLoader::Free()
       
    62 	{
       
    63 	if (iHandle != NULL)
       
    64 		{
       
    65 		FreeLibrary((HMODULE)iHandle);
       
    66 		iHandle = NULL;
       
    67 		}
       
    68 	}
       
    69 
       
    70 #else
       
    71 
       
    72 #include <dlfcn.h>
       
    73 
       
    74 // Mangled function names
       
    75 const char funcnames [22][60] =
       
    76 	{
       
    77 	"Dummy",
       
    78 	"_ZN4Locl9AmPmTableEv",
       
    79 	"_ZN4Locl7CharSetEv",
       
    80 	"_ZN4Locl9CollTableEv",
       
    81 	"_ZN4Locl14CurrencySymbolEv",
       
    82 	"_ZN4Locl15DateSuffixTableEv",
       
    83 	"_ZN4Locl11DayAbbTableEv",
       
    84 	"_ZN4Locl8DayTableEv",
       
    85 	"_ZN4Locl9FoldTableEv",
       
    86 	"_ZN4Locl8LanguageEv",
       
    87 	"_ZN4Locl10LocaleDataEP11SLocaleData",
       
    88 	"_ZN4Locl10LowerTableEv",
       
    89 	"_ZN4Locl13MonthAbbTableEv",
       
    90 	"_ZN4Locl10MonthTableEv",
       
    91 	"_ZN4Locl8MsgTableEv",
       
    92 	"_ZN4Locl9TypeTableEv",
       
    93 	"_ZN4Locl7UniCodeEv",
       
    94 	"_ZN4Locl10UpperTableEv",
       
    95 	"_ZN4Locl19ShortDateFormatSpecEv",
       
    96 	"_ZN4Locl18LongDateFormatSpecEv",
       
    97 	"_ZN4Locl19FatUtilityFunctionsEv",
       
    98 	};
       
    99 
       
   100 // Linux version of the function
       
   101 TInt RLoader::LoadLocale(const TDesC& aLocaleDllName, TLibraryFunction* aExportList)
       
   102 	{
       
   103 	TBuf8<256> nameBuf;
       
   104 	nameBuf.Copy(aLocaleDllName);
       
   105 	const char* name = reinterpret_cast<const char*>(nameBuf.PtrZ());
       
   106 
       
   107 	iHandle = dlopen(name, RTLD_NOW);
       
   108 	TLibraryFunction myFunc;
       
   109 
       
   110 	if(iHandle == NULL) {
       
   111 		printf("Error loading locale: %s\n", dlerror());
       
   112 		return KErrGeneral;
       
   113 	}
       
   114 
       
   115 	for ( TInt i = 1 ; i < KNumLocaleExports ; i ++ )
       
   116 		{
       
   117 		myFunc = (TLibraryFunction) dlsym(iHandle, funcnames[i]);
       
   118 		if ( myFunc != NULL )
       
   119 			{
       
   120 			aExportList[i] = (TLibraryFunction)myFunc;
       
   121 			}
       
   122 		else
       
   123 			{
       
   124 			printf("Error loading locale function: %s\n", dlerror());
       
   125 			dlclose(iHandle);
       
   126 			return KErrGeneral;
       
   127 			}
       
   128 		}
       
   129 	return KErrNone;
       
   130 	}
       
   131 
       
   132 void RLoader::Free()
       
   133 	{
       
   134 	if ( iHandle != NULL )
       
   135 		{
       
   136 		dlclose(iHandle);
       
   137 		iHandle = NULL;
       
   138 		}
       
   139 	}
       
   140 
       
   141 #endif
       
   142