os/kernelhwsrv/kernel/eka/euser/epoc/win32/emulator.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#define _CRTIMP			// we want to use the static runtime library
sl@0
    17
#include <e32cmn.h>
sl@0
    18
#include <e32cmn_private.h>
sl@0
    19
#include <string.h>
sl@0
    20
#include <emulator.h>
sl@0
    21
#include <e32ldr.h>
sl@0
    22
#include <e32ldr_private.h>
sl@0
    23
#include <e32uid.h>
sl@0
    24
sl@0
    25
#pragma data_seg(".data2")
sl@0
    26
#ifdef __VC32__
sl@0
    27
#pragma bss_seg(".data2")
sl@0
    28
#endif
sl@0
    29
static TBool UnicodeHost;
sl@0
    30
static Emulator::SInit Data;
sl@0
    31
#pragma data_seg()
sl@0
    32
#ifdef __VC32__
sl@0
    33
#pragma bss_seg()
sl@0
    34
#endif
sl@0
    35
sl@0
    36
/**	
sl@0
    37
Initializes a module and prepares it to handle requests.
sl@0
    38
sl@0
    39
@param aInit	An object of the structure SInit.
sl@0
    40
sl@0
    41
@see SInit.
sl@0
    42
*/
sl@0
    43
EXPORT_C void Emulator::Init(const Emulator::SInit& aInit)
sl@0
    44
	{
sl@0
    45
	UnicodeHost = (GetVersion() & 0x80000000) ? FALSE : TRUE;
sl@0
    46
	Data = aInit;
sl@0
    47
	if (Data.iCodePage == 0)
sl@0
    48
		Data.iCodePage = CP_ACP;
sl@0
    49
	}
sl@0
    50
sl@0
    51
LOCAL_C DWORD UStringLength(LPCSTR lpMultiByteStr)
sl@0
    52
//
sl@0
    53
// returns length of unicode string generated - assumes null terminated string
sl@0
    54
//
sl@0
    55
	{
sl@0
    56
	return MultiByteToWideChar(Data.iCodePage,0,lpMultiByteStr,-1,0,0);
sl@0
    57
	}
sl@0
    58
sl@0
    59
sl@0
    60
LOCAL_C DWORD ConvertToUnicode(LPCSTR aNarrow,LPWSTR aUnicode,DWORD aLength,BOOL aCharsRequired)
sl@0
    61
//
sl@0
    62
// Converts narrow string to unicode string
sl@0
    63
//
sl@0
    64
	{
sl@0
    65
	DWORD uniLength=UStringLength(aNarrow);
sl@0
    66
	if(uniLength>aLength || uniLength==0)
sl@0
    67
		return aCharsRequired ? uniLength : 0;
sl@0
    68
	uniLength=MultiByteToWideChar(Data.iCodePage,0,aNarrow,-1,aUnicode,aLength);
sl@0
    69
	// return number of characters excluding the null terminator
sl@0
    70
	return uniLength ? uniLength-1 : 0;
sl@0
    71
	}
sl@0
    72
sl@0
    73
sl@0
    74
LOCAL_C DWORD ConvertToNarrow(LPCWSTR aUnicode,LPSTR aNarrow,DWORD aLength)
sl@0
    75
//
sl@0
    76
// Converts unicode string to narrow string
sl@0
    77
//
sl@0
    78
	{
sl@0
    79
	return WideCharToMultiByte(Data.iCodePage,0,aUnicode,-1,aNarrow,aLength,"@",NULL);
sl@0
    80
	}
sl@0
    81
sl@0
    82
template <TUint S>
sl@0
    83
struct Buf8
sl@0
    84
	{
sl@0
    85
public:
sl@0
    86
	Buf8(LPCWSTR aUnicode);
sl@0
    87
	inline operator LPCSTR() const
sl@0
    88
		{return iPtr;}
sl@0
    89
private:
sl@0
    90
	const char* iPtr;
sl@0
    91
	char iBuf[S];
sl@0
    92
	};
sl@0
    93
sl@0
    94
template <TUint S>
sl@0
    95
Buf8<S>::Buf8(LPCWSTR aUnicode)
sl@0
    96
	{
sl@0
    97
	if (aUnicode)
sl@0
    98
		{
sl@0
    99
		iPtr = iBuf;
sl@0
   100
		ConvertToNarrow(aUnicode,iBuf,S);
sl@0
   101
		}
sl@0
   102
	else
sl@0
   103
		{
sl@0
   104
		iPtr = NULL;
sl@0
   105
		}
sl@0
   106
	}
sl@0
   107
sl@0
   108
sl@0
   109
sl@0
   110
/**	
sl@0
   111
Acquires the global lock for host interaction.
sl@0
   112
*/
sl@0
   113
EXPORT_C void Emulator::Lock()
sl@0
   114
	{
sl@0
   115
	Data.iLock();
sl@0
   116
	}
sl@0
   117
sl@0
   118
/**
sl@0
   119
Releases the global lock for host interaction.
sl@0
   120
This may overwrite the error code for this thread and that is not the behaviour expected, 
sl@0
   121
so save and restore it.
sl@0
   122
*/
sl@0
   123
EXPORT_C void Emulator::Unlock()
sl@0
   124
	{
sl@0
   125
	DWORD error=GetLastError();
sl@0
   126
	Data.iUnlock();
sl@0
   127
	SetLastError(error);
sl@0
   128
	}
sl@0
   129
sl@0
   130
/**
sl@0
   131
Takes the current thread out of the emulator scheduling model.
sl@0
   132
*/
sl@0
   133
EXPORT_C void Emulator::Escape()
sl@0
   134
	{
sl@0
   135
	Data.iEscape();
sl@0
   136
	}
