错误"访问冲突读取位置0x00000008"在SetWindowSubclass中使用msftedit.dll RichEdit控件

时间:2016-05-20 22:56:08

标签: c++ winapi visual-studio-2015

我在单独的子类文件中编辑控件。上班后开始使用SetWindowSubclass函数(我是C ++的新手,之前我使用SetWindowLongPtr进行子类化,但是我得到了建议,开始使用SetWindowSubclass),我遇到了这个问题:

编译程序后,应用程序绘制空窗口,立即停止响应(我必须通过任务管理器关闭它)。 输出窗口中的结果错误:

  

在TaskTracklist.exe中的0x635F3DEF(msftedit.dll)抛出异常:   0xC0000005:访问冲突读取位置0x00000008。

整个代码:

var cropArray = ["Berries - Blueberries", "Peas", "Squash", "Okra"];
cropArray.sort();

我很清楚,问题在于使用SetEindowSubclass函数进行RichEdit控件。

即使我没有找到与Msftedit.dll相关的此错误专门相关的任何主题,我从这些文章(hereherehere)中了解到我可能引用NULL指针,但这对我没有意义,因为那时我会从SetWindowSubclass函数上面得到错误(我不会这样做):

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <string.h>
#include "SearchEditBox.h"

/*global vars*/
LPCWSTR SearchEditBox::editBoxDefText = L"Search...";
bool SearchEditBox::firstLoad = false;
int SearchEditBox::width = 0;
int SearchEditBox::height = 0;
HWND SearchEditBox::editBox;

/*functions*/
LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uint_ptr, DWORD dwRefData);
void ChangeEdit(const HWND hwnd, const bool change);

/*set-get functions*/
HWND SearchEditBox::getEditBox()
{
    return SearchEditBox::editBox;
}

SearchEditBox * SearchEditBox::CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height) {
    SearchEditBox * p_SearchEditBox = new SearchEditBox;

    LoadLibrary(TEXT("Msftedit.dll")); //enables RichEdit field
    SearchEditBox::editBox = CreateWindowEx(0, (L"RICHEDIT50W"), editBoxDefText, WS_VISIBLE | WS_CHILD, pos_x + 6, pos_y + 4, width, height, hwnd, NULL, (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), p_SearchEditBox);
    SearchEditBox::width = width;
    SearchEditBox::height = height;

    if (SearchEditBox::editBox == NULL)
    {
        delete p_SearchEditBox;
        MessageBox(NULL, L"Problem creating the Search box.", L"Error", 0);
        return 0;
    }

    SetWindowSubclass(SearchEditBox::editBox, SearchEditBox::EditBoxProc, 0, 0);

    return p_SearchEditBox;
}

LRESULT CALLBACK SearchEditBox::EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD dwRefData)
{
    PAINTSTRUCT ps;
    HDC hdc;
    switch (uMsg)
    {
        case WM_SETFOCUS:
        {
            LPWSTR getText = new TCHAR[10];
            GetWindowText(hwnd, getText, 10);
            if (_tcscmp(getText, editBoxDefText) == 0)
            {
                SearchEditBox::ChangeEdit(hwnd, 1);
            }
            delete getText;
            break;
        }
        case WM_KILLFOCUS:
        {
            LPWSTR getText = new TCHAR[10];
            if (GetWindowText(hwnd, getText, 10) == 0)
            {
                SearchEditBox::ChangeEdit(hwnd, 0);
            }
            ::SetFocus(NULL);
            delete getText;
            break;
        }
        case WM_LBUTTONDBLCLK:
            SearchEditBox::ChangeEdit(hwnd, 1);
            break;
        case WM_PAINT:
        {
            hdc = BeginPaint(hwnd, &ps);

            RECT rc;
            InvalidateRect(hwnd, &rc, 0);
            if (SearchEditBox::firstLoad == false)
            {
                SearchEditBox::ChangeEdit(hwnd, 0);
                ::SetFocus(NULL);
                SearchEditBox::firstLoad = true;
            }
            HBRUSH hBrush = CreateSolidBrush(RGB(33, 33, 33));
            SelectObject(hdc, hBrush);
            RoundRect(hdc, -6, -4, SearchEditBox::width + 7, SearchEditBox::height + 4, 5, 5);
            EndPaint(hwnd, &ps);
            break;
        }
    }
    return DefSubclassProc(hwnd, uMsg, lParam, wParam);
}

我还认为问题可能是外部函数(意思是comctl32中定义的函数而不是类内部)访问类特定变量(指针为NULL,但它可能无法访问以某种方式到函数)所以我尝试为SetWindowSubclass函数创建局部变量(它也不起作用):

    if (SearchEditBox::editBox == NULL)
    {
        delete p_SearchEditBox;
        MessageBox(NULL, L"Problem creating the Search box.", L"Error", 0);
        return 0;
    }

使用函数getEditBox()也没有解决这个问题。

