0

i want to install a new font in my system,but something wrong in my code,i can't find out what mistake i had make.After i call CopyFile,the file didn't exists in C:\Windows\Fonts.Can you give me some suggestion?Thank you. Here is my code:

`//the source file
string sSourceDir = "F:\\my_job\\font\\";
//the file name
string sFontFileName = "jdqw.TTF";
string sFontName = "jdqw";
TCHAR sWinDir[MAX_PATH];

GetWindowsDirectory(sWinDir, MAX_PATH);

string sFontDir(sWinDir);
//make the path like C:\\Windows\\Fonts
sFontDir += "\\Fonts\\";
string sFOTFile = sFontDir;
sFOTFile += sFontFileName.substr(0, sFontFileName.length() - 4);
sFOTFile += ".FOT";
string source = sSourceDir;
string dest = sFontDir;
source += sFontFileName;
dest += sFontFileName;

//copy file
cout << source.c_str() << "   " << dest.c_str() << endl;
cout << CopyFile(source.c_str(), dest.c_str(), FALSE) << endl;
cout << GetLastError() << endl;`
Hitesh Vaghani
  • 1,342
  • 12
  • 31
  • 2
    What do `CopyFile` (and `GetLastError`) return? (Also, I"m guessing it's a lack of permissions) – Hasturkun Aug 12 '13 at 08:33
  • the CopyFile return 1,and GetLaseError value is 0,my permissions is manager.the file exists in C:\Windows\Fonts,but it's unused.no name,no attribute,once flash,it gone – Emersion Liu Aug 12 '13 at 08:36
  • I dont think its possible to copy a font to fonts folder without installing a font first. `CreateScalableFontResource` and `AddFontResource` APIs are required – SSpoke Aug 12 '13 at 08:38
  • 1
    i read an article that said two step to install a new font.First,copy a font to fonts folder,next call AddFontResourcel and register in regedit...its that right? – Emersion Liu Aug 12 '13 at 08:42
  • Yeah you are right try the solution in this post http://stackoverflow.com/questions/8149407/set-folder-permission-using-plain-c-or-java – SSpoke Aug 12 '13 at 08:51
  • This is how Microsoft guide you http://msdn.microsoft.com/en-us/library/dd144833.aspx – Nayana Adassuriya Aug 12 '13 at 09:06
  • the problem is still exists...what confuse me is that after call CopyFile, a file which i can't read and write exists,and flash the folder,it's gone.And i try to copy and paste the font into this folder,system install it and then the font can use... – Emersion Liu Aug 12 '13 at 09:10

1 Answers1

1

There's a pretty good description by Michael Kaplan. In short, you can't and shouldn't copy files there because it is a virtual view. Instead, use the appropriate method: Call AddFontResourceEx, pass the appropriate flags. If the font is system-wide available, broadcast WM_FONTCHANGE. To install the font permanently, you require admin rights (ie. UAC elevation), because you need to list the font filename under HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts.

You might want to consider doing these steps from your installer instead, since that usually runs with the necessary permissions.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Thanks all.I have solved this problem.It seem that copy a file to a virtual view like C:\Windows\Fonts is not allowed.So I don't copy it,I use the absolute path of the font file in the function(AddFontResource),it is a little bad that after thd machine reboot,the font have to install once againt. – Emersion Liu Aug 13 '13 at 03:27