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?