sl@0
   137
sl@0
   138
/**
sl@0
   139
Returns the calling thread into the emulator scheduling model.
sl@0
   140
This may overwrite the error code for this thread and that is not the behaviour expected, 
sl@0
   141
so save and restore it.
sl@0
   142
*/
sl@0
   143
EXPORT_C void Emulator::Reenter()
sl@0
   144
	{
sl@0
   145
	DWORD error=GetLastError();
sl@0
   146
	Data.iReenter();
sl@0
   147
	SetLastError(error);
sl@0
   148
	}
sl@0
   149
sl@0
   150
sl@0
   151
/**
sl@0
   152
Jumps to the NThread Win32 SEH exception handler.
sl@0
   153
sl@0
   154
@param aException	Describes an exception.
sl@0
   155
@param aContext		Describes the context.
sl@0
   156
	
sl@0
   157
@return   A handler to handle the exception occurred
sl@0
   158
*/
sl@0
   159
EXPORT_C DWORD Emulator::Win32SEHException(EXCEPTION_RECORD* aException, CONTEXT* aContext)
sl@0
   160
	{
sl@0
   161
	return Data.iException(aException, aContext);
sl@0
   162
	}
sl@0
   163
sl@0
   164
sl@0
   165
/**
sl@0
   166
This function wraps the Win32 CreateDirectory API (see http://msdn2.microsoft.com/en-us/library/aa363855.aspx).
sl@0
   167
*/  
sl@0
   168
EXPORT_C BOOL Emulator::CreateDirectory(LPCWSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes)
sl@0
   169
	{
sl@0
   170
	__LOCK_HOST;
sl@0
   171
sl@0
   172
	if (UnicodeHost)
sl@0
   173
		return ::CreateDirectoryW(lpPathName, lpSecurityAttributes);
sl@0
   174
sl@0
   175
	return ::CreateDirectoryA(Buf8<MAX_PATH>(lpPathName),lpSecurityAttributes);
sl@0
   176
	}
sl@0
   177
sl@0
   178
#ifdef __VC32__
sl@0
   179
//disable unreachable code warning in VC++
sl@0
   180
#pragma warning (disable : 4702)
sl@0
   181
#endif
sl@0
   182
sl@0
   183
sl@0
   184
/**
sl@0
   185
Recursively ensures that the full directory exists.
sl@0
   186
	
sl@0
   187
@param aPathName  	Provides the path name.
sl@0
   188
sl@0
   189
@return  TRUE, if the function succeeds
sl@0
   190
		 FALSE, if the function fails
sl@0
   191
*/
sl@0
   192
EXPORT_C BOOL Emulator::CreateAllDirectories(LPCSTR aPathName)
sl@0
   193
	{
sl@0
   194
	__LOCK_HOST;
sl@0
   195
sl@0
   196
	char path[MAX_PATH];
sl@0
   197
	strcpy(path, aPathName);
sl@0
   198
	char* p = path;
sl@0
   199
	for (;;)
sl@0
   200
		{
sl@0
   201
		p = strchr(p, '\\');
sl@0
   202
		char temp=0;
sl@0
   203
		if (p)
sl@0
   204
			{
sl@0
   205
			temp = *++p;
sl@0
   206
			*p = '\0';
sl@0
   207
			}
sl@0
   208
		DWORD att = ::GetFileAttributesA(path);
sl@0
   209
		if ((att == -1 || !(att&FILE_ATTRIBUTE_DIRECTORY)) && !::CreateDirectoryA(path, NULL))
sl@0
   210
			return EFalse;
sl@0
   211
		if (!p)
sl@0
   212
			return ETrue;
sl@0
   213
		*p = temp;
sl@0
   214
		}
sl@0
   215
	}
sl@0
   216
#ifdef __VC32__
sl@0
   217
//enable unreachable code warning in VC++
sl@0
   218
#pragma warning (default : 4702)
sl@0
   219
#endif
sl@0
   220
sl@0
   221
#ifndef INVALID_FILE_ATTRIBUTES	
sl@0
   222
#define INVALID_FILE_ATTRIBUTES	 ((DWORD)-1)
sl@0
   223
#endif
sl@0
   224
sl@0
   225
sl@0
   226
/**
sl@0
   227
This function wraps the Win32 CreateFile API (see http://msdn2.microsoft.com/en-us/library/aa363858.aspx).
sl@0
   228
It also modifies file attributes depending on whether the file is hidden or not.
sl@0
   229
*/
sl@0
   230
EXPORT_C HANDLE Emulator::CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess,DWORD dwShareMode,
sl@0
   231
						   LPSECURITY_ATTRIBUTES lpSecurityAttributes,DWORD dwCreationDistribution,
sl@0
   232
						   DWORD dwFlagsAndAttributes,HANDLE hTemplateFile)
sl@0
   233
	{
sl@0
   234
	__LOCK_HOST;
sl@0
   235
	HANDLE h;
sl@0
   236
	BOOL hidden = 0; 
sl@0
   237
	DWORD att = INVALID_FILE_ATTRIBUTES;
sl@0
   238
	if(dwCreationDistribution==CREATE_ALWAYS)
sl@0
   239
		{
sl@0
   240
		att = ::GetFileAttributes(lpFileName);
sl@0
   241
		if(att!=INVALID_FILE_ATTRIBUTES && (att&FILE_ATTRIBUTE_HIDDEN))
sl@0
   242
			{
sl@0
   243
			hidden = ::SetFileAttributes(lpFileName, (att&~FILE_ATTRIBUTE_HIDDEN));
sl@0
   244
			}
sl@0
   245
		}
sl@0
   246
		
sl@0
   247
	if (UnicodeHost)
sl@0
   248
		h = ::CreateFileW(lpFileName,dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution,
sl@0
   249
							dwFlagsAndAttributes,hTemplateFile);
sl@0
   250
	else
sl@0
   251
		h = ::CreateFileA(Buf8<MAX_PATH>(lpFileName),dwDesiredAccess,dwShareMode,lpSecurityAttributes,dwCreationDistribution,
sl@0
   252
						dwFlagsAndAttributes,hTemplateFile);
sl@0
   253
	
sl@0
   254
	if(hidden && h!=INVALID_HANDLE_VALUE)
sl@0
   255
		{
sl@0
   256
		::SetFileAttributes(lpFileName, att);
sl@0
   257
		}
sl@0
   258
	return h;
sl@0
   259
	}
