【发布时间】:2021-11-07 06:37:10
【问题描述】:
我有这个代码:
GetParent()->SendMessage(UWM_DELETE_NAME_HISTORY_MSG, (WPARAM)strName.GetBufferSetLength(_MAX_PATH));
strName.ReleaseBuffer();
这样更改对我来说安全吗:
GetParent()->SendMessage(UWM_DELETE_NAME_HISTORY_MSG, (WPARAM)strName.GetString());
与此相关,使用static_cast<WPARAM>(strName.GetString())是否正确?
为了完成,这是我的自定义消息处理程序:
LRESULT CChristianLifeMinistryEditorDlg::OnDeleteNameHistory(WPARAM wParam, LPARAM lParam)
{
auto szName = (LPCTSTR)wParam;
m_History.erase(szName);
for (auto& kv : m_mapWeekendHistData)
kv.second.erase(szName);
return 0;
}
【问题讨论】:
-
这取决于 UWM_DELETE_NAME_HISTORY_MSG 的消息处理程序的作用。
-
标题说PostMessage,问题问SendMessage,下定决心,Send safe时Post可能不安全
-
static_cast<WPARAM>(strName.GetString())不会编译。您需要reinterpret_cast才能从指针转到 WPARAM。是的,您需要对标准 Windows 消息执行大量reinterpret_cast<LPARAM>(string)处理,这些消息将char*作为 Lparam。
标签: visual-c++ casting mfc c-strings postmessage