Hi,
I know this code is huge but can I know why there is a runtime error, and with the help of a debugger , the problem is around the underlined section(In the Toolbar function(CreateToolbarEx) in Engine.cpp).
Any comments about a different style of doing it is appreciated.
Code:
main.cpp:
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
|
#include <Windows.h>
#include <CommCtrl.h>
#include "engine.h"
#include "resource.h"
#pragma comment(lib,"comctl32.lib")
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_CLOSE:
if(MessageBox(hwnd,L"Are you sure you want to quit this app?\n\tYes/No",L"Warning...",
MB_ICONWARNING|MB_YESNO|MB_DEFBUTTON2)==IDYES)
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR pCmdLine,int nCmdShow)
{
int test;
BASE* mainwindow=new EXTRA;
test=mainwindow->Fill(hInstance,WindowProc,L"OOP-2",NULL,CS_VREDRAW|CS_HREDRAW,
MAKEINTRESOURCE(IDC_FREEWRITE),MAKEINTRESOURCE(IDI_ICON));
if(test==1)
{
MessageBox(NULL,L"Initialization of window failed!",NULL,NULL);
return 0;
}
test=mainwindow->Register();
if(test==1)
{
MessageBox(NULL,L"Registration of window failed!",NULL,NULL);
return 0;
}
test=mainwindow->Create(hInstance);
if(test==1)
{
MessageBox(NULL,L"Creation of window failed!",NULL,NULL);
return 0;
}
test=mainwindow->MakeToolbar(hInstance);
if(test==1)
{
MessageBox(NULL,L"Creation of the main toolbar failed!",NULL,NULL);
return 0;
}
test=mainwindow->Show();
if(test==1)
{
MessageBox(NULL,L"Showing of window failed!",NULL,NULL);
return 0;
}
test=mainwindow->Update();
if(test==1)
{
MessageBox(NULL,L"Painting of window failed!",NULL,NULL);
return 0;
}
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.lParam;
}
|
Engine.cpp:
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
#include "engine.h"
//------------Implementations-of-the-BASE-class---------------------------------------------
BASE::BASE(int show)
{
how_to_show=show;
}
int BASE::Fill(HINSTANCE inst,WNDPROC WindowProceedure,
LPCTSTR clsname,LPCTSTR menuname,UINT clsstyle,
LPCTSTR idc,LPCTSTR idi)
{
wcex.cbClsExtra=0;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.cbWndExtra=0;
wcex.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
wcex.hCursor=(HCURSOR)LoadImage(inst,MAKEINTRESOURCE(idc),IMAGE_CURSOR,16,16,LR_DEFAULTSIZE);
wcex.hIcon=(HICON)LoadImage(inst,MAKEINTRESOURCE(idi),IMAGE_ICON,32,32,LR_DEFAULTSIZE);
wcex.hIconSm=(HICON)LoadImage(inst,MAKEINTRESOURCEW(idi),IMAGE_ICON,16,16,LR_DEFAULTSIZE);
wcex.hInstance=inst;
wcex.lpfnWndProc=WindowProceedure;
wcex.lpszClassName=clsname;
wcex.lpszMenuName=menuname;
wcex.style=clsstyle;
if(wcex.cbSize!=0)
return 0;
else
return 1;
}
int BASE::Register()
{
ATOM temp=NULL;
temp=RegisterClassEx(&wcex);
if(temp!=NULL)
return 0;
else
return 1;
}
int BASE::Create(HINSTANCE inst,
HMENU hMenu,HWND hParent,
LPCWSTR clsname,LPCWSTR wndname,
DWORD wndstyle,DWORD exwndstyle,
int posx,int posy,int wid,int hei)
{
mhwnd=0;
mhwnd=CreateWindowExW(exwndstyle,
clsname,
wndname,
wndstyle,
posx,
posy,
wid,
hei,
hParent,
hMenu,
inst,
NULL);
if(mhwnd==0)
return 1;
else
return 0;
}
int BASE::Show()
{
BOOL temp=0;
temp=ShowWindow(mhwnd,how_to_show);
if(temp==0)
return 0;
else
return 1;
}
int BASE::Update()
{
BOOL temp=0;
temp=UpdateWindow(mhwnd);
if(temp==0)
return 1;
else
return 0;
}
int EXTRA::Toolbar(HINSTANCE instance,int idt)
{
iccex.dwICC=ICC_BAR_CLASSES;
iccex.dwSize=sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&iccex);
const int no_of_buttons=7;
hwndToolbar=NULL;
TBBUTTON tbbuttons[no_of_buttons];
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=0;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=1;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=2;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=3;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_SEP;
tbbuttons[0].iBitmap=0;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=4;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
tbbuttons[0].dwData=0L;
tbbuttons[0].fsState=TBSTATE_ENABLED;
tbbuttons[0].fsStyle=TBSTYLE_BUTTON;
tbbuttons[0].iBitmap=5;
tbbuttons[0].idCommand=0;
tbbuttons[0].iString=0;
hwndToolbar=CreateToolbarEx(mhwnd,
WS_CHILD|WS_VISIBLE|WS_BORDER,
idt,
no_of_buttons,
instance,
idt,
tbbuttons,
no_of_buttons,
16,16,16,16,
sizeof(TBBUTTON));
if(hwndToolbar!=NULL)
return 0;
else
return 1;
}
|
Engine.h:
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
|
#include <Windows.h>
#include <CommCtrl.h>
#include "resource.h"
class BASE
{
public:
BASE(int show=SW_SHOWNORMAL);
int Fill(HINSTANCE inst,WNDPROC WindowProceedure,
LPCTSTR clsname=L"OOP-2",LPCTSTR menuname=NULL,UINT clsstyle=CS_VREDRAW|CS_HREDRAW,
LPCTSTR idc=IDC_ARROW,LPCTSTR idi=IDI_APPLICATION);
int Register();
int Create(HINSTANCE inst,
HMENU hMenu=NULL,HWND hParent=NULL,
LPCWSTR clsname=L"OOP-2",LPCWSTR wndname=L"Object Oriented Programming-2",
DWORD wndstyle=WS_OVERLAPPEDWINDOW,DWORD exwndstyle=WS_EX_OVERLAPPEDWINDOW,
int posx=CW_USEDEFAULT,int posy=CW_USEDEFAULT,int wid=CW_USEDEFAULT,int hei=CW_USEDEFAULT);
int Show();
int Update();
int MakeToolbar(HINSTANCE instance,int idt=IDT_TOOLBAR)
{
int temp;
temp=this->Toolbar(instance,idt);
return temp;
}
protected:
WNDCLASSEX wcex;
int how_to_show;
HWND mhwnd;
private:
virtual int Toolbar(HINSTANCE,int)=0;
};
class EXTRA:public BASE
{
public:
int Toolbar(HINSTANCE instance,int idt);
private:
INITCOMMONCONTROLSEX iccex;
HWND hwndToolbar;
};
|
For more info: Unhandled exception at 0x560b54e8 in OOP-2.exe: 0xC0000005: Access violation reading location 0xcccccccc.
This is the debugging error.
|