【问题标题】:C++ Win32 application continuously crashes when working with static text control. Can't figure out whyC++ Win32 应用程序在使用静态文本控件时不断崩溃。不知道为什么
【发布时间】:2012-07-20 07:40:42
【问题描述】:
SendMessage(hEditControl, WM_GETTEXT,255,(LPARAM)editbuffer);
 GetWindowText(hTextControl, (LPWSTR)allText,GetWindowTextLength(hTextControl));

//allText = appendStrings((char*)TEXT("whatever"), (char*)TEXT("whatever"));
SetWindowText(hTextControl, (LPCWSTR)allText);

//where editbuffer and allText are defined as:

    static WCHAR*       editbuffer;
    static WCHAR*       allText;

其中 hEditControl 是编辑控件的句柄,hTextControl 是静态文本控件的句柄。我想从编辑控件中获取数据并将其附加到静态控件文本中。但是我的程序在 getwindowtext 函数处崩溃,我不知道为什么。即使没有 appendstring 函数,它仍然会崩溃。顺便说一句,这段代码在我的主窗口的消息处理函数中。

【问题讨论】:

  • 您是否为editBufferallText 分配了空间?
  • 什么电话最好这样做
  • 至少对于editBuffer,您可以只使用堆栈上的数组。如果你想使用GetWindowTextLengthallText 仍然需要动态分配新的,甚至更好的智能指针。
  • 如果你把它作为答案,那就记错了

标签: c++ c winapi win32gui


【解决方案1】:

您的两个缓冲区没有为它们分配空间,因此写入它们的任何内容都是未定义的行为。您需要分配内存,最好为editBuffer 分配内存,为allText 分配动态内存,因为 C++ 不支持 VLA。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    相关资源
    最近更新 更多