这是一个读取整个文件并支持 UNICODE 的函数框架:
void MyReadFile(wchar_t *filename)
{
HANDLE hFile;
DWORD dwBytesRead = 0;
wchar_t ReadBuffer[BUFFERSIZE] = {0};
OVERLAPPED ol = {0};
hFile = CreateFile(filename,
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
return;
}
// Read one character less than the buffer size to save room for
// the terminating NULL character.
if( ReadFileEx(hFile, ReadBuffer, BUFFERSIZE-1, &ol, FileIOCompletionRoutine) == FALSE)
{
CloseHandle(hFile);
return;
}
SleepEx(5000, TRUE);
dwBytesRead = g_BytesTransferred;
if (dwBytesRead > 0 && dwBytesRead <= BUFFERSIZE-1)
{
ReadBuffer[dwBytesRead]=L'\0'; // NULL character
}
else if (dwBytesRead == 0)
{
}
else
{
}
CloseHandle(hFile);
}