sl@0
   260
sl@0
   261
sl@0
   262
/**
sl@0
   263
This function wraps the Win32 DeleteFile API (see http://msdn2.microsoft.com/en-us/library/aa363915.aspx).
sl@0
   264
*/
sl@0
   265
EXPORT_C BOOL Emulator::DeleteFile(LPCWSTR lpFileName)
sl@0
   266
	{
sl@0
   267
	__LOCK_HOST;
sl@0
   268
sl@0
   269
	if (UnicodeHost)
sl@0
   270
		return ::DeleteFileW(lpFileName);
sl@0
   271
sl@0
   272
	return ::DeleteFileA(Buf8<MAX_PATH>(lpFileName));
sl@0
   273
	}
sl@0
   274
sl@0
   275
LOCAL_C TBool MapFindFileData(LPWIN32_FIND_DATAW lpFindFileData, LPWIN32_FIND_DATAA lpFindNarrow)
sl@0
   276
	{
sl@0
   277
	lpFindFileData->dwFileAttributes=lpFindNarrow->dwFileAttributes;
sl@0
   278
	lpFindFileData->ftCreationTime=lpFindNarrow->ftCreationTime;
sl@0
   279
	lpFindFileData->ftLastAccessTime=lpFindNarrow->ftLastAccessTime;
sl@0
   280
	lpFindFileData->ftLastWriteTime=lpFindNarrow->ftLastWriteTime;
sl@0
   281
	lpFindFileData->nFileSizeHigh=lpFindNarrow->nFileSizeHigh;
sl@0
   282
	lpFindFileData->nFileSizeLow=lpFindNarrow->nFileSizeLow;
sl@0
   283
	
sl@0
   284
	if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cFileName,-1,lpFindFileData->cFileName,MAX_PATH))
sl@0
   285
		return FALSE;
sl@0
   286
sl@0
   287
	if(lpFindNarrow->cAlternateFileName!=NULL)
sl@0
   288
		{
sl@0
   289
		// magic number 14 comes from MS documentation
sl@0
   290
		if(!MultiByteToWideChar(Data.iCodePage,0,lpFindNarrow->cAlternateFileName,-1,lpFindFileData->cAlternateFileName,14))
sl@0
   291
			return FALSE;
sl@0
   292
		}
sl@0
   293
	return TRUE;
sl@0
   294
	}
sl@0
   295
sl@0
   296
sl@0
   297
/**
sl@0
   298
This function wraps the Win32 FindFirstFile API (see http://msdn2.microsoft.com/en-us/library/aa364418.aspx).	
sl@0
   299
*/
sl@0
   300
EXPORT_C HANDLE Emulator::FindFirstFile(LPCWSTR lpFileName,LPWIN32_FIND_DATAW lpFindFileData)
sl@0
   301
	{
sl@0
   302
	__LOCK_HOST;
sl@0
   303
sl@0
   304
	if (UnicodeHost)
sl@0
   305
		return ::FindFirstFileW(lpFileName, lpFindFileData);
sl@0
   306
sl@0
   307
	WIN32_FIND_DATAA lpFindNarrow;
sl@0
   308
	HANDLE h=::FindFirstFileA(Buf8<MAX_PATH>(lpFileName),&lpFindNarrow);
sl@0
   309
	if(h==INVALID_HANDLE_VALUE)
sl@0
   310
		return h;
sl@0
   311
sl@0
   312
	if (!MapFindFileData(lpFindFileData, &lpFindNarrow))
sl@0
   313
		{
sl@0
   314
		FindClose(h);
sl@0
   315
		return INVALID_HANDLE_VALUE;
sl@0
   316
		}
sl@0
   317
sl@0
   318
	return h;
sl@0
   319
	}
sl@0
   320
sl@0
   321
sl@0
   322
/**
sl@0
   323
This function wraps the Win32 FindNextFile API (see http://msdn2.microsoft.com/en-us/library/aa364428.aspx).	
sl@0
   324
*/
sl@0
   325
EXPORT_C BOOL Emulator::FindNextFile(HANDLE hFindFile,LPWIN32_FIND_DATAW lpFindFileData)
sl@0
   326
	{
sl@0
   327
	__LOCK_HOST;
sl@0
   328
sl@0
   329
	if (UnicodeHost)
sl@0
   330
		return ::FindNextFileW(hFindFile, lpFindFileData);
sl@0
   331
sl@0
   332
	WIN32_FIND_DATAA lpFindNarrow;
sl@0
   333
	if(!::FindNextFileA(hFindFile,&lpFindNarrow))
sl@0
   334
		return FALSE;
sl@0
   335
sl@0
   336
	return MapFindFileData(lpFindFileData, &lpFindNarrow);
sl@0
   337
	}
sl@0
   338
sl@0
   339
/**
sl@0
   340
This function wraps the Win32 GetDiskFreeSpace API (see http://msdn2.microsoft.com/en-us/library/aa364935.aspx).
sl@0
   341
*/
sl@0
   342
