Windows CE/Mobile突破2G文件读写的限制
The declaration of CFile actually already has a big-file operation interface
CFile::Seek(LONGLONG lOff, UINT nFrom); // LONGLONG -> far far more than 2G
However, in Windows CE/Mobile, it still doesn't support 4G-file I/O.
The secret is in the implementation of CFile in filecore.cpp, as follows:
ULONGLONG CFile::Seek(LONGLONG lOff, UINT nFrom)
{
...
LARGE_INTEGER liOff;
liOff.QuadPart = lOff;
liOff.LowPart = ::SetFilePointer(m_hFile, liOff.LowPart,
#ifndef _WIN32_WCE // ----------> the macro BLOCK Windows CE/Mobile from supporting big files!!!
&liOff.HighPart,
#else // !_WIN32_WCE
NULL,
#endif // !_WIN32_WCE // -------> BLOCK Windows CE/Mobile from supporting big files!!!
(DWORD)nFrom);
...
return liOff.QuadPart;
}
The root cause is simple: the macro __WIN32_WCE blocks reading/writing big files.
Actually, whether support big-file I/O depends on the functionality of SetFilePointer(),
fortunately it supports reading/writing a big file with size more than 2G in Windows CE/Mobile.
So, the simplest method to let Windows CE/Mobile support that could be the following code,
very simple:
class CFile64: public File
{
public:
virtual ULONGLONG Seek(LONGLONG lOff, UINT nFrom)
{
...
LARGE_INTEGER liOff;
liOff.QuadPart = lOff;
liOff.LowPart = ::SetFilePointer(m_hFile, liOff.LowPart,
//#ifndef _WIN32_WCE
&liOff.HighPart,
//#else // !_WIN32_WCE
// NULL,
//#endif // !_WIN32_WCE
(DWORD)nFrom);
...
return liOff.QuadPart;
}
ULONGLONG GetPosition() const
{
ASSERT_VALID(this);
ASSERT(m_hFile != INVALID_HANDLE_VALUE);
LARGE_INTEGER liPos;
liPos.QuadPart = 0;
liPos.LowPart = ::SetFilePointer(m_hFile, liPos.LowPart,
//#ifndef _WIN32_WCE //------------ disable limitation!!
&liPos.HighPart,
//#else // !_WIN32_WCE
// NULL,
//#endif // !_WIN32_WCE
FILE_CURRENT);
if (liPos.LowPart == (DWORD)-1)
if (::GetLastError() != NO_ERROR)
CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
return liPos.QuadPart;
}
}
A famous stupid implementation could be found in the following URL, personally think it is stupid
and low-efficient. Why not directly copy MFC code, and make little change?
CFile64 1.0,2009-year version:
http://download.fyxm.net/CFile64-30528.html
By the way, the biggest file size in FAT32 system is 4G. Most SD cards only support FAT32.