Program works when starting with F5, but does not when starting with CTRL + F5

-2

I wrote a small exe that can install a font, the path to the font is passed via an argument.

  • If I start this program with F5, everything works fine
  • If I start this program with Ctrl + F5, I get an exception System.ComponentModel.Win32Exception (0x80004005): system cannot find the file specified
  • Same behaviour when running the executable via windows cmd.

Here is the code:

public static class FontInstaller
{
    [DllImport(@"C:\Windows\System32\gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
    public static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]string lpFileName);

    static void Main()
    {
        InstallFont(@"C:\Fonts\font.ttf");
    }

    private static void InstallFont(string fontPath)
    {
        // Try install the font.
        var result = AddFontResource(fontPath);
        var error = Marshal.GetLastWin32Error();
        if (error != 0)
        {
            Console.WriteLine(new Win32Exception(error));
        }
        else
        {
            Console.WriteLine(result == 0 ? "Font is already installed." : "Font installed successfully.");
        }
    }
}

What is going wrong here? Where is the difference between F5 and Ctrl F5 in that specific case?

c#
visual-studio
fonts
dllimport
win32exception
asked on Stack Overflow Mar 12, 2019 by Doenbot3000 • edited Mar 24, 2019 by marc_s

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0