簡體   English   中英

SetWindowSubclass中的msftedit.dll RichEdit控件出現錯誤“訪問沖突讀取位置0x00000008”

[英]Error “Access violation reading location 0x00000008” with msftedit.dll RichEdit control inside SetWindowSubclass

我在單獨的子類文件中有編輯控件。 在開始使用SetWindowSubclass函數之后(我是C ++的新手,以前我使用SetWindowLongPtr進行子類化,但效果很好,但是我被建議開始使用SetWindowSubclass),我遇到了這個問題:

編譯程序后,應用程序將繪制一個空窗口,該窗口立即停止響應(我必須通過任務管理器將其關閉)。 輸出窗口中的結果錯誤:

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函數上方的此位獲取錯誤(我沒有)

該問題與訪問NULL HWND句柄無關。 它與訪問NULL 對象指針有關 而且您的代碼中確實有對象指針,因此您需要確定哪個是NULL。 發生AccessViolation時,請使用調試器查看在內存地址0x635F3DEF上運行的代碼,這將使您進入代碼崩潰的那一行。

話雖如此, EditBoxProc()的最后一個參數需要是DWORD_PTR而不是DWORD ,並且您將wParamlParam值以錯誤的順序傳遞給DefSubclassProc()

同樣,您的WM_KILLFOCUSWM_PAINT處理程序不應調用SetFocus() ,並且您的WM_PAINT處理程序不應調用InvalidateRect()

我建議您重新編寫SearchEditBox類,以使其數據成員不再是static 在類中將它們設置為static可防止您使用單個HWND創建多個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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM