2013-08-12 20 views
0

我想在我的系统中安装一种新的字体,但是在我的代码中出现错误,我找不到我犯的错误。在我调用CopyFile之后,该文件不存在于C:\ Windows \ Fonts中。可以给我一些建议吗?谢谢。 这里是我的代码:为什么我不能将字体文件复制到c: Windows Fonts中CopyFile

`//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;` 
+2

'CopyFile'(和'GetLastError')返回什么? (另外,我猜这是缺少权限) – Hasturkun

+0

CopyFile返回1,GetLaseError值为0,我的权限是manager.the文件存在于C:\ Windows \ Fonts中,但它没有使用。没有名称,没有属性,一旦闪光,它不见了 –

+0

我不认为它可能复制字体到字体文件夹没有安装字体第一。需要'CreateScalableFontResource'和'AddFontResource' APIs – SSpoke

回答

1

有一个pretty good description by Michael Kaplan。总之,你不能也不应该复制文件,因为它是一个虚拟视图。相反,请使用适当的方法:拨打AddFontResourceEx,传递适当的标志。如果字体是系统可用的,则广播WM_FONTCHANGE。要永久安装字体,您需要管理员权限(即UAC提升),因为您需要列出HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts下的字体文件名。

您可能想考虑从您的安装程序执行这些步骤,因为它通常以必要的权限运行。

+0

谢谢所有。我已经解决了这个问题。它似乎将文件复制到像C:\ Windows \ Fonts这样的虚拟视图是不允许的。所以我不复制它,我在函数中使用字体文件的绝对路径(AddFontResource ),在机器重新启动后,字体必须再次安装一次,这有点不好。 –

相关问题