I'm using the AddFontResource function to install a font locally for the current login session.
private void installFont(string fontPath)
{
IntPtr HWND_BROADCAST = new IntPtr(0xFFFF);
const int WM_FONTCHANGE = 0x1D;
string fontLocation = Environment.ExpandEnvironmentVariables(fontPath);
int result = AddFontResourceA(fontLocation);
//This is currently printing Number of Fonts Installed = 1
Console.WriteLine("Number of Fonts Installed = " + result);
SendMessage(HWND_BROADCAST, WM_FONTCHANGE);
PrivateFontCollection fontCol = new PrivateFontCollection();
fontCol.AddFontFile(fontLocation);
var actualFontName = fontCol.Families[0].Name;
Console.WriteLine("Font Installed? = " + IsFontInstalled(actualFontName));
}
The int result that the AddFontResource
function is returning is 1, which according to the documentation is the number of fonts that were successfully installed.
If the function succeeds, the return value specifies the number of fonts added.
If the function fails, the return value is zero. No extended error information is available.
I am then programmatically testing the font using the following code.
private static bool IsFontInstalled(string fontName)
{
using (var testFont = new Font(fontName, 8))
{
return fontName.Equals(testFont.Name, StringComparison.InvariantCultureIgnoreCase);
}
}
However the isFontInstalled
function is always returning false.
This function runs a simple test where it tries to create a Font using the installed Fonts name. If the installation is successful, the new font will have the name of the font used, if not installed it will default to a different System font name.
NOTE I recognize that my current implementation of verifying font installation programmatically may not work for all cases, feel free to suggest superior ways to verify, I assume part of the issue may be that my current implementation only works to verify fonts that were installed using the registry.
I use this same function to test if a font I install via the registry is installed and it works as expected. Any insights into how to use the font that was apparently installed?
According to the docs:
This function installs the font only for the current session. When the system restarts, the font will not be present. To have the font installed even after restarting the system, the font must be listed in the registry.
To my understanding current session lasts until the user logs out, and that should include the test function of this program.
According to the documentation on RemoveFontResourceA function
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
I created a quick application where I added a font using AddFontResource
on a valid font and then called RemoveFontResource
on that same valid font, a non-zero exit code was returned. I then did the opposite and added a bogus font using the AddFontResource
, and RemoveFontResource
returned a 0 exit code. You can use this to validate whether or not the font was actually installed, and if it succeeds in uninstalling, simply have the verify method reinstall the font again.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With