gpt4 book ai didi

c++ - SetWindowSubclass 内的 msftedit.dll RichEdit 控件出现错误 "Access violation reading location 0x00000008"

转载 作者:行者123 更新时间:2023-11-28 00:03:13 25 4
gpt4 key购买 nike

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

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

Exception thrown at 0x635F3DEF (msftedit.dll) in TaskTracklist.exe: 0xC0000005: Access violation reading location 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 相关的错误特别相关的主题,但我从这些文章(hereherehere)中了解到我可能引用了 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 的免费社区版。

编辑:从代码中清除了我不成功尝试的一些遗留问题。

最佳答案

Exception thrown at 0x635F3DEF (msftedit.dll) in TaskTracklist.exe: 0xC0000005: Access violation reading location 0x00000008.

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

Even though I did not find any topic related specifically to this error in connection to Msftedit.dll, I understood from these articles (...) that I may be referencing to NULL pointer, but that does not make sense to me because then I would get error from this bit just above SetWindowSubclass function (which I don't)

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

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

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

我建议您重写您的 SearchEditBox 类,使其数据成员不再是 static。在类中将它们设置为 static 可防止您创建多个具有单个 HWNDSearchEditBox 对象。数据成员不需要是static。您可以将 SearchEditBox* 指针作为子类的 dwRefData 参数传递,这样 EditBoxProc() 就可以访问 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

搜索编辑框.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);
}

关于c++ - SetWindowSubclass 内的 msftedit.dll RichEdit 控件出现错误 "Access violation reading location 0x00000008",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37357022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com