EXPORT_C BOOL Emulator::GetDiskFreeSpace(LPCWSTR lpRootPathName,LPDWORD lpSectorsPerCluster,\
sl@0
   343
							   LPDWORD lpBytesPerSector,LPDWORD lpNumberOfFreeClusters,\
sl@0
   344
							   LPDWORD lpTotalNumberOfClusters)
sl@0
   345
	{
sl@0
   346
	__LOCK_HOST;
sl@0
   347
sl@0
   348
	if (UnicodeHost)
sl@0
   349
		return ::GetDiskFreeSpaceW(lpRootPathName,lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters);
sl@0
   350
sl@0
   351
	return ::GetDiskFreeSpaceA(Buf8<MAX_PATH>(lpRootPathName),lpSectorsPerCluster,lpBytesPerSector,lpNumberOfFreeClusters,lpTotalNumberOfClusters);
sl@0
   352
sl@0
   353
	}
sl@0
   354
sl@0
   355
sl@0
   356
/**
sl@0
   357
This function wraps the Win32 GetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa364944.aspx).
sl@0
   358
*/
sl@0
   359
EXPORT_C DWORD Emulator::GetFileAttributes(LPCWSTR lpFileName)
sl@0
   360
	{
sl@0
   361
	__LOCK_HOST;
sl@0
   362
sl@0
   363
	if (UnicodeHost)
sl@0
   364
		return ::GetFileAttributesW(lpFileName);
sl@0
   365
sl@0
   366
	return ::GetFileAttributesA(Buf8<MAX_PATH>(lpFileName));
sl@0
   367
	}
sl@0
   368
sl@0
   369
sl@0
   370
/**
sl@0
   371
This function wraps the Win32 GetModuleHandle API (see http://msdn2.microsoft.com/en-us/library/ms683199.aspx).
sl@0
   372
*/
sl@0
   373
EXPORT_C HMODULE Emulator::GetModuleHandle(LPCWSTR lpModuleName)
sl@0
   374
	{
sl@0
   375
	__LOCK_HOST;
sl@0
   376
sl@0
   377
	if (UnicodeHost)
sl@0
   378
		return ::GetModuleHandleW(lpModuleName);
sl@0
   379
sl@0
   380
	return ::GetModuleHandleA(Buf8<MAX_PATH>(lpModuleName));
sl@0
   381
	}
sl@0
   382
sl@0
   383
sl@0
   384
/**
sl@0
   385
This function wraps the Win32 GetModuleFileName API (see http://msdn2.microsoft.com/en-us/library/ms683197.aspx).
sl@0
   386
*/
sl@0
   387
EXPORT_C DWORD Emulator::GetModuleFileName(HMODULE hModule, LPWSTR lpFilename)
sl@0
   388
	{
sl@0
   389
	__LOCK_HOST;
sl@0
   390
	if (UnicodeHost)
sl@0
   391
		return ::GetModuleFileNameW(hModule, lpFilename, MAX_PATH);
sl@0
   392
	char fn[MAX_PATH];
sl@0
   393
	DWORD r=::GetModuleFileNameA(hModule, fn, MAX_PATH);
sl@0
   394
	if (r>MAX_PATH||r==0)
sl@0
   395
		return 0;
sl@0
   396
	return ConvertToUnicode(fn, lpFilename, MAX_PATH, TRUE);
sl@0
   397
	}
sl@0
   398
sl@0
   399
sl@0
   400
/**
sl@0
   401
This function wraps the Win32 GetTempPath API (see http://msdn2.microsoft.com/en-us/library/Aa364992.aspx).
sl@0
   402
*/
sl@0
   403
EXPORT_C DWORD Emulator::GetTempPath(DWORD nBufferLength,LPWSTR lpBuff)
sl@0
   404
	{
sl@0
   405
	__LOCK_HOST;
sl@0
   406
sl@0
   407
	if (UnicodeHost)
sl@0
   408
		return ::GetTempPathW(nBufferLength,lpBuff);
sl@0
   409
sl@0
   410
	char path[MAX_PATH];
sl@0
   411
	DWORD r=::GetTempPathA(MAX_PATH,path);
sl@0
   412
	if(r>MAX_PATH||r==0)
sl@0
   413
		return 0;
sl@0
   414
	return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE);
sl@0
   415
	}
sl@0
   416
sl@0
   417
sl@0
   418
/**
sl@0
   419
This function wraps the Win32 GetCurrentDirectory API (see http://msdn2.microsoft.com/en-us/library/aa364934.aspx).
sl@0
   420
*/
sl@0
   421
EXPORT_C DWORD Emulator::GetCurrentDirectory(DWORD nBufferLength,LPWSTR lpBuff)
sl@0
   422
	{
sl@0
   423
	__LOCK_HOST;
sl@0
   424
sl@0
   425
	if (UnicodeHost)
sl@0
   426
		return ::GetCurrentDirectoryW(nBufferLength,lpBuff);
sl@0
   427
sl@0
   428
	char path[MAX_PATH];
sl@0
   429
	DWORD r=::GetCurrentDirectoryA(MAX_PATH,path);
sl@0
   430
	if(r>MAX_PATH||r==0)
sl@0
   431
		return 0;
sl@0
   432
	return ConvertToUnicode(path,lpBuff,nBufferLength,TRUE);
sl@0
   433
	}
sl@0
   434
sl@0
   435
sl@0
   436
/**
sl@0
   437
This function wraps the Win32 GetVolumeInformation API (see http://msdn2.microsoft.com/en-us/library/aa364993.aspx).
sl@0
   438
*/
sl@0
   439
