msftedit "Access violation reading location 0x00000008"错误.dll SetWindowSub类中的RichEdit控件

Error "Access violation reading location 0x00000008" with msftedit.dll RichEdit control inside SetWindowSubclass

本文关键字:SetWindowSub RichEdit 控件 dll 错误 violation Access reading location 0x00000008 msftedit      更新时间:2023-10-16

我在单独的子类文件中拥有编辑控件。在开始使用SetWindowSubclass函数后(我是C++的新手,以前我使用SetWindowLongPtr进行子类划分,它运行良好,但有人建议我开始使用SetWindowsSubclass),我遇到了这个问题:

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

TaskTracklist.exe中0x635F3DEF(msftedit.dll)处引发异常:0xC0000005:读取位置0x00000008时发生访问冲突。

完整代码:

#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);
}

我很清楚,问题出在带有SetWindowSubclass函数的RichEdit控件上。

尽管我在Msftedit.dll的连接中没有发现任何与此错误相关的主题,但我从这些文章(这里和这里)中了解到,我可能引用了NULL指针,但这对我来说没有意义,因为那样我会从SetWindowSubclass函数上方的这一位得到错误(我没有):

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

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

HWND newHWND = SearchEditBox::editBox;
SetWindowSubclass(newHWND, SearchEditBox::EditBoxProc, 0, 0);

使用函数getEditBox()也不能解决这个问题。

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

我还尝试在CreateEditBox中声明HWND-editBox(使其成为局部变量),但这也没有帮助。

我使用免费社区版本的VS.

编辑:从代码中清除了一些未成功的尝试。

TaskTracklist.exe中0x635F3DEF(msftedit.dll)处引发异常:0xC0000005:读取位置0x00000008时发生访问冲突。

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

尽管我在Msftedit.dll的连接中没有发现任何与此错误相关的主题,但我从这些文章(…)中了解到,我可能引用了NULL指针,但这对我来说没有意义,因为那样我会从SetWindowSubclass函数(我没有)上方的这一位得到错误

此问题与访问NULLHWND句柄无关。它与访问NULL对象指针有关。而且您的代码中确实有对象指针,所以您需要弄清楚哪一个是NULL。当发生AccessViolation时,请使用调试器查看在内存地址0x635F3DEF处运行的代码,这将导致代码中的哪一行崩溃。

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

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

我建议您重写SearchEditBox类,使其数据成员不再是static。在类中使它们成为static可以防止创建多个具有单个HWNDs的SearchEditBox对象。数据成员不需要是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);
}