sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: // All rights reserved.
sl@0: // This component and the accompanying materials are made available
sl@0: // under the terms of the License "Eclipse Public License v1.0"
sl@0: // which accompanies this distribution, and is available
sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: //
sl@0: // Initial Contributors:
sl@0: // Nokia Corporation - initial contribution.
sl@0: //
sl@0: // Contributors:
sl@0: //
sl@0: // Description:
sl@0: //
sl@0: 
sl@0: #define _CRTIMP			// we want to use the static runtime library
sl@0: #include <e32cmn.h>
sl@0: #include <e32cmn_private.h>
sl@0: #include <string.h>
sl@0: #include <emulator.h>
sl@0: #include <e32ldr.h>
sl@0: #include <e32ldr_private.h>
sl@0: #include <e32uid.h>
sl@0: 
sl@0: #pragma data_seg(".data2")
sl@0: #ifdef __VC32__
sl@0: #pragma bss_seg(".data2")
sl@0: #endif
sl@0: static TBool UnicodeHost;
sl@0: static Emulator::SInit Data;
sl@0: #pragma data_seg()
sl@0: #ifdef __VC32__
sl@0: #pragma bss_seg()
sl@0: #endif
sl@0: 
sl@0: /**	
sl@0: Initializes a module and prepares it to handle requests.
sl@0: 
sl@0: @param aInit	An object of the structure SInit.
sl@0: 
sl@0: @see SInit.
sl@0: */
sl@0: EXPORT_C void Emulator::Init(const Emulator::SInit& aInit)
sl@0: 	{
sl@0: 	UnicodeHost = (GetVersion() & 0x80000000) ? FALSE : TRUE;
sl@0: 	Data = aInit;
sl@0: 	if (Data.iCodePage == 0)
sl@0: 		Data.iCodePage = CP_ACP;
sl@0: 	}
sl@0: 
sl@0: LOCAL_C DWORD UStringLength(LPCSTR lpMultiByteStr)
sl@0: //
sl@0: // returns length of unicode string generated - assumes null terminated string
sl@0: //
sl@0: 	{
sl@0: 	return MultiByteToWideChar(Data.iCodePage,0,lpMultiByteStr,-1,0,0);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: LOCAL_C DWORD ConvertToUnicode(LPCSTR aNarrow,LPWSTR aUnicode,DWORD aLength,BOOL aCharsRequired)
sl@0: //
sl@0: // Converts narrow string to unicode string
sl@0: //
sl@0: 	{
sl@0: 	DWORD uniLength=UStringLength(aNarrow);
sl@0: 	if(uniLength>aLength || uniLength==0)
sl@0: 		return aCharsRequired ? uniLength : 0;
sl@0: 	uniLength=MultiByteToWideChar(Data.iCodePage,0,aNarrow,-1,aUnicode,aLength);
sl@0: 	// return number of characters excluding the null terminator
sl@0: 	return uniLength ? uniLength-1 : 0;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: LOCAL_C DWORD ConvertToNarrow(LPCWSTR aUnicode,LPSTR aNarrow,DWORD aLength)
sl@0: //
sl@0: // Converts unicode string to narrow string
sl@0: //
sl@0: 	{
sl@0: 	return WideCharToMultiByte(Data.iCodePage,0,aUnicode,-1,aNarrow,aLength,"@",NULL);
sl@0: 	}
sl@0: 
sl@0: template <TUint S>
sl@0: struct Buf8
sl@0: 	{
sl@0: public:
sl@0: 	Buf8(LPCWSTR aUnicode);
sl@0: 	inline operator LPCSTR() const
sl@0: 		{return iPtr;}
sl@0: private:
sl@0: 	const char* iPtr;
sl@0: 	char iBuf[S];
sl@0: 	};
sl@0: 
sl@0: template <TUint S>
sl@0: Buf8<S>::Buf8(LPCWSTR aUnicode)
sl@0: 	{
sl@0: 	if (aUnicode)
sl@0: 		{
sl@0: 		iPtr = iBuf;
sl@0: 		ConvertToNarrow(aUnicode,iBuf,S);
sl@0: 		}
sl@0: 	else
sl@0: 		{
sl@0: 		iPtr = NULL;
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: /**	
sl@0: Acquires the global lock for host interaction.
sl@0: */
sl@0: EXPORT_C void Emulator::Lock()
sl@0: 	{
sl@0: 	Data.iLock();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Releases the global lock for host interaction.
sl@0: This may overwrite the error code for this thread and that is not the behaviour expected, 
sl@0: so save and restore it.
sl@0: */
sl@0: EXPORT_C void Emulator::Unlock()
sl@0: 	{
sl@0: 	DWORD error=GetLastError();
sl@0: 	Data.iUnlock();
sl@0: 	SetLastError(error);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Takes the current thread out of the emulator scheduling model.
sl@0: */
sl@0: EXPORT_C void Emulator::Escape()
sl@0: 	{
sl@0: 	Data.iEscape();
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Returns the calling thread into the emulator scheduling model.
sl@0: This may overwrite the error code for this thread and that is not the behaviour expected, 
sl@0: so save and restore it.
sl@0: */
sl@0: EXPORT_C void Emulator::Reenter()
sl@0: 	{
sl@0: 	DWORD error=GetLastError();
sl@0: 	Data.iReenter();
sl@0: 	SetLastError(error);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Jumps to the NThread Win32 SEH exception handler.
sl@0: 
sl@0: @param aException	Describes an exception.
sl@0: @param aContext		Describes the context.
sl@0: 	
sl@0: @return   A handler to handle the exception occurred
sl@0: */
sl@0: EXPORT_C DWORD Emulator::Win32SEHException(EXCEPTION_RECORD* aException, CONTEXT* aContext)
sl@0: 	{
sl@0: 	return Data.iException(aException, aContext);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 CreateDirectory API (see http://msdn2.microsoft.com/en-us/library/aa363855.aspx).
sl@0: */  
sl@0: EXPORT_C BOOL Emulator::CreateDirectory(LPCWSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::CreateDirectoryW(lpPathName, lpSecurityAttributes);
sl@0: 
sl@0: 	return ::CreateDirectoryA(Buf8<MAX_PATH>(lpPathName),lpSecurityAttributes);
sl@0: 	}
sl@0: 
sl@0: #ifdef __VC32__
sl@0: //disable unreachable code warning in VC++
sl@0: #pragma warning (disable : 4702)
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /**
sl@0: Recursively ensures that the full directory exists.
sl@0: 	
sl@0: @param aPathName  	Provides the path name.
sl@0: 
sl@0: @return  TRUE, if the function succeeds
sl@0: 		 FALSE, if the function fails
sl@0: */
sl@0: EXPORT_C BOOL Emulator::CreateAllDirectories(LPCSTR aPathName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	char path[MAX_PATH];
sl@0: 	strcpy(path, aPathName);
sl@0: 	char* p = path;
sl@0: 	for (;;)
sl@0: 		{
sl@0: 		p = strchr(p, '\\');
sl@0: 		char temp=0;
sl@0: 		if (p)
sl@0: 			{
sl@0: 			temp = *++p;
sl@0: 			*p = '\0';
sl@0: 			}
sl@0: 		DWORD att = ::GetFileAttributesA(path);
sl@0: 		if ((att == -1 || !(att&FILE_ATTRIBUTE_DIRECTORY)) && !::CreateDirectoryA(path, NULL))
sl@0: 			return EFalse;
sl@0: 		if (!p)
sl@0: 			return ETrue;
sl@0: 		*p = temp;
sl@0: 		}
sl@0: 	}
sl@0: #ifdef __VC32__
sl@0: //enable unreachable code warning in VC++
sl@0: #pragma warning (default : 4702)
sl@0: #endif
sl@0: 
sl@0: #ifndef INVALID_FILE_ATTRIBUTES	
sl@0: #define INVALID_FILE_ATTRIBUTES	 ((DWORD)-1)
sl@0: #endif
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 CreateFile API (see http://msdn2.microsoft.com/en-us/library/aa363858.aspx).
sl@0: It also modifies file attributes depending on whether the file is hidden or not.
sl@0: */
sl@0: EXPORT_C HANDLE Emulator::CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,
sl@0: 						   LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDistribution,
sl@0: 						   DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 	HANDLE h;
sl@0: 	BOOL hidden = 0; 
sl@0: 	DWORD att = INVALID_FILE_ATTRIBUTES;
sl@0: 	if(dwCreationDistribution==CREATE_ALWAYS)
sl@0: 		{
sl@0: 		att = ::GetFileAttributes(lpFileName);
sl@0: 		if(att!=INVALID_FILE_ATTRIBUTES && (att&FILE_ATTRIBUTE_HIDDEN))
sl@0: 			{
sl@0: 			hidden = ::SetFileAttributes(lpFileName, (att&~FILE_ATTRIBUTE_HIDDEN));
sl@0: 			}
sl@0: 		}
sl@0: 		
sl@0: 	if (UnicodeHost)
sl@0: 		h = ::CreateFileW(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution,
sl@0: 							dwFlagsAndAttributes,hTemplateFile);
sl@0: 	else
sl@0: 		h = ::CreateFileA(Buf8<MAX_PATH>(lpFileName),dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution,
sl@0: 						dwFlagsAndAttributes,hTemplateFile);
sl@0: 	
sl@0: 	if(hidden && h!=INVALID_HANDLE_VALUE)
sl@0: 		{
sl@0: 		::SetFileAttributes(lpFileName, att);
sl@0: 		}
sl@0: 	return h;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 DeleteFile API (see http://msdn2.microsoft.com/en-us/library/aa363915.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::DeleteFile(LPCWSTR lpFileName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::DeleteFileW(lpFileName);
sl@0: 
sl@0: 	return ::DeleteFileA(Buf8<MAX_PATH>(lpFileName));
sl@0: 	}
sl@0: 
sl@0: LOCAL_C TBool MapFindFileData(LPWIN32_FIND_DATAW lpFindFileData, LPWIN32_FIND_DATAA lpFindNarrow)
sl@0: 	{
sl@0: 	lpFindFileData->dwFileAttributes=lpFindNarrow->dwFileAttributes;
sl@0: 	lpFindFileData->ftCreationTime=lpFindNarrow->ftCreationTime;
sl@0: 	lpFindFileData->ftLastAccessTime=lpFindNarrow->ftLastAccessTime;
sl@0: 	lpFindFileData->ftLastWriteTime=lpFindNarrow->ftLastWriteTime;
sl@0: 	lpFindFileData->nFileSizeHigh=lpFindNarrow->nFileSizeHigh;
sl@0: 	lpFindFileData->nFileSizeLow=lpFindNarrow->nFileSizeLow;
sl@0: 	
sl@0: 	if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cFileName,-1,lpFindFileData->cFileName,MAX_PATH))
sl@0: 		return FALSE;
sl@0: 
sl@0: 	if(lpFindNarrow->cAlternateFileName!=NULL)
sl@0: 		{
sl@0: 		// magic number 14 comes from MS documentation
sl@0: 		if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cAlternateFileName,-1,lpFindFileData->cAlternateFileName,14))
sl@0: 			return FALSE;
sl@0: 		}
sl@0: 	return TRUE;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 FindFirstFile API (see http://msdn2.microsoft.com/en-us/library/aa364418.aspx).	
sl@0: */
sl@0: EXPORT_C HANDLE Emulator::FindFirstFile(LPCWSTR lpFileName,LPWIN32_FIND_DATAW lpFindFileData)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::FindFirstFileW(lpFileName, lpFindFileData);
sl@0: 
sl@0: 	WIN32_FIND_DATAA lpFindNarrow;
sl@0: 	HANDLE h=::FindFirstFileA(Buf8<MAX_PATH>(lpFileName),&lpFindNarrow);
sl@0: 	if(h==INVALID_HANDLE_VALUE)
sl@0: 		return h;
sl@0: 
sl@0: 	if (!MapFindFileData(lpFindFileData, &lpFindNarrow))
sl@0: 		{
sl@0: 		FindClose(h);
sl@0: 		return INVALID_HANDLE_VALUE;
sl@0: 		}
sl@0: 
sl@0: 	return h;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 FindNextFile API (see http://msdn2.microsoft.com/en-us/library/aa364428.aspx).	
sl@0: */
sl@0: EXPORT_C BOOL Emulator::FindNextFile(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::FindNextFileW(hFindFile, lpFindFileData);
sl@0: 
sl@0: 	WIN32_FIND_DATAA lpFindNarrow;
sl@0: 	if(!::FindNextFileA(hFindFile,&lpFindNarrow))
sl@0: 		return FALSE;
sl@0: 
sl@0: 	return MapFindFileData(lpFindFileData, &lpFindNarrow);
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetDiskFreeSpace API (see http://msdn2.microsoft.com/en-us/library/aa364935.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::GetDiskFreeSpace(LPCWSTR lpRootPathName,LPDWORD lpSectorsPerCluster,\
sl@0: 							   LPDWORD lpBytesPerSector,LPDWORD lpNumberOfFreeClusters,\
sl@0: 							   LPDWORD lpTotalNumberOfClusters)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetDiskFreeSpaceW(lpRootPathName,lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters);
sl@0: 
sl@0: 	return ::GetDiskFreeSpaceA(Buf8<MAX_PATH>(lpRootPathName),lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters);
sl@0: 
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa364944.aspx).
sl@0: */
sl@0: EXPORT_C DWORD Emulator::GetFileAttributes(LPCWSTR lpFileName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetFileAttributesW(lpFileName);
sl@0: 
sl@0: 	return ::GetFileAttributesA(Buf8<MAX_PATH>(lpFileName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetModuleHandle API (see http://msdn2.microsoft.com/en-us/library/ms683199.aspx).
sl@0: */
sl@0: EXPORT_C HMODULE Emulator::GetModuleHandle(LPCWSTR lpModuleName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetModuleHandleW(lpModuleName);
sl@0: 
sl@0: 	return ::GetModuleHandleA(Buf8<MAX_PATH>(lpModuleName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetModuleFileName API (see http://msdn2.microsoft.com/en-us/library/ms683197.aspx).
sl@0: */
sl@0: EXPORT_C DWORD Emulator::GetModuleFileName(HMODULE hModule, LPWSTR lpFilename)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetModuleFileNameW(hModule, lpFilename, MAX_PATH);
sl@0: 	char fn[MAX_PATH];
sl@0: 	DWORD r=::GetModuleFileNameA(hModule, fn, MAX_PATH);
sl@0: 	if (r>MAX_PATH||r==0)
sl@0: 		return 0;
sl@0: 	return ConvertToUnicode(fn, lpFilename, MAX_PATH, TRUE);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetTempPath API (see http://msdn2.microsoft.com/en-us/library/Aa364992.aspx).
sl@0: */
sl@0: EXPORT_C DWORD Emulator::GetTempPath(DWORD nBufferLength,LPWSTR lpBuff)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetTempPathW(nBufferLength,lpBuff);
sl@0: 
sl@0: 	char path[MAX_PATH];
sl@0: 	DWORD r=::GetTempPathA(MAX_PATH,path);
sl@0: 	if(r>MAX_PATH||r==0)
sl@0: 		return 0;
sl@0: 	return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetCurrentDirectory API (see http://msdn2.microsoft.com/en-us/library/aa364934.aspx).
sl@0: */
sl@0: EXPORT_C DWORD Emulator::GetCurrentDirectory(DWORD nBufferLength,LPWSTR lpBuff)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetCurrentDirectoryW(nBufferLength,lpBuff);
sl@0: 
sl@0: 	char path[MAX_PATH];
sl@0: 	DWORD r=::GetCurrentDirectoryA(MAX_PATH,path);
sl@0: 	if(r>MAX_PATH||r==0)
sl@0: 		return 0;
sl@0: 	return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetVolumeInformation API (see http://msdn2.microsoft.com/en-us/library/aa364993.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::GetVolumeInformation(LPCWSTR lpRootPathName,LPWSTR lpVolumeNameBuffer,DWORD nVolumeNameSize,
sl@0: 								   LPDWORD lpVolumeSerialNumber,LPDWORD lpMaximumComponentLength,
sl@0: 								   LPDWORD lpFileSystemFlags,LPWSTR,DWORD)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	// lpfileSystemNameBuffer always NULL so no need to convert
sl@0: 	if (UnicodeHost)
sl@0: 		return ::GetVolumeInformationW(lpRootPathName,lpVolumeNameBuffer,nVolumeNameSize,lpVolumeSerialNumber,
sl@0: 										lpMaximumComponentLength,lpFileSystemFlags,NULL,0);
sl@0: 
sl@0: 	char volName[MAX_PATH];
sl@0: 	BOOL res=::GetVolumeInformationA(Buf8<MAX_PATH>(lpRootPathName),volName,MAX_PATH,lpVolumeSerialNumber,
sl@0: 									lpMaximumComponentLength,lpFileSystemFlags,NULL,0);
sl@0: 
sl@0: 	if(res && lpVolumeNameBuffer)
sl@0: 		ConvertToUnicode(volName,lpVolumeNameBuffer,nVolumeNameSize,FALSE);
sl@0: 	return res;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 LoadLibrary API (see http://msdn2.microsoft.com/en-us/library/ms684175.aspx).
sl@0: */
sl@0: EXPORT_C HMODULE Emulator::LoadLibrary(LPCWSTR lpLibFileName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::LoadLibraryW(lpLibFileName);
sl@0: 
sl@0: 	return ::LoadLibraryA(Buf8<MAX_PATH>(lpLibFileName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 FreeLibrary API (see http://msdn2.microsoft.com/en-us/library/ms683152.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::FreeLibrary(HMODULE hLibModule)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 	return ::FreeLibrary(hLibModule);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 MoveFile API (see http://msdn2.microsoft.com/en-us/library/aa365239.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::MoveFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::MoveFileW(lpExistingFileName,lpNewFileName);
sl@0: 
sl@0: 	return ::MoveFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 CopyFile API (see http://msdn2.microsoft.com/en-us/library/aa363851.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::CopyFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,BOOL aFailIfExists)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::CopyFileW(lpExistingFileName,lpNewFileName,aFailIfExists);
sl@0: 
sl@0: 	return ::CopyFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName),aFailIfExists);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 OutputDebugString API (see http://msdn2.microsoft.com/en-us/library/aa363362.aspx).
sl@0: */
sl@0: EXPORT_C VOID Emulator::OutputDebugString(LPCWSTR lpOutputString)
sl@0: 	{
sl@0: 	if (UnicodeHost)
sl@0: 		::OutputDebugStringW(lpOutputString);
sl@0: 	else
sl@0: 		::OutputDebugStringA(Buf8<1024>(lpOutputString));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 RemoveDirectory API (see http://msdn2.microsoft.com/en-us/library/aa365488.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::RemoveDirectory(LPCWSTR lpPathName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::RemoveDirectoryW(lpPathName);
sl@0: 
sl@0: 	return ::RemoveDirectoryA(Buf8<MAX_PATH>(lpPathName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 SetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa365535.aspx).
sl@0: */
sl@0: 
sl@0: EXPORT_C BOOL Emulator::SetFileAttributes(LPCWSTR lpFileName,DWORD dwFileAttributes)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::SetFileAttributesW(lpFileName,dwFileAttributes);
sl@0: 
sl@0: 	return ::SetFileAttributesA(Buf8<MAX_PATH>(lpFileName),dwFileAttributes);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 SetVolumeLabel API (see http://msdn2.microsoft.com/en-us/library/aa365560.aspx).
sl@0: */
sl@0: EXPORT_C BOOL Emulator::SetVolumeLabel(LPCWSTR lpRootPathName,LPCWSTR lpVolumeName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 
sl@0: 	if (UnicodeHost)
sl@0: 		return ::SetVolumeLabelW(lpRootPathName,lpVolumeName);
sl@0: 
sl@0: 	return ::SetVolumeLabelA(Buf8<MAX_PATH>(lpRootPathName),Buf8<MAX_PATH>(lpVolumeName));
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Maps an NT error to an Epoc32 error.
sl@0: 	
sl@0: @return	 Last-error code of the calling thread.
sl@0: */
sl@0: EXPORT_C TInt Emulator::LastError()
sl@0: 
sl@0: // For other error codes look at MSDN "Numerical List of Error Codes"
sl@0: 	{
sl@0: 	switch (GetLastError())
sl@0: 		{
sl@0: 	case ERROR_SUCCESS:				return KErrNone;
sl@0: 	case ERROR_INVALID_DRIVE:		return KErrNotReady;
sl@0: 	case ERROR_INVALID_NAME:
sl@0: 	case ERROR_FILENAME_EXCED_RANGE:
sl@0: 	case ERROR_OPEN_FAILED:			return KErrBadName; 
sl@0: 	case ERROR_INVALID_HANDLE:		return KErrBadHandle;
sl@0: 	case ERROR_NOT_SUPPORTED:
sl@0: 	case ERROR_INVALID_FUNCTION:	return KErrNotSupported;
sl@0: 	case ERROR_SHARING_VIOLATION: 
sl@0: 	case ERROR_ACCESS_DENIED: 
sl@0: 	case ERROR_WRITE_PROTECT:		return KErrAccessDenied;
sl@0: 	case ERROR_LOCK_VIOLATION:		return KErrLocked;
sl@0: 	case ERROR_FILE_NOT_FOUND:
sl@0: 	case ERROR_MOD_NOT_FOUND:		return KErrNotFound;
sl@0: 	case ERROR_DIRECTORY: 
sl@0: 	case ERROR_BAD_PATHNAME:
sl@0: 	case ERROR_PATH_NOT_FOUND:		return KErrPathNotFound; 
sl@0: 	case ERROR_ALREADY_EXISTS:
sl@0: 	case ERROR_FILE_EXISTS:			return KErrAlreadyExists;
sl@0: 	case ERROR_NOT_READY:			return KErrNotReady; 
sl@0: 	case ERROR_UNRECOGNIZED_VOLUME:
sl@0: 	case ERROR_NOT_DOS_DISK:		return KErrUnknown;
sl@0: 	case ERROR_UNRECOGNIZED_MEDIA:
sl@0: 	case ERROR_BAD_EXE_FORMAT:		return KErrCorrupt;
sl@0: 	case ERROR_NO_MORE_FILES:		return KErrEof; 
sl@0: 	case ERROR_DIR_NOT_EMPTY:		return KErrInUse;
sl@0: 	case ERROR_INVALID_USER_BUFFER:
sl@0: 	case ERROR_NOT_ENOUGH_MEMORY:
sl@0: 	case ERROR_INSUFFICIENT_BUFFER:
sl@0: 	case ERROR_OUTOFMEMORY:			return KErrNoMemory;
sl@0: 	case ERROR_DISK_FULL:			return KErrDiskFull;
sl@0: 	case ERROR_INVALID_DATA:
sl@0: 	case ERROR_INVALID_PARAMETER:	return KErrArgument;
sl@0: 	case ERROR_OPERATION_ABORTED:	return KErrCancel;
sl@0: 
sl@0:     default:						return KErrGeneral;
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: This function wraps the Win32 GetProcAddress API (see http://msdn2.microsoft.com/en-us/library/ms683212.aspx).
sl@0: */
sl@0: FARPROC Emulator::GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
sl@0: 	{
sl@0: 	__LOCK_HOST;
sl@0: 	return ::GetProcAddress(hModule, lpProcName);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: 
sl@0: // image file support
sl@0: 
sl@0: // loaded modules
sl@0: 
sl@0: 
sl@0: /**
sl@0: Gets the header format of the file system.
sl@0: 
sl@0: @return		Returns the header format of the file system.	
sl@0: */
sl@0: EXPORT_C const IMAGE_NT_HEADERS32* Emulator::TModule::NtHeader() const
sl@0: 	{
sl@0: 	if (!IsValid())
sl@0: 		return 0;
sl@0: 	const IMAGE_DOS_HEADER* dhead = (const IMAGE_DOS_HEADER*)Translate(0);
sl@0: 	if ( IMAGE_DOS_SIGNATURE != dhead->e_magic )
sl@0: 		return 0;
sl@0: 	if (dhead->e_lfarlc < sizeof(IMAGE_DOS_HEADER))
sl@0: 		return 0;
sl@0: 	const IMAGE_NT_HEADERS32* ntHead = (const IMAGE_NT_HEADERS32*)Translate(dhead->e_lfanew);
sl@0:     if ( ntHead->Signature != IMAGE_NT_SIGNATURE )
sl@0: 		return 0;
sl@0: 	return ntHead;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Constructor which sets the handles of loaded module to the specified module.
sl@0: 
sl@0: @param aModuleName		Holds the name of the module to be loaded.
sl@0: */
sl@0: EXPORT_C Emulator::TModule::TModule(LPCSTR aModuleName)
sl@0: 	: iModule(GetModuleHandleA(aModuleName)), iBase(iModule)
sl@0: 	{
sl@0: 	if (!NtHeader())
sl@0: 		{
sl@0: 		iModule = 0;
sl@0: 		iBase = 0;
sl@0: 		}
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Gets the pointer of the section header if the header name matches with the buffer aSection[].
sl@0: 		
sl@0: @param aSection[]	A buffer of type BYTE.		
sl@0: 
sl@0: @return 	Returns the pointer to section header of the file system.
sl@0: */
sl@0: EXPORT_C const IMAGE_SECTION_HEADER* Emulator::TModule::SectionHeader(const BYTE aSection[]) const
sl@0: 	{
sl@0: 	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0: 	if (!ntHead)
sl@0: 		return 0;
sl@0: 
sl@0: 	const IMAGE_SECTION_HEADER* imgHead = (const IMAGE_SECTION_HEADER*)((TUint8*)&ntHead->OptionalHeader + ntHead->FileHeader.SizeOfOptionalHeader);
sl@0: 	const IMAGE_SECTION_HEADER* end = imgHead + ntHead->FileHeader.NumberOfSections;
sl@0: 	for (; imgHead < end; ++imgHead)
sl@0: 		{
sl@0: 		if (memcmp(imgHead->Name, aSection, IMAGE_SIZEOF_SHORT_NAME)==0)
sl@0: 			return imgHead;
sl@0: 		}
sl@0: 	return 0;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Points to the first byte or first page of the loaded module or mapped file image.
sl@0: 		
sl@0: @param aSection[]	A buffer of type BYTE.
sl@0: 		
sl@0: @return TAny		Returns first byte of the loaded module or first page of the mapped file image.
sl@0: */
sl@0: EXPORT_C const TAny* Emulator::TModule::Section(const BYTE aSection[]) const
sl@0: 	{
sl@0: 	const IMAGE_SECTION_HEADER* imgHead = SectionHeader(aSection);
sl@0: 	if (imgHead)
sl@0: 		return Translate(IsLoaded() ? imgHead->VirtualAddress : imgHead->PointerToRawData);
sl@0: 	return 0;
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Go through the section headers looking for UID section.
sl@0: 	
sl@0: @param aType	Contains a UID type.
sl@0: */
sl@0: EXPORT_C void Emulator::TModule::GetUids(TUidType& aType) const
sl@0: 	{
sl@0: 	const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian);
sl@0: 	if (hdr)
sl@0: 		aType = *(const TUidType*)&hdr->iUids[0];
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Goes through section headers and gets the information of file system.
sl@0: 
sl@0: @param aInfo	Contains the information of the file system.
sl@0: */
sl@0: EXPORT_C void Emulator::TModule::GetInfo(TProcessCreateInfo& aInfo) const
sl@0: 	{
sl@0: 	aInfo.iExceptionDescriptor = 0;
sl@0: 	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0: 	if (ntHead)
sl@0: 		{
sl@0: 		const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian);
sl@0: 		if (hdr)
sl@0: 			{
sl@0: 			aInfo.iUids = *(const TUidType*)&hdr->iUids[0];
sl@0: 			TBool isExe = (hdr->iUids[0].iUid==KExecutableImageUidValue);
sl@0: 			TBool data_section_present = (Section(KWin32SectionName_EpocData)!=NULL);
sl@0: 			TBool data = hdr->iFlags & KEmulatorImageFlagAllowDllData;
sl@0: 			if (data_section_present && isExe)
sl@0: 				data = ETrue;
sl@0: 			aInfo.iBssSize=data?1:0;
sl@0: 			aInfo.iDataSize=0;
sl@0: 			aInfo.iTotalDataSize=aInfo.iBssSize;
sl@0: 			aInfo.iDepCount = 0;
sl@0: 			aInfo.iHeapSizeMin = ntHead->OptionalHeader.SizeOfHeapCommit;
sl@0: 			aInfo.iHeapSizeMax = ntHead->OptionalHeader.SizeOfHeapReserve;
sl@0: 			aInfo.iStackSize = 0x1000;
sl@0: 			aInfo.iPriority = hdr->iPriority;
sl@0: 			aInfo.iHandle = NULL;
sl@0: 			aInfo.iS = hdr->iS;
sl@0: 			aInfo.iModuleVersion = hdr->iModuleVersion;
sl@0: 			if (ntHead->FileHeader.Characteristics & IMAGE_FILE_DLL)
sl@0: 				aInfo.iAttr |= ECodeSegAttHDll;
sl@0: 			}
sl@0: 		}
sl@0: 	GetUids(aInfo.iUids);
sl@0: 	}
sl@0: 
sl@0: 
sl@0: /**
sl@0: Finds the import section from the data directory. This relies on the module being loaded.
sl@0: 		
sl@0: @return 	Returns the imported executable.
sl@0: */
sl@0: EXPORT_C const IMAGE_IMPORT_DESCRIPTOR* Emulator::TModule::Imports() const
sl@0: 	{
sl@0: 	if (!IsLoaded())
sl@0: 		return NULL;
sl@0: 	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0: 	if (!ntHead)
sl@0: 		return NULL;
sl@0: 	DWORD va = ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
sl@0: 	if (!va)
sl@0: 		return NULL;
sl@0: 	return (const IMAGE_IMPORT_DESCRIPTOR*)Translate(va);
sl@0: 	}
sl@0: 
sl@0: // modules in the file system
sl@0: EXPORT_C TInt Emulator::RImageFile::Open(LPCTSTR aImageFile)
sl@0: 	{
sl@0: 	Buf8<MAX_PATH>   nameBuf(aImageFile);
sl@0: 	char *pName = (char *)strrchr(LPCSTR(nameBuf), '\\');
sl@0: 	pName ? ++pName : pName = (char *)LPCSTR(nameBuf);
sl@0: 
sl@0: 	__LOCK_HOST;
sl@0: 	iMapping = OpenFileMapping(FILE_MAP_READ, FALSE, (LPCTSTR)pName);
sl@0: 	if (!iMapping)
sl@0: 		{
sl@0: 		if (pName == (char *)LPCSTR(nameBuf))
sl@0: 			iModule = Emulator::GetModuleHandle(aImageFile);
sl@0: 		if (iModule)
sl@0: 			iBase = iModule;
sl@0: 		else
sl@0: 			{
sl@0: 			// need to map the file instead
sl@0: 			HANDLE file = Emulator::CreateFile(aImageFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
sl@0: 			if (file == INVALID_HANDLE_VALUE)
sl@0: 				return LastError();
sl@0: 			iMapping = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, pName);
sl@0: 			CloseHandle(file);
sl@0: 			}
sl@0: 		}
sl@0: 	if (!iModule)
sl@0: 		{
sl@0: 		if (!iMapping)
sl@0: 			return LastError();
sl@0: 
sl@0: 		iBase = MapViewOfFile(iMapping, FILE_MAP_READ, 0, 0, 0);
sl@0: 
sl@0: 		if (!iBase)
sl@0: 			{
sl@0: 			CloseHandle(iMapping);
sl@0: 			iMapping = 0;
sl@0: 			return LastError();
sl@0: 			}
sl@0: 		}
sl@0: 
sl@0: 	if (!NtHeader())
sl@0: 		{
sl@0: 		Close();
sl@0: 		return KErrNotSupported;
sl@0: 		}
sl@0: 	return KErrNone;
sl@0: 	}
sl@0: 
sl@0: EXPORT_C void Emulator::RImageFile::Close()
sl@0: 	{
sl@0: 	if (iMapping)
sl@0: 		{
sl@0: 		UnmapViewOfFile(iBase);
sl@0: 		CloseHandle(iMapping);
sl@0: 		iMapping = 0;
sl@0: 		}
sl@0: 	iBase = 0;
sl@0: 	iModule = 0;
sl@0: 	}