我尝试使用 Visual Studio 2013 为 x64 构建我的 Win32 API 项目。但是路由的 WindowProc 回调无法正常工作。我正在使用 SetWindowLongPtr/GetWindowLongPtr 和 GWLP_USERDATA 来存储我的窗口的 this 指针。过去,我为此目的使用了 SetWindowLong/GetWindowLong 和 GWL_USERDATA - 但这些在 x64 上都没有了。但是在 x86 上一切仍然正常(即使使用 SetWindowLongPtr/GetWindowLongPtr 和 GWLP_USERDATA),但在 x64 上,只要我尝试访问成员函数 WindowProc 中的任何方法/成员,就会出现访问冲突。
#include <windows.h>
#include <stdio.h>
#include "main.h"
class Window{
public:
Window(const char* title, const float width, const float height){
char windowClass[255];
sprintf_s(windowClass, "WindowClass%s", title);
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProcRouter;
wc.hInstance = nullptr;
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = windowClass;
RegisterClassEx(&wc);
DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom = (long)height;
AdjustWindowRect(&WindowRect, dwStyle, FALSE);
hWnd = CreateWindowEx(0,
windowClass,
title,
dwStyle,
0, 0,
WindowRect.right - WindowRect.left,
WindowRect.bottom - WindowRect.top,
nullptr,
nullptr,
wc.hInstance,
(LPVOID) this);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
ShowWindow(hWnd, SW_SHOW);
SetFocus(hWnd);
closed = false;
}
static LRESULT CALLBACK Window::WindowProcRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
LRESULT returnValue = 0;
Window* pWnd = nullptr;
if (uMsg == WM_NCCREATE){
SetWindowLongPtr(hWnd, GWLP_USERDATA,
(long)((LPCREATESTRUCT(lParam))->lpCreateParams));
}
pWnd = (Window*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
if (pWnd){
returnValue = pWnd->WindowProc(hWnd, uMsg, wParam, lParam);
}
else{
returnValue = DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return returnValue;
}
LRESULT CALLBACK Window::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg){
case WM_DESTROY:
closed = true;
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
bool Window::isClosed(){
return closed;
}
Window::~Window(){
if (hWnd && !DestroyWindow(hWnd)){
hWnd = nullptr;
}
}
private:
HWND hWnd;
bool closed;
};
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
Window win("Title", 640, 480);
MSG msg;
while(!win.isClosed()){
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
在线closed = true;
上WindowProc
发生违规。任何想法为什么?