BOOL ProcessTheFile(void)
{
HANDLE hFile = (HANDLE)-1;
DWORD dwBytesRead, dwFileSize;
OpenFileName Ofn;
TCHAR szFile[MAX_PATH+1] = "\0";
TCHAR *lpBuf = NULL;
BOOL bRet = FALSE;
if (! GetOpenFileName(&Ofn))
goto Exit;
if ((hFile=CreateFile(Ofn.lpstrFile)) == (HANDLE)-1)
{
MessageBox(NULL, "File open failed.");
goto Exit;
}
dwFileSize = GetFileSize(hFile);
if (dwFileSize == 0xFFFFFFFF) // invalid size
{
MessageBox(NULL, "GetFileSize failed!");
goto Exit;
}
if (dwFileSize == 0)
goto Exit; // exit if nothing to read
lpBuf = (TCHAR *)GlobalAlloc(GMEM_FIXED,dwFileSize);
if (lpBuf == NULL)
{
MessageBox(NULL, "GlobalAlloc failed!");
goto Exit;
}
if (! ReadFile(hFile,lpBuf,dwFileSize,&dwBytesRead))
{
MessageBox(NULL, "Error reading file.");
goto Exit;
}
if (dwBytesRead == 0)
{
MessageBox(NULL, "Zero bytes read.");
goto Exit;
}
if (DoSomething(lpBuf))
bRet = TRUE;
Exit:
if (lpBuf)
GlobalFree(lpBuf);
if (hFile != (HANDLE)-1)
CloseHandle(hFile);
return bRet;
}