Am Creating this function to return the Font Family Name but obviously it seems to be pointing on different results every time:
unsigned int len = txtFormat->GetFontFamilyNameLength();
WCHAR fontFamilyName[64];
txtFormat->GetFontFamilyName(fontFamilyName, len+1);
//fontFamilyName[len] = '\0'; not needed because the function "GetFontFamilyName" returns a null terminated pointer.
return fontFamilyName;
答案 0 :(得分:0)
Change:
txtFormat->GetFontFamilyName(fontFamilyName, len+1);
to:
txtFormat->GetFontFamilyName(fontFamilyName, sizeof(fontFamilyName)/sizeof(fontFamilyName[0]));
This will make sure that GetFontFamilyName doesn't overrun the buffer that you're passing in.
答案 1 :(得分:0)
它比我需要的时间长了一点但我现在就明白了。
原因是用[]创建数组不会返回正确的地址。所以我们可以有两个解决方案:
解决方案1:动态定位要在函数内返回的数组。记得用它完成后删除返回的文本。
inline WCHAR *GetFontFamilyName(IDWriteTextFormat *txtFormat)
{
WCHAR *fontFamilyName = new WCHAR[64];
unsigned int len = txtFormat->GetFontFamilyNameLength();
txtFormat->GetFontFamilyName(fontFamilyName, len+1);
//fontFamilyName[len] = L'\0'; not really needed but it can be used as confirmation
return fontFamilyName;
}
解决方案2:将静态数组传递给函数。在这种情况下,您不必删除它,因为它不是动态分配的。
在这种情况下,您可以将名称[64]等数组传递给该函数,您不必担心删除该文本。
inline void GetFontFamilyName(IDWriteTextFormat *txtFormat, WCHAR fontFamilyName [])
{
unsigned int len = txtFormat->GetFontFamilyNameLength();
txtFormat->GetFontFamilyName(fontFamilyName, len+1);
//fontFamilyName[len] = L'\0'; not really needed but it can be used as confirmation.
}