EXPORT_C BOOL Emulator::GetVolumeInformation(LPCWSTR lpRootPathName,LPWSTR lpVolumeNameBuffer,DWORD nVolumeNameSize,
sl@0
   440
								   LPDWORD lpVolumeSerialNumber,LPDWORD lpMaximumComponentLength,
sl@0
   441
								   LPDWORD lpFileSystemFlags,LPWSTR,DWORD)
sl@0
   442
	{
sl@0
   443
	__LOCK_HOST;
sl@0
   444
sl@0
   445
	// lpfileSystemNameBuffer always NULL so no need to convert
sl@0
   446
	if (UnicodeHost)
sl@0
   447
		return ::GetVolumeInformationW(lpRootPathName,lpVolumeNameBuffer,nVolumeNameSize,lpVolumeSerialNumber,
sl@0
   448
										lpMaximumComponentLength,lpFileSystemFlags,NULL,0);
sl@0
   449
sl@0
   450
	char volName[MAX_PATH];
sl@0
   451
	BOOL res=::GetVolumeInformationA(Buf8<MAX_PATH>(lpRootPathName),volName,MAX_PATH,lpVolumeSerialNumber,
sl@0
   452
									lpMaximumComponentLength,lpFileSystemFlags,NULL,0);
sl@0
   453
sl@0
   454
	if(res && lpVolumeNameBuffer)
sl@0
   455
		ConvertToUnicode(volName,lpVolumeNameBuffer,nVolumeNameSize,FALSE);
sl@0
   456
	return res;
sl@0
   457
	}
sl@0
   458
sl@0
   459
sl@0
   460
/**
sl@0
   461
This function wraps the Win32 LoadLibrary API (see http://msdn2.microsoft.com/en-us/library/ms684175.aspx).
sl@0
   462
*/
sl@0
   463
EXPORT_C HMODULE Emulator::LoadLibrary(LPCWSTR lpLibFileName)
sl@0
   464
	{
sl@0
   465
	__LOCK_HOST;
sl@0
   466
sl@0
   467
	if (UnicodeHost)
sl@0
   468
		return ::LoadLibraryW(lpLibFileName);
sl@0
   469
sl@0
   470
	return ::LoadLibraryA(Buf8<MAX_PATH>(lpLibFileName));
sl@0
   471
	}
sl@0
   472
sl@0
   473
sl@0
   474
/**
sl@0
   475
This function wraps the Win32 FreeLibrary API (see http://msdn2.microsoft.com/en-us/library/ms683152.aspx).
sl@0
   476
*/
sl@0
   477
EXPORT_C BOOL Emulator::FreeLibrary(HMODULE hLibModule)
sl@0
   478
	{
sl@0
   479
	__LOCK_HOST;
sl@0
   480
	return ::FreeLibrary(hLibModule);
sl@0
   481
	}
sl@0
   482
sl@0
   483
sl@0
   484
sl@0
   485
/**
sl@0
   486
This function wraps the Win32 MoveFile API (see http://msdn2.microsoft.com/en-us/library/aa365239.aspx).
sl@0
   487
*/
sl@0
   488
EXPORT_C BOOL Emulator::MoveFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName)
sl@0
   489
	{
sl@0
   490
	__LOCK_HOST;
sl@0
   491
sl@0
   492
	if (UnicodeHost)
sl@0
   493
		return ::MoveFileW(lpExistingFileName,lpNewFileName);
sl@0
   494
sl@0
   495
	return ::MoveFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName));
sl@0
   496
	}
sl@0
   497
sl@0
   498
sl@0
   499
/**
sl@0
   500
This function wraps the Win32 CopyFile API (see http://msdn2.microsoft.com/en-us/library/aa363851.aspx).
sl@0
   501
*/
sl@0
   502
EXPORT_C BOOL Emulator::CopyFile(LPCWSTR lpExistingFileName,LPCWSTR lpNewFileName,BOOL aFailIfExists)
sl@0
   503
	{
sl@0
   504
	__LOCK_HOST;
sl@0
   505
sl@0
   506
	if (UnicodeHost)
sl@0
   507
		return ::CopyFileW(lpExistingFileName,lpNewFileName,aFailIfExists);
sl@0
   508
sl@0
   509
	return ::CopyFileA(Buf8<MAX_PATH>(lpExistingFileName),Buf8<MAX_PATH>(lpNewFileName),aFailIfExists);
sl@0
   510
	}
sl@0
   511
sl@0
   512
sl@0
   513
/**
sl@0
   514
This function wraps the Win32 OutputDebugString API (see http://msdn2.microsoft.com/en-us/library/aa363362.aspx).
sl@0
   515
*/
sl@0
   516
EXPORT_C VOID Emulator::OutputDebugString(LPCWSTR lpOutputString)
sl@0
   517
	{
sl@0
   518
	if (UnicodeHost)
sl@0
   519
		::OutputDebugStringW(lpOutputString);
sl@0
   520
	else
sl@0
   521
		::OutputDebugStringA(Buf8<1024>(lpOutputString));
sl@0
   522
	}
sl@0
   523
sl@0
   524
sl@0
   525
sl@0
   526
/**
sl@0
   527
This function wraps the Win32 RemoveDirectory API (see http://msdn2.microsoft.com/en-us/library/aa365488.aspx).
sl@0
   528
*/
sl@0
   529
EXPORT_C BOOL Emulator::RemoveDirectory(LPCWSTR lpPathName)
sl@0
   530
	{
sl@0
   531
	__LOCK_HOST;
sl@0
   532
sl@0
   533
	if (UnicodeHost)
sl@0
   534
		return ::RemoveDirectoryW(lpPathName);
sl@0
   535
sl@0
   536
	return ::RemoveDirectoryA(Buf8<MAX_PATH>(lpPathName));
sl@0
   537
	}
