I have an application that catalogs image files on my computer. In CWinApp::InitInstance, I initialize Gdiplus using the following:
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
also, in the init function of my library class, I do the same.
Finally, because I spawn a separate thread to perform this task, I initialize GDI+ again at the beginning of the thread proc. The meat and potatoes of this is a call to the following code:
void CImageEntry::UpdateInformation()
{
if (IsRemoteFile())
{
UpdateRemoteFileInformation();
return;
}
HANDLE hFile = CreateFile(m_csPath.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile && hFile != INVALID_HANDLE_VALUE)
{
FILETIME CreationTime, LastAccessTime, LastWriteTime;
GetFileTime(hFile, &CreationTime, &LastAccessTime, &LastWriteTime);
FileTimeToSystemTime(&CreationTime, &m_sysCreationTime);
FileTimeToSystemTime(&LastAccessTime, &m_sysLastAccessTime);
FileTimeToSystemTime(&LastWriteTime, &m_sysLastWriteTime);
CloseHandle(hFile);
Sleep(10);
hFile = NULL;
}
try
{
Gdiplus::Image img(m_csPath.c_str());
if (img.GetLastStatus() == Gdiplus::Status::Ok)
{
UINT totalBufferSize = 0;
UINT numProperties = 0;
img.GetPropertySize(&totalBufferSize, &numProperties);
Gdiplus::PropertyItem* pAllItems = (Gdiplus::PropertyItem*)malloc(totalBufferSize);
img.GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
free(pAllItems);
}
}
catch (...)
{
}
}
using Image::FromFile for a pointer is failing as well. The exception crashes my application despite the try/catch block. m_csPath has been previously validated and thus guaranteed to exist here.