kernel/eka/include/emulator.h
changeset 0 a41df078684a
child 39 2bb754abd467
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1998-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 "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32\include\emulator.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @publishedPartner
       
    21  @released
       
    22 */
       
    23 
       
    24 #ifndef __EMULATOR_H__
       
    25 #define __EMULATOR_H__
       
    26 
       
    27 #include <e32def.h>
       
    28 #define WIN32_LEAN_AND_MEAN
       
    29 #pragma warning( disable : 4201 ) // nonstandard extension used : nameless struct/union
       
    30 #include <windows.h>
       
    31 #pragma warning( default : 4201 ) // nonstandard extension used : nameless struct/union
       
    32 
       
    33 class TUidType;
       
    34 class TProcessCreateInfo;
       
    35 
       
    36 static const BYTE KWin32SectionName_Symbian[IMAGE_SIZEOF_SHORT_NAME]	= {'.','S','Y','M','B','I','A','N'};
       
    37 static const BYTE KWin32SectionName_Import[IMAGE_SIZEOF_SHORT_NAME]		= {'.','i','d','a','t','a','\0','\0'};
       
    38 static const BYTE KWin32SectionName_EpocData[IMAGE_SIZEOF_SHORT_NAME]	= {'.','d','a','t','a','\0','\0','\0'};
       
    39 static const BYTE KWin32SectionName_EpocBss[IMAGE_SIZEOF_SHORT_NAME]	= {'.','b','s','s','\0','\0','\0','\0'};
       
    40 static const BYTE KWin32SectionName_Text[IMAGE_SIZEOF_SHORT_NAME]		= {'.','t','e','x','t','\0','\0','\0'};
       
    41 static const BYTE KWin32SectionName_RData[IMAGE_SIZEOF_SHORT_NAME]		= {'.','r','d','a','t','a','\0','\0'};
       
    42 static const BYTE KWin32SectionName_NmdExpData[IMAGE_SIZEOF_SHORT_NAME]	= {'.','e','x','p','d','a','t','a'};
       
    43 
       
    44 /**
       
    45 Emulator utility functions. These APIs should only be used for extensions to emulator 
       
    46 functionality and are unsupported on non-emulator platforms.
       
    47 
       
    48 @publishedPartner
       
    49 @released
       
    50 */
       
    51 struct Emulator
       
    52 	{
       
    53 public:
       
    54 
       
    55 	/**
       
    56 	Declares a function pointer to TLock.
       
    57 	
       
    58 	@see TLock.
       
    59 	*/
       
    60 	typedef void (*TLockFunc)();
       
    61 	
       
    62 	/**
       
    63 	Declares a function pointer to Win32SEHException.
       
    64 	
       
    65 	@see Win32SEHException.		
       
    66 	*/
       
    67 	typedef DWORD (*TExceptionFunc)(EXCEPTION_RECORD* aException, CONTEXT* aContext);
       
    68 	
       
    69 	/**
       
    70 	A structure which packs parameters to be inintialized. 
       
    71 	*/
       
    72 	struct SInit
       
    73 		{
       
    74 		TUint iCodePage;
       
    75 		TLockFunc iLock;
       
    76 		TLockFunc iUnlock;
       
    77 		TLockFunc iEscape;
       
    78 		TLockFunc iReenter;
       
    79 		TExceptionFunc iException;
       
    80 		};
       
    81 	
       
    82 	/**
       
    83 	A structure which handles the global lock for host interaction.
       
    84 	*/
       
    85 	struct TLock
       
    86 		{
       
    87 		
       
    88 		/**
       
    89 		Constructor which calls the Emulator::Lock() function.
       
    90 		
       
    91 		@see Emulator::Lock().
       
    92 		*/
       
    93 		inline TLock()
       
    94 			{Emulator::Lock();}
       
    95 		
       
    96 		/**
       
    97 		Destructor which calls the Emulator::Unlock() function.
       
    98 		
       
    99 		@see Emulator::Unlock().
       
   100 		*/
       
   101 		inline ~TLock()
       
   102 			{Emulator::Unlock();}
       
   103 		};
       
   104 		
       
   105 	/**
       
   106 	A structure which handles current thread by taking in or out of the emulator scheduling model.
       
   107 	*/
       
   108 	struct TEscape
       
   109 		{
       
   110 		
       
   111 		/**
       
   112 		Constructor which calls the Emulator::Escape() function.
       
   113 		
       
   114 		@see Emulator::Escape().
       
   115 		*/
       
   116 		inline TEscape()
       
   117 			{Emulator::Escape();}
       
   118 		
       
   119 		/**
       
   120 		Destructor which calls the Emulator::Reenter() function.
       
   121 		
       
   122 		@see Emulator::Reenter().
       
   123 		*/
       
   124 		inline ~TEscape()
       
   125 			{Emulator::Reenter();}
       
   126 		};
       
   127 #define __LOCK_HOST Emulator::TLock __lock
       
   128 #define __ESCAPE_HOST Emulator::TEscape __escape
       
   129 public:
       
   130 
       
   131 	IMPORT_C static void Init(const SInit& aInit);
       
   132 //
       
   133 	IMPORT_C static void Lock();
       
   134 	IMPORT_C static void Unlock();
       
   135 	IMPORT_C static void Escape();
       
   136 	IMPORT_C static void Reenter();
       
   137 //
       
   138 	IMPORT_C static DWORD Win32SEHException(EXCEPTION_RECORD* aException, CONTEXT* aContext);
       
   139 //
       
   140 	IMPORT_C static TInt LastError();
       
   141 	IMPORT_C static BOOL CreateDirectory(LPCTSTR, LPSECURITY_ATTRIBUTES);
       
   142 	IMPORT_C static BOOL CreateAllDirectories(LPCSTR);
       
   143 	IMPORT_C static HANDLE CreateFile(LPCTSTR ,DWORD ,DWORD ,LPSECURITY_ATTRIBUTES ,DWORD ,DWORD ,HANDLE);
       
   144 	IMPORT_C static BOOL DeleteFile(LPCTSTR);
       
   145 	IMPORT_C static HANDLE FindFirstFile(LPCTSTR ,LPWIN32_FIND_DATA);	
       
   146 	IMPORT_C static BOOL FindNextFile(HANDLE ,LPWIN32_FIND_DATA);
       
   147 	IMPORT_C static BOOL GetDiskFreeSpace(LPCTSTR ,LPDWORD ,LPDWORD ,LPDWORD ,LPDWORD);
       
   148 	IMPORT_C static DWORD GetFileAttributes(LPCTSTR);
       
   149 	IMPORT_C static HMODULE GetModuleHandle(LPCTSTR);
       
   150 	IMPORT_C static DWORD GetCurrentDirectory(DWORD ,LPTSTR);
       
   151 	IMPORT_C static DWORD GetTempPath(DWORD ,LPTSTR);
       
   152 	IMPORT_C static BOOL GetVolumeInformation(LPCTSTR ,LPTSTR ,DWORD ,LPDWORD ,LPDWORD ,LPDWORD ,LPTSTR,DWORD);
       
   153 	IMPORT_C static HMODULE LoadLibrary(LPCTSTR);	
       
   154 	IMPORT_C static BOOL FreeLibrary(HMODULE);	 
       
   155 	IMPORT_C static BOOL MoveFile(LPCTSTR ,LPCTSTR);
       
   156 	IMPORT_C static BOOL CopyFile(LPCTSTR ,LPCTSTR, BOOL);
       
   157 	IMPORT_C static VOID OutputDebugString(LPCTSTR);	
       
   158 	IMPORT_C static BOOL RemoveDirectory(LPCTSTR);
       
   159 	IMPORT_C static BOOL SetFileAttributes(LPCTSTR ,DWORD);	
       
   160 	IMPORT_C static BOOL SetVolumeLabel(LPCTSTR ,LPCTSTR);	
       
   161 	IMPORT_C static FARPROC GetProcAddress(HMODULE, LPCSTR);
       
   162 	IMPORT_C static DWORD GetModuleFileName(HMODULE hModule, LPWSTR lpFilename);
       
   163 public:
       
   164 
       
   165 	/**
       
   166 	A class which holds a loaded module of the file system. 
       
   167 	*/
       
   168 	class TModule
       
   169 		{
       
   170 	public:
       
   171 	
       
   172 		/**
       
   173 		Constructor which sets the handles of loaded module	to current instance of the application.
       
   174 		
       
   175 		@param aModule		A handle to the current instance of the application.
       
   176 		*/
       
   177 		inline TModule(HINSTANCE aModule)
       
   178 			:iModule(aModule), iBase(aModule)
       
   179 			{}
       
   180 			
       
   181 		IMPORT_C TModule(LPCSTR aModuleName);
       
   182 		
       
   183 		/**
       
   184 		Checks the validity of the loaded module.
       
   185 		
       
   186 		@return		TRUE, if the function succeeds
       
   187 					FALSE, if the function fails		 
       
   188 		*/
       
   189 		inline TBool IsValid() const
       
   190 			{return iBase!=NULL;}
       
   191 			
       
   192 		/**
       
   193 		Relocates the address of the loaded module by appending aRawVirtualAddress to the handle of loaded module.
       
   194 		
       
   195 		@param aRawVirtualAddress	Contains the address of the first byte of the section when loaded into 
       
   196 									memory, relative to the image base. For object files, this is the 
       
   197 									address of the first byte before relocation is applied.
       
   198 		
       
   199 		@return 	Returns the actual address of the handle.
       
   200 		*/
       
   201 		inline const TAny* Translate(TInt32 aRawVirtualAddress) const
       
   202 			{return (const TUint8*)iBase + aRawVirtualAddress;}
       
   203 	
       
   204 		
       
   205 		IMPORT_C void GetUids(TUidType& aType) const;
       
   206 		IMPORT_C void GetInfo(TProcessCreateInfo& aInfo) const;
       
   207 	public:
       
   208 	
       
   209 		/**
       
   210 		Default constructor which sets the handles of loaded module or mapped file image to the default handle.	
       
   211 		*/
       
   212 		inline TModule()
       
   213 			:iModule(0), iBase(0)
       
   214 			{}
       
   215 	
       
   216 		/**
       
   217 		Checks whether the current module is a loaded module, or just a mapped file image.
       
   218 		
       
   219 		@return		TRUE, if the current module is a loaded module 
       
   220 					FALSE, if the current module is a mapped image 
       
   221 		*/
       
   222 		TBool IsLoaded() const		// Is this a loaded module, or just a mapped file image.
       
   223 			{return iModule != 0;}
       
   224 			
       
   225 		IMPORT_C const IMAGE_NT_HEADERS32* NtHeader() const;
       
   226 		
       
   227 		/**
       
   228 		Passes the handle of a loaded module to the calling module.
       
   229 		
       
   230 		@return 	Returns a handle of a loaded module.
       
   231 	    */
       
   232 		const TAny* Base() const
       
   233 			{return iBase;}
       
   234 
       
   235 		IMPORT_C const IMAGE_SECTION_HEADER* SectionHeader(const BYTE aSection[]) const;
       
   236 		IMPORT_C const TAny* Section(const BYTE aSection[]) const;	
       
   237 		IMPORT_C const IMAGE_IMPORT_DESCRIPTOR* Imports() const;
       
   238 	public:
       
   239 	
       
   240 		/**
       
   241 		A handle to a loaded module.
       
   242 		*/
       
   243 		HMODULE iModule;
       
   244 		
       
   245 		/**
       
   246 		A handle to a loaded module for the mapped view.
       
   247 		*/
       
   248 		const TAny* iBase;
       
   249 		};
       
   250 		
       
   251 	/**
       
   252 	A class which holds a mapped image of the file system.
       
   253 	*/
       
   254 	class RImageFile : public TModule
       
   255 		{
       
   256 	public:
       
   257 	
       
   258 		/**
       
   259 		Constructor which sets the handle of the mapped image file to default handle.	
       
   260 		*/
       
   261 		inline RImageFile()
       
   262 			: iMapping(0)
       
   263 			{}
       
   264 			
       
   265 		IMPORT_C TInt Open(LPCTSTR aImageFile);
       
   266 		IMPORT_C void Close();
       
   267 	private:
       
   268 	
       
   269 		/**
       
   270 		A handle to the mapped image file.
       
   271 		*/
       
   272 		HANDLE iMapping;
       
   273 		};
       
   274 	};
       
   275 
       
   276 
       
   277 #endif