GetWindowText的失败

时间:2017-05-10 20:45:31

标签: c++ visual-studio visual-studio-2017

我正在尝试学习如何使用visual studio为C ++制作GUI。但是在使用GetWindowText()函数时遇到了一些问题。它不会将LPTSTR标题更改为文本框中的文本,并且在调试时也会出现错误:“Win32Project1.exe中的0x74F8207D(user32.dll)抛出异常:0xC0000005:访问冲突写入位置0x002B8D38。”代码可以在下面看到我做错了什么?

#define ID_BUTTON1 1
#define ID_BUTTON2 2
#define ID_TEXT3 3
static HWND hWndTextbox;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
    HMENU hMenubar = CreateMenu();
    HMENU hFile = CreateMenu();
    HMENU hOptions = CreateMenu();

    AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFile, L"File");
    AppendMenu(hMenubar, MF_POPUP, NULL, L"Edit");
    AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hOptions, L"Options");

    AppendMenu(hFile, MF_STRING, NULL, L"Exit");
    AppendMenu(hOptions, MF_STRING, NULL, L"option 1");
    AppendMenu(hOptions, MF_STRING, NULL, L"option 2");
        SetMenu(hWnd, hMenubar);

    CreateWindow(TEXT("button"), TEXT("Hello"),
        WS_VISIBLE | WS_CHILD,
        10, 10, 80, 25,
        hWnd, (HMENU) ID_BUTTON1, NULL, NULL);

    static HWND hWndTextbox = CreateWindow(TEXT("edit"), TEXT("tekst goes here"),
        WS_VISIBLE | WS_CHILD|WS_BORDER | ES_AUTOHSCROLL,
        90, 120, 300, 20,
        hWnd, (HMENU) ID_TEXT3, NULL, NULL);

    CreateWindow(TEXT("button"), TEXT("shiny"),
        WS_VISIBLE | WS_CHILD,
        50, 50, 80, 50,
        hWnd, (HMENU) ID_BUTTON2, NULL, NULL);
}
break;
case WM_COMMAND:
    {
    if (LOWORD(wParam) == ID_BUTTON1) {
        MessageBox(hWnd, L"Button has been clicked", L"title for popup", MB_ICONINFORMATION);
    }
    if (LOWORD(wParam) == ID_BUTTON2) {
        // create some default vars
        // int length = GetWindowTextLength(hWndTextbox) + 1;
        LPTSTR title = L"test" ;

        GetWindowText(hWndTextbox, title, GetWindowTextLength(hWndTextbox) + 1);
        MessageBox(hWnd, title, L"Message box", MB_OK);
    }
    break;
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;
case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code that uses hdc here...
        EndPaint(hWnd, &ps);
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

其余的代码是由visual studio自动生成的,所以我只包含了我做出更改的部分。

1 个答案:

答案 0 :(得分:1)

GetWindowText()的文档声明参数lpString应该指向将接收文本"的缓冲区,这意味着可写缓冲。

使用代码LPTSTR title = L"test";,您可以创建指向字符串文字的指针,该字符串文字通常位于只读内存中。当GetWindowText()尝试写入该内存时,会导致访问冲突。

要解决此问题,请使用如下可写缓冲区:

// Allocate buffer including terminating null
std::wstring title( GetWindowTextLength(hWndTextbox) + 1, 0 );

// Address of first character is used to obtain pointer to non-const data
// (as opposed to wstring::c_str()).
int size = GetWindowText( hWndTextbox, &title[0], title.size() );

// Resize buffer to the actual text length
title.resize( size );

// MessageBox only needs pointer to const string, so we can use wstring::c_str() here.
MessageBox(hWnd, title.c_str(), L"Message box", MB_OK);