Hi Group!  First of all, greetings from Argentina.

I've been programming for a while in VB .NET and, frankly, I can't stand the zillion GB framework "portability" anymore.  A "Hello World" program takes a few KB's, but in order to distribute it you have to pack the whole framework thing.

I've "discovered" win32 assembly recently and love the little exe's it makes, just like the good old days.

I've been trying to embed a font in my exe, with mixed results:

a) Worked OK when loading it from a ttf file using the AddFontResource, SendMessage invokes.  I mean, I place a ttf file in the same directory, simply point to that filename in the AddFontResource invoke and voila, it works.  As long as the exe finds the ttf file in its own directory, everything works fine.  This would be the "unembedded" alternative.

b) When I try to do exactly the same thing but creating a RES file I cannot make it work.

The RC file looks like this:

#define FONTLOKI 1
FONTLOKI FONT "LokiCola.ttf"

The ASM file (a mutation of Iczelion's Tut Number 5) is:
FontName db "Loki Cola",0
            invoke AddFontResource, FONTLOKI
            invoke SendMessage,HWND_BROADCAST,WM_FONTCHANGE,0,0
            invoke CreateFont,100,0,0,0,400,0,0,0,DEFAULT_CHARSET,\
                            ADDR FontName

The only difference with the unembedded alternative is that I am replacing "LokiCola.ttf" with FONTLOKI at the  AddFontResource invoke.


Thanks in advance.

Posted on 2007-03-30 14:41:35 by Mdurruty
Resources cannot be referenced directly like that, you may wish to look up some of the following API: FindResource,LoadResource,LockResource.
Also, it may be a better idea to write the resource to disk before loading it, in which case you will also need CreateFile, WriteFile and CloseHandle. Also of interest may be GetTempFileName and GetTempPath.

Hope this helps, Ehtyar.
Posted on 2007-03-30 16:26:02 by Ehtyar

Thank you for your quick response.

It was my understanding that resources could actually be referenced directly.  In Iczelion's tutorials, bitmaps are embedded in the exe code and referenced just like I did in my example above.  I thought that bmp's, icons and fonts could be referenced that way.

Since the FONT is just there, properly addressed within the exe code, I thought that there should be a more straightforward way to reference it.

Other opinions will be welcomed.

Thanks again.

Posted on 2007-03-30 19:18:04 by Mdurruty

After searching the Internet for hours, finally I've found a site where someone has succesfully embedded a font in his executable file, loaded it to memory and used it wherever needed.

Here is the link:


As you might see, he uses the AddFontMemResourceEx function.  In the article the poster says that the piece of code works seamlessly for True Type Fonts.

The only problem is that it's written in Pascal, and I don't know how to translate it to MASM (I mean, learning the correct buffering sizing and memory allocation, plus handles management might take me months to make it properly work).

I wonder if anybody here could help me doing that job.  An adaptation of Iczelion's tut number 5, so as to make it draw text with an embedded font, would be great.

Thanks in advance.

Posted on 2007-04-01 13:36:34 by Mdurruty
Keep in mind that AddFontMemResourceEx is unsupported on Win9x, and only works on NT from win2000 and onwards.

Unfortunately I'm heading off for work soon, so I don't have time to look at the Delphi code - it's generally pretty easy to do conversion to assembly when code is mostly API calls, though.
Posted on 2007-04-03 10:03:53 by f0dder