Hey, it's me again. This time with a thing that has been bugging me for months and i still haven't gotten around to solving it.

I need to install a font without rebooting the computer. Sounds simple but i have to do it in code or with a batch file, popping windows in front of the user isn't an option. I've done a lot of trials on batch files and that fontinst.exe crap, nothing worked. Somehow i stumbled upon the MSDN documentation of AddFontResource() which sounds like the thing for me. I understand i also have to use SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0) at the end so the fonts are refreshed in all windows.

I chose to do it in C++ because i also learn it at school (i'm in 11th grade). What we learn is the old Borland stuff but still it is of some use. Anyway, i really need to wrap up all those batches and javascripts i wrote in one single program with as little dependencies as possible, and C++ seems to be the best choice.

I'm currently using Dev-C++ 5 beta.

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
    string myfont;
    cout<<"Gimme my font: ";cin>>myfont;
int AddFontResource(
LPCTSTR myfont   // font file name
);
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
    system("PAUSE");
    return EXIT_SUCCESS;
}

I wrote that just to test the function. The problem with the above code is that... It does nothing. :-O It compiles and runs, but the font is still not registered to the system until i reboot. Prior to running this program the font is copied to the Windows\Fonts folder and the necessary registry entries are created, via a batch file.

I need some explanation on the LPCTSTR thing. What the heck is it exactly? It seems it's not a string as it doesn't take the name stored in "myfont". The MSDN entry is very very vague.

The AddFontResource function adds the font resource from the specified file to the system font table. The font can subsequently be used for text output by any application.

int AddFontResource(
LPCTSTR lpszFilename // font file name
);

Parameters
lpszFilename
[in] Pointer to a null-terminated character string that contains a valid font file name. This parameter can specify any of the following files.

Uhh... what??? I really don't understand the part in bold. Could someone shed some light on this please?

Recommended Answers

All 3 Replies

I'm a newbie, so i make newbie mistakes. I didn't understand Microsoft's notations and thought LPCTSTR was a parameter, while it was actually the LPCTSTR datatype. I got it solved like this:

#include <cstdlib>
#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char *argv[])
{
char filename[255];
cout<<"Gimme my font: ";cin>>filename;
LPCTSTR myFont = filename;

AddFontResource(myFont);
SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);

    system("PAUSE");
    return EXIT_SUCCESS;
}

And adding -lgdi32 to the Dev-Cpp linker's parameters. Damn, it was that simple.

Off to make it read the font name from an INI file and wrap up the rest of the batches. ;)

But if char array is all you have, and you're not compiling with UNICODE, then this (it would seem) would work just as well

AddFontResource(filename);

I might need Unicode sometime, i'll just leave it as it is for now. I set it to read from an INI file, now i'm off to finish the rest of the code.

Thanks, and you can be sure i'll be back if i run into trouble again. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.