Access Violation Error when calling Gdiplus::PrivateFontCollection.AddMemoryFont

2

Following this walkthrough, showing how to use a font without having to install it in C++: http://www.codeproject.com/Articles/42041/How-to-Use-a-Font-Without-Installing-it I get an Adress Violation Error (0xc0000005) in ntdll.dll

The calling code in the DialogEx::OnInitDialog() inherited function is the following:

Gdiplus::PrivateFontCollection m_fontcollection;
HINSTANCE instance = AfxGetResourceHandle();
HRSRC res = FindResource(instance, MAKEINTRESOURCE(IDR_MAIN_FONT), L"BINARY");

HGLOBAL mem = LoadResource(instance, res);
void *data = LockResource(mem);
size_t len = SizeofResource(instance, res);

Gdiplus::Status Result = m_fontcollection.AddMemoryFont(data, len); // Exception arises

I debugged the calls and everything looks good to me. The length matches and there are no NullPointers...

The Exception happens somewhere in the called method in gdiplusfontcollection.h

inline Status
PrivateFontCollection::AddMemoryFont(IN const void* memory,
                                     IN INT length)
{
    return SetStatus(DllExports::GdipPrivateAddMemoryFont(
        nativeFontCollection,
        memory,
        length));
}

which was not further debuggable...

Any ideas how to find out what is going wrong in the function call?

c++
mfc
gdi+
privatefontcollection
asked on Stack Overflow Aug 29, 2014 by Vinz • edited Jul 6, 2019 by Cœur

1 Answer

-1

Seems to be the nativeFontCollection is null. GdiplusStartup method must be called before. Don't forget to call GdiplusShutdown after.

answered on Stack Overflow Sep 30, 2019 by Alexey Dobroslavsky • edited Sep 30, 2019 by Toby Speight

User contributions licensed under CC BY-SA 3.0