Access violation reading location using WndClass - c++

I have one problem, using this code:
BOOL RegisterApp(HINSTANCE hInst)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.lpszClassName = szClassName;
//
//
return RegisterClass(&wc);
}
When i'm trying using &wc, i have this error:
Unhandled exception at 0x763adf81 in lab3.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Please help me, what i need to do with this?

You have not initialized all of the data members of wc.
Refer http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx

Related

How to tell which parameters are required and which are not? (visual c++)

I am following an MSDN tutorial on Visual C++ on creating windows. It's using this code to register a window class.
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
When I look up the WNDCLASS structure on MSDN: WNDCLASS Structure it gives this implementation:
typedef struct tagWNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS, *PWNDCLASS;
How can you tell by the documentation that you only need these three parameters?
lpfnWndProc
hInstance
lpszClassName
All parameters are required, but most of them can be set to default values.
WNDCLASS wc;
wc.style = CS_BYTEALIGNWINDOW | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = DefWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetModuleHandle(NULL);
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = 0;
wc.lpszClassName = "MYCLASS";

Create Child window on windows API

I'm trying to add a child window to my main window, but the function CreatWindow is throwing an exception saying that can't access address at 0x00000, but it works fine when I try to create a button, I tracked the variables and none of them are null, here it is:
WNDCLASSEX windowClass;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = NULL;
windowClass.lpszClassName = className;
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.lpszMenuName = NULL;
if (!RegisterClassEx(&windowClass)){
return;
}
window = CreateWindowEx(0,
className,
(LPCTSTR)NULL,
WS_CHILD | WS_BORDER,
0, 0, 0, 0,
owner,
(HMENU)ID,
hInstance,
NULL);
the code above giving the error, the important variables come from here:
gl = new OpenGLContainer("hellogl", hInstance);
addChild(gl);
the first parameter is the className, the constructor only performs an attribution, the addChild method call gl->setOwner(window_handler) and gl->create() which is the first piece of code I posted.
I also saw the stack list, and the problem is after the program enter in the CreateWindow function, which is very strange because the debugger shows that none of the values(pointers) are null.
Could it be failing because lpfnWndProc is NULL?

Can't load ICON resource in VC++ 2012 Ultimate

I am having problem adding icon resource to C++ Win32 Project. I followed the steps in pictures down under but we I run program.exe there is no my icon in menu bar (only default icon). *.exe file has new icon as it should.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
LPCWSTR className = L"MyWindow";
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = className;
wc.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE( IDI_ICON1));
if(!RegisterClassEx(&wc))
{
MessageBox(NULL,L"Error, registration of class faild", L"ERROR", MB_OK);
return 0;
}
Your window style is 0.
Try:
wc.style = WS_SYSMENU;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

Why am I getting error code 87 (Invalid parameter) from RegisterClassEx?

Here's the code
#include <windows.h>
const wchar_t g_szClassName[] = L"myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{/*...*/
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window...
return Msg.wParam;
}
This code is straight from Forgers Win32 Tutorial (with L"" and wchar_t where needed). Yet I can't make it work on WinXP SP3 x86 and VC2008Express SP1.
You didn't set style member, for instance (taken from wizard created code):
wc.style = CS_HREDRAW | CS_VREDRAW;
After the declaration of WNDCLASSEX wc; the memory will just contain random values.
You can do this
WNDCLASSEX wc = { 0 };
or you can do that
ZeroMemory(&wc, sizeof(WNDCLASSEX));
to ensure that every field is set to at least zero.

No icon in title bar after switching from WNDCLASS to WNDCLASSEX

I just switched the game to use WNDCLASSEX so I can change the hIconSm, but for some reason there isn't an icon in the title bar anymore. The cursor and icon on the task bar and the icon for the EXE are all working fine though. Here is the main part of the window creation code:
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WinProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_ERROR);
wc.hIconSm = LoadIcon(NULL, IDI_ASTERISK);
wc.hCursor = LoadCursorFromFile((LPCSTR)"FierceCursor.cur");
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW+1);
wc.lpszClassName = "FierceWindow";
RegisterClassEx(&wc);
game->hinstance = hInstance;
hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
"FierceWindow", "Fierce",
WS_OVERLAPPED, 400, 100,
game->SCREEN_WIDTH, game->SCREEN_HEIGHT,
NULL, NULL, hInstance, NULL);
I've tried a few different settings for CreateWindowEx in hopes that maybe I used a style without an icon, but no luck. I'm using default icons right now to test.
Won't compile:
error: cast from 'CHAR*' to 'WORD' loses precision
Thanks for the edit btw.
Edit - Got it to compile by changing it to:
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(32513));
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(32513));
It's because of your window style. The icon appears only if you add WS_SYSMENU style.
WS_OVERLAPPED|WS_SYSMENU