exhostappservices::ttffilenamebydescriptor problems
exhostappservices::ttffilenamebydescriptor problems
question is related to win32 platform only. sample code refered to are from the 2.0.3 release.
the current implementation in the examples of ttffilenamebydescriptor cause access violations now and then when it fails to find a font.
so i'm currently in the progress of rewriting this for our application.
but while reading another post i came across a reply stating that:
"for ttf fonts on windows dd uses windows api to access installed fonts. and does not access .ttf files directly."
ttffilenamebydescriptor() is used to get file name to access ttf font via freetype library and in file dependence manager. freetype library is used on non windows platform basically but may be use on windows also (for example: debug). so we need file name (freetype) and descriptor(windows api) for initialize font manager.
we don't need to call this function for shx font because drawing contents file name.
best regards,
sergey z.
i'm also having occasional crashes caused because the font enumeration code isn't checking for success before using the result. is there any chance this could be fixed for the next release? we copy this class directly from the installation into our codebase when we upgrade dwgdirect so it would be nice if we didn't have to do the change ourselves everytime. thanks.
can someone please explain how this fix was included in 2.2:
4083 - memory overwrite may occur in exhostappservices::ttffilenamebydescriptor when font is not found.
i have found that this method causes an exception even on 2.2. i thought the sample code may have an updated method but it appears to be exactly the same as what i have.
if i comment out this method and just return false the exception goes away.
thanks for any additional info you can provide.
chad
the fix certainly got into 2.2. make sure you did not mix old and new versions. in 2.2 the code looks like this:
code:
bool exhostappservices::ttffilenamebydescriptor(const odttfdescriptor& descr, odstring& filename)
{
#if defined(_msc_ver) && defined(_win32) && !defined(_win32_wce)
odstring snonexactmatched;
odstring sfacename = descr.typeface();
osversioninfo os;
os.dwosversioninfosize=sizeof(osversioninfo);
::getversionex(&os);
odstring sname;
sname.format(dd_t("software\\microsoft\\windows%ls\\currentversion\\"),
(os.dwplatformid & ver_platform_win32_nt)!=0 ? dd_t(" nt") : dd_t(""));
long nres;
hkey hfontsubstitutes;
dword ndatasize;
nres = ::regopenkeyex(hkey_local_machine, sname + dd_t("fontsubstitutes\\"), 0, key_read, &hfontsubstitutes);
if (hfontsubstitutes)
{
odstring svaluename;
svaluename.format(dd_t("%ls,%d"), descr.typeface().c_str(), descr.charset());
nres = ::regqueryvalueex(hfontsubstitutes, svaluename, null, null, null, &ndatasize);
if(nres == error_success)
{
char* lpdata = new char[ndatasize];
nres = ::regqueryvalueex(hfontsubstitutes, svaluename, null, null, (lpbyte)lpdata, &ndatasize);
sfacename = (lpctstr)lpdata;
delete [] lpdata;
int n = sfacename.find(',');
if(n>0)
{
sfacename = sfacename.left(n);
}
}
nres = ::regclosekey(hfontsubstitutes);
}
hkey hfonts;
::regopenkeyex(hkey_local_machine, sname + dd_t("fonts\\"), 0, key_read, &hfonts);
if (hfonts)
{
dword nindex = 0;
dword nvalnamesize = 20;
ndatasize = odmax(filename.getalloclength(), 20);
int n_bt = sfacename.replace(dd_t(" bt"), dd_t(" "));
for(;

{
odstring svaluename;
do
{
tchar* lpvalname = new tchar[nvalnamesize];
lpbyte lpdata = new byte[ndatasize];
nres = ::regenumvalue(hfonts, nindex, lpvalname, &nvalnamesize, null, null, lpdata, &ndatasize);
if (nres == error_more_data)
{
nvalnamesize += 20;
}
else if (nres == error_success)
{
svaluename = lpvalname;
filename = (lpctstr)lpdata;
}
else
{
svaluename.empty();
filename.empty();
}
delete [] lpvalname;
delete [] lpdata;
}
while (nres == error_more_data);
if (nres == error_success)
{
++nindex;
nvalnamesize = svaluename.getalloclength();
ndatasize = filename.getalloclength();
if(svaluename.replace(dd_t("(truetype)"), dd_t(""))) // is truetype font?
{
if(svaluename.replace(dd_t(" bt"), dd_t(""))==n_bt)
{
bool bbold = false;
if(n_bt)
{
svaluename.replace(dd_t(" extra bold "), dd_t(" xbd "));
}
else
{
if(svaluename.find(dd_t(" extra bold "))==-1)
bbold = (svaluename.replace(dd_t(" bold "), dd_t(" "))!=0);
}
bool bitalic = (svaluename.replace(dd_t(" italic "), dd_t(" "))!=0);
svaluename.remove(' ');
sfacename.remove(' ');
if(svaluename==sfacename)
{
snonexactmatched = filename;
if(descr.isbold()==bbold && descr.isitalic()==bitalic)
break;
}
if ( svaluename.find((sfacename+l"&").c_str()) != -1 ||
svaluename.find((l"&"+sfacename).c_str()) != -1 )
{
snonexactmatched = filename;
break;
}
}
}
}
else
{
filename = snonexactmatched;
break;
}
}
::regclosekey(hfonts);
}
return (!filename.isempty());
#else
return false;
#endif
}
where do you get the error?
vladimir
thank you very much. after a closer inspection of the code i found where there were a couple of changes.
before making this change, i would get unexplainable exceptions without a clear callstack after loading a specific set of multiple dwg files consecutively. i never saw it crash in this method.
-chad