sl@0
   538
sl@0
   539
sl@0
   540
/**
sl@0
   541
This function wraps the Win32 SetFileAttributes API (see http://msdn2.microsoft.com/en-us/library/aa365535.aspx).
sl@0
   542
*/
sl@0
   543
sl@0
   544
EXPORT_C BOOL Emulator::SetFileAttributes(LPCWSTR lpFileName,DWORD dwFileAttributes)
sl@0
   545
	{
sl@0
   546
	__LOCK_HOST;
sl@0
   547
sl@0
   548
	if (UnicodeHost)
sl@0
   549
		return ::SetFileAttributesW(lpFileName,dwFileAttributes);
sl@0
   550
sl@0
   551
	return ::SetFileAttributesA(Buf8<MAX_PATH>(lpFileName),dwFileAttributes);
sl@0
   552
	}
sl@0
   553
sl@0
   554
sl@0
   555
sl@0
   556
/**
sl@0
   557
This function wraps the Win32 SetVolumeLabel API (see http://msdn2.microsoft.com/en-us/library/aa365560.aspx).
sl@0
   558
*/
sl@0
   559
EXPORT_C BOOL Emulator::SetVolumeLabel(LPCWSTR lpRootPathName,LPCWSTR lpVolumeName)
sl@0
   560
	{
sl@0
   561
	__LOCK_HOST;
sl@0
   562
sl@0
   563
	if (UnicodeHost)
sl@0
   564
		return ::SetVolumeLabelW(lpRootPathName,lpVolumeName);
sl@0
   565
sl@0
   566
	return ::SetVolumeLabelA(Buf8<MAX_PATH>(lpRootPathName),Buf8<MAX_PATH>(lpVolumeName));
sl@0
   567
	}
sl@0
   568
sl@0
   569
sl@0
   570
/**
sl@0
   571
Maps an NT error to an Epoc32 error.
sl@0
   572
	
sl@0
   573
@return	 Last-error code of the calling thread.
sl@0
   574
*/
sl@0
   575
EXPORT_C TInt Emulator::LastError()
sl@0
   576
sl@0
   577
// For other error codes look at MSDN "Numerical List of Error Codes"
sl@0
   578
	{
sl@0
   579
	switch (GetLastError())
sl@0
   580
		{
sl@0
   581
	case ERROR_SUCCESS:				return KErrNone;
sl@0
   582
	case ERROR_INVALID_DRIVE:		return KErrNotReady;
sl@0
   583
	case ERROR_INVALID_NAME:
sl@0
   584
	case ERROR_FILENAME_EXCED_RANGE:
sl@0
   585
	case ERROR_OPEN_FAILED:			return KErrBadName; 
sl@0
   586
	case ERROR_INVALID_HANDLE:		return KErrBadHandle;
sl@0
   587
	case ERROR_NOT_SUPPORTED:
sl@0
   588
	case ERROR_INVALID_FUNCTION:	return KErrNotSupported;
sl@0
   589
	case ERROR_SHARING_VIOLATION: 
sl@0
   590
	case ERROR_ACCESS_DENIED: 
sl@0
   591
	case ERROR_WRITE_PROTECT:		return KErrAccessDenied;
sl@0
   592
	case ERROR_LOCK_VIOLATION:		return KErrLocked;
sl@0
   593
	case ERROR_FILE_NOT_FOUND:
sl@0
   594
	case ERROR_MOD_NOT_FOUND:		return KErrNotFound;
sl@0
   595
	case ERROR_DIRECTORY: 
sl@0
   596
	case ERROR_BAD_PATHNAME:
sl@0
   597
	case ERROR_PATH_NOT_FOUND:		return KErrPathNotFound; 
sl@0
   598
	case ERROR_ALREADY_EXISTS:
sl@0
   599
	case ERROR_FILE_EXISTS:			return KErrAlreadyExists;
sl@0
   600
	case ERROR_NOT_READY:			return KErrNotReady; 
sl@0
   601
	case ERROR_UNRECOGNIZED_VOLUME:
sl@0
   602
	case ERROR_NOT_DOS_DISK:		return KErrUnknown;
sl@0
   603
	case ERROR_UNRECOGNIZED_MEDIA:
sl@0
   604
	case ERROR_BAD_EXE_FORMAT:		return KErrCorrupt;
sl@0
   605
	case ERROR_NO_MORE_FILES:		return KErrEof; 
sl@0
   606
	case ERROR_DIR_NOT_EMPTY:		return KErrInUse;
sl@0
   607
	case ERROR_INVALID_USER_BUFFER:
sl@0
   608
	case ERROR_NOT_ENOUGH_MEMORY:
sl@0
   609
	case ERROR_INSUFFICIENT_BUFFER:
sl@0
   610
	case ERROR_OUTOFMEMORY:			return KErrNoMemory;
sl@0
   611
	case ERROR_DISK_FULL:			return KErrDiskFull;
sl@0
   612
	case ERROR_INVALID_DATA:
sl@0
   613
	case ERROR_INVALID_PARAMETER:	return KErrArgument;
sl@0
   614
	case ERROR_OPERATION_ABORTED:	return KErrCancel;
sl@0
   615
sl@0
   616
    default:						return KErrGeneral;
sl@0
   617
		}
sl@0
   618
	}
sl@0
   619
sl@0
   620
/**
sl@0
   621
This function wraps the Win32 GetProcAddress API (see http://msdn2.microsoft.com/en-us/library/ms683212.aspx).
sl@0
   622
*/
sl@0
   623
FARPROC Emulator::GetProcAddress(HMODULE hModule, LPCSTR lpProcName)
sl@0
   624
	{
sl@0
   625
	__LOCK_HOST;
sl@0
   626
	return ::GetProcAddress(hModule, lpProcName);
sl@0
   627
	}
