1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <Windows.h>
#define WINDOW_NAME L"Application"
#define WINDOW_CLASSNAME L"WindowClass1"
LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
class CTestClass
{
private:
int Number;
public:
CTestClass();
virtual ~CTestClass();
void TestFunction();
};
CTestClass::CTestClass()
{
Number = 100;
}
CTestClass::~CTestClass()
{
}
void CTestClass::TestFunction()
{
if (Number == 100) // Access violation here, after calling this function from WindowProc
{
MessageBox(NULL, L"Test", NULL, NULL);
}
}
CTestClass* g_TestClass;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS WindowClass;
ZeroMemory(&WindowClass, sizeof(WNDCLASS));
WindowClass.style = CS_DBLCLKS;
WindowClass.lpfnWndProc = WindowProc;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.hInstance = hInstance;
WindowClass.hIcon = NULL;
WindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WindowClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WindowClass.lpszMenuName = NULL;
WindowClass.lpszClassName = WINDOW_CLASSNAME;
RegisterClass(&WindowClass);
HWND hWnd;
hWnd = CreateWindow(WINDOW_CLASSNAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
10, 10, 800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
g_TestClass = new CTestClass(); // Initialize the Test Class
MSG Message;
Message.message = NULL;
while (Message.message != WM_QUIT)
{
if (PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
delete g_TestClass;
return Message.wParam;
}
LRESULT WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOVE:
g_TestClass->TestFunction(); // Call the Test Function --> Leads to Access Violation
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
| |