CString.Format 在 32 位中崩溃
CString.Format crashes in 32-bit
我的 CString 格式导致 32 位 Unicode MFC 崩溃 Static/VS2013 SDK 文件中的项目 output.c 第 1629 行 while (i-- && *pwch)
bool MyClass::Function1(LPCTSTR sAppName, HKEY hKey, LPCTSTR tcszValue1, LPCTSTR tcszValue2, LPCTSTR tcszValue3, BOOL bValue)
{
__int64 nAppId=0;
__int64 nId2=0;
sSql.Format(_T("INSERT INTO Table (AppId, Id2, RegPath, RegKey, RegValueName,
bRecurseDelete, RemoveIt) VALUES ('%d', '%d', '%s', '%s', '%s', '%d', 1)"),
nAppId, nId2, tcszValue1, tcszValue2, tcszValue3, bValue);
}
当我在 64 位编译它时它没有任何问题,在 32 位它在 sValue3 为空时崩溃(但不是第一次,在第 4 次调用 CString.Format 时 sValue 为空)
不好!不允许在 Format 语句中使用 CString 对象。始终使用 GetString!
CString sValue1 = tcszValue1;
CString sValue2 = tcszValue2;
CString sValue3 = tcszValue3;
sSql.Format(_T("INSERT INTO Table (AppId, Id2, RegPath, RegKey, RegValueName, bRecurseDelete, RemoveIt) VALUES ('%d', '%d', '%s', '%s', '%s', '%d', 1)"),
nAppId, nId2, sValue1.GetString(), sValue2.GetString(), sValue3.GetString(), bValue);
您必须使用 %lld
格式说明符而不是 %d
说明符。
在 32 位世界中,%d
需要一个 32 位整数。但是您提供 64 位整数作为参数。因此你会得到未定义的行为,因为 Format
会完全混淆参数。
我的 CString 格式导致 32 位 Unicode MFC 崩溃 Static/VS2013 SDK 文件中的项目 output.c 第 1629 行 while (i-- && *pwch)
bool MyClass::Function1(LPCTSTR sAppName, HKEY hKey, LPCTSTR tcszValue1, LPCTSTR tcszValue2, LPCTSTR tcszValue3, BOOL bValue)
{
__int64 nAppId=0;
__int64 nId2=0;
sSql.Format(_T("INSERT INTO Table (AppId, Id2, RegPath, RegKey, RegValueName,
bRecurseDelete, RemoveIt) VALUES ('%d', '%d', '%s', '%s', '%s', '%d', 1)"),
nAppId, nId2, tcszValue1, tcszValue2, tcszValue3, bValue);
}
当我在 64 位编译它时它没有任何问题,在 32 位它在 sValue3 为空时崩溃(但不是第一次,在第 4 次调用 CString.Format 时 sValue 为空)
不好!不允许在 Format 语句中使用 CString 对象。始终使用 GetString!
CString sValue1 = tcszValue1;
CString sValue2 = tcszValue2;
CString sValue3 = tcszValue3;
sSql.Format(_T("INSERT INTO Table (AppId, Id2, RegPath, RegKey, RegValueName, bRecurseDelete, RemoveIt) VALUES ('%d', '%d', '%s', '%s', '%s', '%d', 1)"),
nAppId, nId2, sValue1.GetString(), sValue2.GetString(), sValue3.GetString(), bValue);
您必须使用 %lld
格式说明符而不是 %d
说明符。
在 32 位世界中,%d
需要一个 32 位整数。但是您提供 64 位整数作为参数。因此你会得到未定义的行为,因为 Format
会完全混淆参数。