sl@0
   628
sl@0
   629
sl@0
   630
sl@0
   631
// image file support
sl@0
   632
sl@0
   633
// loaded modules
sl@0
   634
sl@0
   635
sl@0
   636
/**
sl@0
   637
Gets the header format of the file system.
sl@0
   638
sl@0
   639
@return		Returns the header format of the file system.	
sl@0
   640
*/
sl@0
   641
EXPORT_C const IMAGE_NT_HEADERS32* Emulator::TModule::NtHeader() const
sl@0
   642
	{
sl@0
   643
	if (!IsValid())
sl@0
   644
		return 0;
sl@0
   645
	const IMAGE_DOS_HEADER* dhead = (const IMAGE_DOS_HEADER*)Translate(0);
sl@0
   646
	if ( IMAGE_DOS_SIGNATURE != dhead->e_magic )
sl@0
   647
		return 0;
sl@0
   648
	if (dhead->e_lfarlc < sizeof(IMAGE_DOS_HEADER))
sl@0
   649
		return 0;
sl@0
   650
	const IMAGE_NT_HEADERS32* ntHead = (const IMAGE_NT_HEADERS32*)Translate(dhead->e_lfanew);
sl@0
   651
    if ( ntHead->Signature != IMAGE_NT_SIGNATURE )
sl@0
   652
		return 0;
sl@0
   653
	return ntHead;
sl@0
   654
	}
sl@0
   655
sl@0
   656
sl@0
   657
/**
sl@0
   658
Constructor which sets the handles of loaded module to the specified module.
sl@0
   659
sl@0
   660
@param aModuleName		Holds the name of the module to be loaded.
sl@0
   661
*/
sl@0
   662
EXPORT_C Emulator::TModule::TModule(LPCSTR aModuleName)
sl@0
   663
	: iModule(GetModuleHandleA(aModuleName)), iBase(iModule)
sl@0
   664
	{
sl@0
   665
	if (!NtHeader())
sl@0
   666
		{
sl@0
   667
		iModule = 0;
sl@0
   668
		iBase = 0;
sl@0
   669
		}
sl@0
   670
	}
sl@0
   671
sl@0
   672
sl@0
   673
/**
sl@0
   674
Gets the pointer of the section header if the header name matches with the buffer aSection[].
sl@0
   675
		
sl@0
   676
@param aSection[]	A buffer of type BYTE.		
sl@0
   677
sl@0
   678
@return 	Returns the pointer to section header of the file system.
sl@0
   679
*/
sl@0
   680
EXPORT_C const IMAGE_SECTION_HEADER* Emulator::TModule::SectionHeader(const BYTE aSection[]) const
sl@0
   681
	{
sl@0
   682
	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0
   683
	if (!ntHead)
sl@0
   684
		return 0;
sl@0
   685
sl@0
   686
	const IMAGE_SECTION_HEADER* imgHead = (const IMAGE_SECTION_HEADER*)((TUint8*)&ntHead->OptionalHeader + ntHead->FileHeader.SizeOfOptionalHeader);
sl@0
   687
	const IMAGE_SECTION_HEADER* end = imgHead + ntHead->FileHeader.NumberOfSections;
sl@0
   688
	for (; imgHead < end; ++imgHead)
sl@0
   689
		{
sl@0
   690
		if (memcmp(imgHead->Name, aSection, IMAGE_SIZEOF_SHORT_NAME)==0)
sl@0
   691
			return imgHead;
sl@0
   692
		}
sl@0
   693
	return 0;
sl@0
   694
	}
sl@0
   695
sl@0
   696
sl@0
   697
/**
sl@0
   698
Points to the first byte or first page of the loaded module or mapped file image.
sl@0
   699
		
sl@0
   700
@param aSection[]	A buffer of type BYTE.
sl@0
   701
		
sl@0
   702
@return TAny		Returns first byte of the loaded module or first page of the mapped file image.
sl@0
   703
*/
sl@0
   704
EXPORT_C const TAny* Emulator::TModule::Section(const BYTE aSection[]) const
sl@0
   705
	{
sl@0
   706
	const IMAGE_SECTION_HEADER* imgHead = SectionHeader(aSection);
sl@0
   707
	if (imgHead)
sl@0
   708
		return Translate(IsLoaded() ? imgHead->VirtualAddress : imgHead->PointerToRawData);
sl@0
   709
	return 0;
sl@0
   710
	}
sl@0
   711
sl@0
   712
sl@0
   713
/**
sl@0
   714
Go through the section headers looking for UID section.
sl@0
   715
	
sl@0
   716
@param aType	Contains a UID type.
sl@0
   717
*/
sl@0
   718
EXPORT_C void Emulator::TModule::GetUids(TUidType& aType) const
sl@0
   719
	{
sl@0
   720
	const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian);
sl@0
   721
	if (hdr)
sl@0
   722
		aType = *(const TUidType*)&hdr->iUids[0];
sl@0
   723
	}
sl@0
   724
sl@0
   725
sl@0
   726
/**
sl@0
   727
Goes through section headers and gets the information of file system.
sl@0
   728
sl@0
   729
@param aInfo	Contains the information of the file system.
sl@0
   730
*/
sl@0
   731
