Skip to content

Commit 682a0c6

Browse files
committed
修正GetTextResource读取TEXT资源时没有判断资源大小导致后面可能会有乱码的问题
1 parent 776d03d commit 682a0c6

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

TrafficMonitor/Common.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,16 +975,21 @@ CString CCommon::GetTextResource(UINT id, int code_type)
975975
HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(id), _T("TEXT"));
976976
if (hRes != NULL)
977977
{
978+
DWORD resSize = SizeofResource(NULL, hRes); // 获取资源的大小
978979
HGLOBAL hglobal = LoadResource(NULL, hRes);
979980
if (hglobal != NULL)
980981
{
982+
LPVOID pResourceData = LockResource(hglobal); // 获取资源数据的指针
981983
if (code_type == 2)
982984
{
983-
res_str = (const wchar_t*)hglobal;
985+
// 资源是宽字符字符串
986+
res_str = CString((const wchar_t*)pResourceData, resSize / sizeof(wchar_t));
984987
}
985988
else
986989
{
987-
res_str = CCommon::StrToUnicode((const char*)hglobal, (code_type != 0)).c_str();
990+
// 资源是窄字符字符串
991+
std::string strData((const char*)pResourceData, resSize);
992+
res_str = CCommon::StrToUnicode(strData.c_str(), (code_type != 0)).c_str();
988993
}
989994
}
990995
}

0 commit comments

Comments
 (0)