Error when initializing window

Started by
5 comments, last by Samsonite 18 years, 9 months ago
Hello! I have some problems compiling my DX window in Visual Studio 2005 Beta 2:

------ Build started: Project: example1, Configuration: Debug Win32 ------
Compiling...
winmain.cpp
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(12) : error C2065: 'WndProc' : undeclared identifier
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(20) : error C2440: '=' : cannot convert from 'const char [15]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(24) : error C2065: 'wndHandle' : undeclared identifier
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(24) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [15]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(34) : error C2365: 'WndProc' : redefinition; previous definition was 'formerly unknown identifier'
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(45) : warning C4007: 'WinMain' : must be '__stdcall'
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(55) : error C2143: syntax error : missing ')' before '{'
c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(55) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Build log was saved at "file://c:\Documents and Settings\Øyvind Ege\Mine dokumenter\Visual Studio 2005\Projects\example1\example1\Debug\BuildLog.htm"
example1 - 7 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

And here's my code:

#include <windows.h>

HINSTANCE hInst;
HWND wnHandle;

bool initWindow( HINSTANCE hInstance )
{
	WNDCLASSEX wcex;

	wcex.cbSize			=sizeof( WNDCLASSEX );
	wcex.style			=CS_HREDRAW| CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra     = 0;
	wcex.cbWndExtra     = 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW);
	wcex.lpszMenuName   = NULL;
	wcex.lpszClassName  = "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	wndHandle = CreateWindow("DirectXExample", "DirectXExample",WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
	if(!wndHandle)
		return false;

	ShowWindow(&wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);
	return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	if (!initWindow(hInstance) )
		return false;

	MSG msg;

	ZeroMemory( &msg, sizeof(msg ) );
	while( msg.message!=WM_QUIT)
	{
		while (GetMessage(&msg, wndHandle, 0, 0)
		{
			TranslateMessage(&msg);
			DispatchMessage8&msg);
		}
	}
	return (int) msg.wParam;
}

Thanks for any help!
Hope I was helpful. And thank you if you were!
Advertisement
1. Insert line:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

befine WinMain(). This is called predeclaration (or something else). Whdn compiler hits WndProc usage in WinMain, it doesn't yet know about it.
Before WinMain()?
Hope I was helpful. And thank you if you were!
2. Turn off unicode in your project settings. Right-click on your project, select properties. In the "General" tab find "Character Set" and select "Use Multi-Byte Character Set".

!EDIT! yes.

3. You made a typo: "HWND wnHandle" -> "HWND wndHandle"

!EDIT! oops. It is named initWindow. Then before initWindow.

4. Change "int WinMain" to "int WINAPI WinMain".

5. You forgot an extra round brace after "while (GetMessage(&msg, wndHandle, 0, 0)".
Now i get these errors:

------ Build started: Project: example1, Configuration: Debug Win32 ------Compiling...winmain.cppc:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(26) : error C2065: 'wndHandle' : undeclared identifierc:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(47) : warning C4007: 'WinMain' : must be '__stdcall'c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(57) : error C2143: syntax error : missing ')' before '{'c:\documents and settings\øyvind ege\mine dokumenter\visual studio 2005\projects\example1\example1\winmain.cpp(57) : fatal error C1903: unable to recover from previous error(s); stopping compilationBuild log was saved at "file://c:\Documents and Settings\Øyvind Ege\Mine dokumenter\Visual Studio 2005\Projects\example1\example1\Debug\BuildLog.htm"example1 - 3 error(s), 1 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hope I was helpful. And thank you if you were!
^ See edits.
It works now! Thank you very much!
Hope I was helpful. And thank you if you were!

This topic is closed to new replies.

Advertisement