EXPORT_C void Emulator::TModule::GetInfo(TProcessCreateInfo& aInfo) const
sl@0
   732
	{
sl@0
   733
	aInfo.iExceptionDescriptor = 0;
sl@0
   734
	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0
   735
	if (ntHead)
sl@0
   736
		{
sl@0
   737
		const TEmulatorImageHeader* hdr = (const TEmulatorImageHeader*)Section(KWin32SectionName_Symbian);
sl@0
   738
		if (hdr)
sl@0
   739
			{
sl@0
   740
			aInfo.iUids = *(const TUidType*)&hdr->iUids[0];
sl@0
   741
			TBool isExe = (hdr->iUids[0].iUid==KExecutableImageUidValue);
sl@0
   742
			TBool data_section_present = (Section(KWin32SectionName_EpocData)!=NULL);
sl@0
   743
			TBool data = hdr->iFlags & KEmulatorImageFlagAllowDllData;
sl@0
   744
			if (data_section_present && isExe)
sl@0
   745
				data = ETrue;
sl@0
   746
			aInfo.iBssSize=data?1:0;
sl@0
   747
			aInfo.iDataSize=0;
sl@0
   748
			aInfo.iTotalDataSize=aInfo.iBssSize;
sl@0
   749
			aInfo.iDepCount = 0;
sl@0
   750
			aInfo.iHeapSizeMin = ntHead->OptionalHeader.SizeOfHeapCommit;
sl@0
   751
			aInfo.iHeapSizeMax = ntHead->OptionalHeader.SizeOfHeapReserve;
sl@0
   752
			aInfo.iStackSize = 0x1000;
sl@0
   753
			aInfo.iPriority = hdr->iPriority;
sl@0
   754
			aInfo.iHandle = NULL;
sl@0
   755
			aInfo.iS = hdr->iS;
sl@0
   756
			aInfo.iModuleVersion = hdr->iModuleVersion;
sl@0
   757
			if (ntHead->FileHeader.Characteristics & IMAGE_FILE_DLL)
sl@0
   758
				aInfo.iAttr |= ECodeSegAttHDll;
sl@0
   759
			}
sl@0
   760
		}
sl@0
   761
	GetUids(aInfo.iUids);
sl@0
   762
	}
sl@0
   763
sl@0
   764
sl@0
   765
/**
sl@0
   766
Finds the import section from the data directory. This relies on the module being loaded.
sl@0
   767
		
sl@0
   768
@return 	Returns the imported executable.
sl@0
   769
*/
sl@0
   770
EXPORT_C const IMAGE_IMPORT_DESCRIPTOR* Emulator::TModule::Imports() const
sl@0
   771
	{
sl@0
   772
	if (!IsLoaded())
sl@0
   773
		return NULL;
sl@0
   774
	const IMAGE_NT_HEADERS32* ntHead = NtHeader();
sl@0
   775
	if (!ntHead)
sl@0
   776
		return NULL;
sl@0
   777
	DWORD va = ntHead->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
sl@0
   778
	if (!va)
sl@0
   779
		return NULL;
sl@0
   780
	return (const IMAGE_IMPORT_DESCRIPTOR*)Translate(va);
sl@0
   781
	}
sl@0
   782
sl@0
   783
// modules in the file system
sl@0
   784
EXPORT_C TInt Emulator::RImageFile::Open(LPCTSTR aImageFile)
sl@0
   785
	{
sl@0
   786
	Buf8<MAX_PATH>   nameBuf(aImageFile);
sl@0
   787
	char *pName = (char *)strrchr(LPCSTR(nameBuf), '\\');
sl@0
   788
	pName ? ++pName : pName = (char *)LPCSTR(nameBuf);
sl@0
   789
sl@0
   790
	__LOCK_HOST;
sl@0
   791
	iMapping = OpenFileMapping(FILE_MAP_READ, FALSE, (LPCTSTR)pName);
sl@0
   792
	if (!iMapping)
sl@0
   793
		{
sl@0
   794
		if (pName == (char *)LPCSTR(nameBuf))
sl@0
   795
			iModule = Emulator::GetModuleHandle(aImageFile);
sl@0
   796
		if (iModule)
sl@0
   797
			iBase = iModule;
sl@0
   798
		else
sl@0
   799
			{
sl@0
   800
			// need to map the file instead
sl@0
   801
			HANDLE file = Emulator::CreateFile(aImageFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
sl@0
   802
			if (file == INVALID_HANDLE_VALUE)
sl@0
   803
				return LastError();
sl@0
   804
			iMapping = CreateFileMappingA(file, NULL, PAGE_READONLY, 0, 0, pName);
sl@0
   805
			CloseHandle(file);
sl@0
   806
			}
sl@0
   807
		}
sl@0
   808
	if (!iModule)
sl@0
   809
		{
sl@0
   810
		if (!iMapping)
sl@0
   811
			return LastError();
sl@0
   812
sl@0
   813
		iBase = MapViewOfFile(iMapping, FILE_MAP_READ, 0, 0, 0);
sl@0
   814
sl@0
   815
		if (!iBase)
sl@0
   816
			{
sl@0
   817
			CloseHandle(iMapping);
sl@0
   818
			iMapping = 0;
sl@0
   819
			return LastError();
sl@0
   820
			}
sl@0
   821
		}
sl@0
   822
sl@0
   823
	if (!NtHeader())
sl@0
   824
		{
sl@0
   825
		Close();
sl@0
   826
		return KErrNotSupported;
sl@0
   827
		}
sl@0
   828
	return KErrNone;
sl@0
   829
	}
sl@0
   830
sl@0
   831
EXPORT_C void Emulator::RImageFile::Close()
sl@0
   832
	{
sl@0
   833
	if (iMapping)
sl@0
   834
		{
sl@0
   835
		UnmapViewOfFile(iBase);
sl@0
   836
		CloseHandle(iMapping);
sl@0
   837
		iMapping = 0;
sl@0
   838
		}
sl@0
   839
	iBase = 0;
sl@0
   840
	iModule = 0;
sl@0
   841
	}