2

这是正在发生的事情。当我尝试从我的 CDialog 扩展类运行 AfxMessageBox 时,我得到一个错误(见下文)。我用谷歌搜索了互联网,但没有找到。这是消息框唯一失败的地方,我知道其余代码有效(我逐步完成了它)。

有谁知道如何解决这一问题?

提前致谢!

AFXMESSAGEBOX 打开时的错误消息:

IsoPro.exe 中 0x014b4b70 处的未处理异常:0xC0000005:访问冲突读取位置 0x34333345。

从 CDialog 中启动 AfxMessageBox 的代码

LPTSTR temp;
mainPassword.GetWindowText((LPTSTR)temp,100);
CString cstr;
cstr.Format("mainPassword = %s",temp);
AfxMessageBox(cstr);

显示 CDialog 的代码:

CEnterpriseManagementDialog* emd = new CEnterpriseManagementDialog();
emd->Create(IDD_ENTERPRISE_MANAGEMENT_DIALOG);
emd->ShowWindow(SW_SHOW);
4

2 回答 2

3

问题是你如何使用GetWindowText

LPTSTR temp;
mainPassword.GetWindowText((LPTSTR)temp,100);

您正在尝试通过未初始化的指针GetWindowText写入一些未分配的内存。temp如果您真的想使用原始输出缓冲区,则应在将指针传递给之前为其分配空间,例如:GetWindowText

TCHAR temp[100];
mainPassword.GetWindowText(temp, _countof(temp));
// NOTE: No need to LPTSTR-cast

但是,由于您使用的是 C++,您可能只想使用类似的字符串类CString而不是原始缓冲区,例如:

CString password;
mainPassword.GetWindowText(password);

CString msg;
msg.Format(_T("mainPassword = %s"), password.GetString());
// or you can just concatenate CStrings using operator+ ... 
AfxMessageBox(msg);
于 2016-11-23T19:56:20.247 回答
1

看起来变量 temp 是一个未初始化的指针(LPTSTR 的定义是一个 char *)。

尝试将 temp 定义为数组:

TCHAR temp[64];
于 2016-11-23T19:55:10.313 回答