void CoordinateAlignDlg::ReadToMemory(const CString& strFilePath, IStream*& pStream)
{
// 将文件读到流内存中
// TCHAR achErrInfo[512] = { 0 };
HANDLE hFile = ::CreateFile(strFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
// memset(achErrInfo, 0, sizeof(achErrInfo));
// _stprintf(achErrInfo, _T("Load (file): Error opening file %s\n"), strFilePath);
// ::OutputDebugString(achErrInfo);
::CloseHandle(hFile);
return;
}
DWORD dwSize;
dwSize = ::GetFileSize(hFile, NULL);
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, dwSize);
char *pData = reinterpret_cast<char*>(GlobalLock(hGlobal));
if (!pData)
{
// ::OutputDebugString(_T("Load (file): Error locking memory\n"));
GlobalFree(hGlobal);
::CloseHandle(hFile);
return;
};
try
{
DWORD dwReadBytes = 0;
::ReadFile(hFile, pData, dwSize, &dwReadBytes, NULL);
}
catch ( /*CFileException, e*/...)
{
// ::OutputDebugString(achErrInfo);
GlobalFree(hGlobal);
::CloseHandle(hFile);
return;
}
GlobalUnlock(hGlobal);
::CloseHandle(hFile);
if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) != S_OK)
return;
}