LNK2001 in “How can I make a WNDPROC or DLGPROC a member of my C++ class?”

a 夏天 提交于 2020-01-07 02:43:11

问题


VS10: MCBS: Hi there, In the light of this discussion encountered a problem attempting a Hello World implementation of Raymond Chen's method in How can I make a WNDPROC or DLGPROC a member of my C++ class? using Pudeyev's code for "Hello World":

error LNK2001: unresolved external symbol "private: long __thiscall
BaseWnd::WndProc(struct HWND__ *,unsigned int,unsigned int,long)" 
(?WndProc@BaseWnd@@AAEJPAUHWND__@@IIJ@Z)

The code is as follows:

// ...Written by Oleg Pudeyev, (c) 2003
#include <windows.h>
HINSTANCE appHinst;

class BaseWnd
{
public:
BaseWnd();

// This is the static callback that we register
static LRESULT CALLBACK s_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

// The static callback recovers the "this" pointer and then calls this member function.
LRESULT BaseWnd::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

};

BaseWnd::BaseWnd(void)
{
    WNDCLASSW wc = {
        // Request redraws as we're centering the string
        CS_HREDRAW|CS_VREDRAW,
        BaseWnd::s_WndProc,
        // No per-class data
        0,
        // No per-window data
        0,
        appHinst,
        LoadIcon(0, IDI_APPLICATION),
        LoadCursor(0, IDC_ARROW),
        HBRUSH(COLOR_BACKGROUND),
        0,
        L"BaseWnd"
    };
RegisterClassW(&wc);
HWND hwnd = CreateWindowW(L"BaseWnd", L"Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, appHinst, this);
ShowWindow(hwnd, SW_SHOW);
}

LRESULT BaseWnd::WndProc(HWND hwnd, UINT Umsg, WPARAM wParam, LPARAM lParam)
{
switch (Umsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hwnd, &ps);
RECT r;
GetClientRect(hwnd, &r);
SetBkMode(ps.hdc, TRANSPARENT);
DrawTextW(ps.hdc, L"Hello, World!", -1, &r, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
EndPaint(hwnd, &ps);
break;

default:
// Use default handling for messages we don't process
return DefWindowProc(hwnd, Umsg, wParam, lParam);
}
return 0;
}

LRESULT CALLBACK BaseWnd::s_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
BaseWnd *pThis; // our "this" pointer will go here

if (uMsg == WM_NCCREATE) {
// Recover the "this" pointer which was passed as a parameter to CreateWindow(Ex).
LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
pThis = static_cast<BaseWnd*>(lpcs->lpCreateParams);
// Put the value in a safe place for future use
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
} else {
// Recover the "this" pointer from where our WM_NCCREATE handler stashed it.
pThis = reinterpret_cast<BaseWnd*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}

if (pThis) {
// Now that we have recovered our "this" pointer, let the member function finish the job.
//Removal of this line removes the LNK2001
return pThis->WndProc(hwnd, uMsg, wParam, lParam);
}

// "this" pointer unknown, so just do the default thing. Hopefully, didn't need to customize behavior yet.
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprev, LPTSTR cmdline, int showcmd)
{
appHinst = hinst;
BaseWnd p;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}

Is there something wrong with either how the BaseWnd constructor is invoked or the class setup - or perhaps, viz the date on Raymond's article, a revision of c++11 that cannot cope?

Edit: Replaced WndProc with absolute qualifier BaseWnd::WndProc and made the offending static callback a callback. Great, the other issues with the code we can work with.
Edit2: The final piece of the puzzle was returning Pudeyev's original CS_HREDRAW|CS_VREDRAW to the WNDCLASS structure.


回答1:


Apparently I think a definition of scope is none.

LRESULT CALLBACK WndProc
{
 ↓
LRESULT CALLBACK BaseWnd::WndProc
{



回答2:


The error should be self-explanatory. BaseWnd::WndProc is declared, but not defined. See @nariuji

Also BaseWnd needs a public constructor because you are going to call it later:

class BaseWnd
{
//******* make this public:
public:
BaseWnd();
...
};

WNDCLASSW wc has to be initialized to zero:

WNDCLASSW wc = { 0 };

When you create the window it is invisible. You have to make it visible with ShowWindow or change the style to WS_VISIBLE | WS_OVERLAPPEDWINDOW

HWND hwnd = CreateWindowW(L"BaseWnd", L"Hello, World!", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, appHinst, this);
ShowWindow(hwnd, SW_SHOW); //*** add this

Declaring a pointer doesn't do anything. You also need a message loop:

WinMain(...)
{
    //BaseWnd *pthis; //***remove this line
    BaseWnd wnd;//***add this line
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    ...
}


来源:https://stackoverflow.com/questions/36111009/lnk2001-in-how-can-i-make-a-wndproc-or-dlgproc-a-member-of-my-c-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!