0

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.

user9778277
  • 153
  • 1
  • 11
  • At the beginning of the thread the code: GdiplusStartupInput gdipsi; GdiplusStartupOutput gdipso; Status stat = GdiplusStartup(&token, &gdipsi, &gdipso); returns stat == Status::Ok and token = 418150485 Then it crashes with the following: – user9778277 Jan 28 '22 at 04:06
  • Unhandled exception at 0x00007FF9D41E6C34 (gdi32full.dll) in PhotoTeca.exe: 0xC0000006: In page error reading location 0x000002CE29EA0028 (status code 0xC0000185). – user9778277 Jan 28 '22 at 04:07
  • Use SEH instead if you plan to catch the exception (or if you want to catch the exceptions globally you can install a Vectored Exception handler). Because the exception is not a C++ one. – AnArrayOfFunctions Jan 28 '22 at 04:08
  • I don't know if it is relevant, but the code is being called from the DLL. The DLL spawned the thread – user9778277 Jan 28 '22 at 04:09
  • The error is `STATUS_IO_DEVICE_ERROR` - generally look here for those [NT statuses](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55). – AnArrayOfFunctions Jan 28 '22 at 04:13
  • Severity Code Description Project File Line Suppression State Error C2712 Cannot use __try in functions that require object unwinding ImageLibrary c:\users\tarie\source\repos\photolibrary\imagelibrary\imageentry.cpp 72 – user9778277 Jan 28 '22 at 04:16
  • https://stackoverflow.com/questions/51701426/cannot-use-try-in-functions-that-require-object-unwinding-fix – AnArrayOfFunctions Jan 28 '22 at 04:18
  • 1
    Use direct 2d gdi+ is really old. – Michael Chourdakis Jan 28 '22 at 05:06
  • @MichaelChourdakis, is Direct 2d readily available without an extra download? – user9778277 Jan 28 '22 at 05:16
  • NOTE: I don't need to display anything in the DLL. I just need to read the metadata from the image to help catalog it – user9778277 Jan 28 '22 at 05:17
  • @AnArrayOfFunctions, it's weird I was able to open the file directly and get the date information and even get file attributes to check if the file should be stored, but GDI + crashes – user9778277 Jan 28 '22 at 05:29
  • Yes, its included in Windows since Vista. – Michael Chourdakis Jan 28 '22 at 05:34
  • Not an answer to the question, but you may want to try the [Windows Imaging Component](https://learn.microsoft.com/en-us/windows/win32/wic/-wic-lh) instead. It is likely to get you better performance over GDI+ when reading metadata only (GDI+ uses the WIC internally, too). Direct2D doesn't buy you anything here; it is a rendering API only. – IInspectable Jan 28 '22 at 09:53

0 Answers0