我也尝试在控制声明中使用MSFTEDIT_CLASS而不是(L&#34; RICHEDIT50W&#34;)(认为问题可能在控件本身内部)但是这也没有帮助,并尝试不同的版本(例如RICHEDIT51W或RICHEDIT80W)导致错误(因此我必须使用VS2015的正确RichEdit版本。)

我还尝试在CreateEditBox中声明HWND editBox(使其成为局部变量),但这也无济于事。

我使用免费的社区版VS.

编辑:从代码中清除了我未成功尝试的遗留物。

1 个答案:

答案 0 :(得分:3)

  

在TaskTracklist.exe中0x635F3DEF(msftedit.dll)抛出异常:0xC0000005:访问冲突读取位置0x00000008。

地址0附近的访问冲突通常意味着通过NULL对象指针访问类/记录数据成员。

  

即使我没有找到与Msftedit.dll相关的任何专门针对此错误的主题,我从这些文章(...)中了解到我可能引用了NULL指针,但这对我来说没有意义因为那时我会从SetWindowSubclass函数上面得到错误(我不会这样做)

问题与访问NULL HWND句柄无关。它与访问NULL 对象指针有关。并且您的代码中确实有对象指针,因此您需要确定哪一个是NULL。发生AccessViolation时,使用调试器查看在内存地址0x635F3DEF上运行的代码,这将导致代码中的哪一行崩溃。

话虽如此,EditBoxProc()的最后一个参数需要是DWORD_PTR而不是DWORD,而您传递的是wParamlParam错误顺序的DefSubclassProc()值。

此外,您的WM_KILLFOCUSWM_PAINT处理程序不应该调用SetFocus(),并且您的WM_PAINT处理程序不应该调用InvalidateRect()

我建议您重新编写SearchEditBox课程,以使其数据成员不再是static。在课程中设置static会阻止您使用单个SearchEditBox创建多个HWND个对象。数据成员不必是static。您可以将SearchEditBox*指针作为子类的dwRefData参数传递,以便EditBoxProc()可以访问SearchEditBox对象及其非静态成员。

尝试更像这样的事情:

SearchEditBox.h

#ifndef SearchEditBoxH
#define SearchEditBoxH

class SearchEditBox
{
private:
    bool firstPaint;
    int width;
    int height;
    HWND editBox;

    SearchEditBox();
    void ChangeEdit(const bool change);

    static LRESULT CALLBACK EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uint_ptr, DWORD_PTR dwRefData);

public:
    ~SearchEditBox();

    HWND getEditBox()

    static SearchEditBox* CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height);
};

#endif

SearchEditBox.cpp

#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include <string.h>
#include "SearchEditBox.h"

/*global vars*/
static LPCTSTR editBoxDefText = TEXT("Search...");

SearchEditBox::SearchEditBox()
{
    firstPaint = false;
    width = 0;
    height = 0;
    editBox = NULL;
}

SearchEditBox::~SearchEditBox()
{
    if (editBox)
        DestroyWindow(editBox);
}

void SearchEditBox::ChangeEdit(const bool change)
{
    //...
}

HWND SearchEditBox::getEditBox()
{
    return editBox;
}

SearchEditBox* SearchEditBox::CreateEditBox(HINSTANCE hInst, HWND hwnd, int pos_x, int pos_y, int width, int height)
{
    // make sure RichEdit 4.1 is enabled
    if (!GetModuleHandle(TEXT("Msftedit.dll")))
    {
        if (!LoadLibrary(TEXT("Msftedit.dll")))
        {
            MessageBox(NULL, L"Problem loading Msftedit.dll.", L"Error", 0);
            return 0;
        }
    }

    SearchEditBox *pSearchEditBox = new SearchEditBox;

    pSearchEditBox->editBox = CreateWindowEx(0, MSFTEDIT_CLASS, editBoxDefText, WS_VISIBLE | WS_CHILD, pos_x + 6, pos_y + 4, width, height, hwnd, NULL, (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE), p_SearchEditBox);
    if (!pSearchEditBox->editBox)
    {
        delete pSearchEditBox;
        MessageBox(NULL, L"Problem creating the Search box.", L"Error", 0);
        return 0;
    }

    if (!SetWindowSubclass(pSearchEditBox->editBox, SearchEditBox::EditBoxProc, 0, (DWORD_PTR)p_SearchEditBox))
    {
        delete pSearchEditBox;
        MessageBox(NULL, L"Problem subclassing the Search box.", L"Error", 0);
        return 0;
    }

    pSearchEditBox->width = width;
    pSearchEditBox->height = height;

    return pSearchEditBox;
}

LRESULT CALLBACK SearchEditBox::EditBoxProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
    SearchEditBox *pSearchEditBox = (SearchEditBox*) dwRefData;

    switch (uMsg)
    {
        case WM_NCDESTROY:
            RemoveWindowSubclass(hwnd, SearchEditBox::EditBoxProc, uIdSubclass);
            pSearchEditBox->editBox = NULL;
            break;

        case WM_SIZE:
            pSearchEditBox->width = LOWORD(lParam);
            pSearchEditBox->height = HIWORD(lParam);
            break;

        case WM_SETFOCUS:
        {
            TCHAR getText[10];
            GetWindowText(hwnd, getText, 10);

            if (_tcscmp(getText, editBoxDefText) == 0)
                pSearchEditBox->ChangeEdit(true);

            break;
        }

        case WM_KILLFOCUS:
        {
            TCHAR getText[10];
            if (GetWindowText(hwnd, getText, 10) == 0)
                pSearchEditBox->ChangeEdit(false);
            break;
        }

        case WM_LBUTTONDBLCLK:
            pSearchEditBox->ChangeEdit(true);
            break;

        case WM_PAINT:
        {
            if (!pSearchEditBox->firstPaint)
            {
                pSearchEditBox->firstPaint = true;
                pSearchEditBox->ChangeEdit(false);
            }

            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            HBRUSH hBrush = CreateSolidBrush(RGB(33, 33, 33));
            SelectObject(hdc, hBrush);
            RoundRect(hdc, -6, -4, pSearchEditBox->width + 7, pSearchEditBox->height + 4, 5, 5);
            EndPaint(hwnd, &ps);

            break;
        }
    }

    return DefSubclassProc(hwnd, uMsg, wParam, lParam);
}