Windows programming

Started by
5 comments, last by Jeff D 22 years, 6 months ago
Ok I buy this bbok called "OpenGL Game Programming", never programmed in windows maybe jumped the gun a little but I dont think thats why this is happening. They show you how to make a Hello World program in windows. I did it and it worked. Next they show a little more advanced version of Hello World in windows, but this one gives me a strange error here is the code:
  
#define WIN32_MEAN_AND_LEAN

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT paintStruct;
	HDC hDC;
	char string[] = "Hello, World!";
	switch(message)
	{
	case WM_CREATE:
		 return 0;
		 break;

	case WM_CLOSE:
		 PostQuitMessage(0);
		 return 0;
		 break;

	case WM_PAINT:
		 hDC = BeginPaint(hwnd, &paintStruct);
		 SetTextColor(hDC, COLORREF(0x00FF0000));
		 TextOut(hDC, 150, 150, string, sizof(string)-1);
		 EndPaint(hwnd, &paintStruct);
		 return 0;
		 break;

	default:
		 break;
	}
	return (DefWindowProc(hwnd, message, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	WNDCLASSEX windowClass;
	HWND hwnd;
	MSG msg;
	bool done;
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	windowClass.lpfnWndProc = WndProc;
	windowClass.cbClsExtra = 0;
	windowClass.cbWndExtra = 0;
	windowClass.hInstance = hInstance;
	windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	windowClass.lpszMenuName = NULL;
	windowClass.lpszClassName = "MyClass";
	windowClass.hIconSM = LoadIcon(NULL, IDI_WINLOGO);

	if (!RegisterClassEx(&windowClass))
			return 0;

	hwnd = CreateWindowEx(Null,
						  "MyClass",
						  "A real Windows Application!",
						  WS_OVERLAPPEDWINDOW |
						  WS_VISIBLE |
						  WS_SYSMENU,
						  100, 100,
						  400, 400,
						  NULL,
						  NULL,
						  hInstance,
						  NULL);

	if (!hwnd)
		return 0;

	done = false;

	while (!done)
	{
		PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

		if (msg.message == WM_QUIT)
		{
			done = true;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}
  
Now when I compile I get this error: --------------------Configuration: firsttry - Win32 Debug-------------------- Compiling... firsttry1.cpp c:\dev-c++\examples\firsttry\firsttry1.cpp(92) : fatal error C1010: unexpected end of file while looking for precompiled header directive Error executing cl.exe. firsttry.exe - 1 error(s), 0 warning(s) Now when I go to help to look up the error it tells me this: Fatal Error C1010 unexpected end of file while looking for precompiled header directive A precompiled header was specified, but it did not contain a precompiled header directive. This error can be caused by specifying an incorrect file as a header file, or by specifying an include file with the /Yu (Use Precompiled Header) command line option that is not listed in the source file as an include file. Since reading this I think its because I am running Dev-c++ Windows.h but really not sure thats why I writing to you . BTW I am usuing VC++. Thx, Jeff Desrosiers
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton
Advertisement
Can''t say that I have an answer to your question... I am new to Microsoft programming and Visual C++ as well

Anyway, I was wondering if YOU might be able to help ME -- I have the same book as you I believe (by Kevin Hawkins and Dave Astle -- bigwigs of GameDev.net) and I am having problems with the first windows programs as well (they don''t make it very clear what to do if you have never compiled windows code from scratch! I have only used the app wizards in VC++)

Anyway, I appear to be having linker problems or something. The CPP file COMPILES okay, but...

I get a message about "unresolved external symbol _main"

I assume this has to do with my use of
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)

Is there another standard windows library that I need to link? The books make no mention of this (I have two Visual C++ books and two game programming books and they all conveniently IGNORE library linking!!) I have no idea why it won''t recognize the call to WinMain or what is going on... (I had similar unresolved symbol problems with the OpenGL function calls, until I linked two OpenGL libraries for my project. But I have NO IDEA what lib to link for this _main problem)


If you can help me, that would be great, and maybe would let me get to the next step where you are and see if I get the same troubles...
Jeff D, try disabling precompiled headers.
You can find this option in Project->Settings, then click the C++ tab and choose "precompiled headers" from the drop down list box. There you can deactivate them by choosing the top most option ("do not use precompiled header"... or sth. like this).
The program compiles fine on my machine when I create a win32 app.

(BTW you have three typos in your code but the compiler should tell you (sizof~sizeof, hIconSM~hIconSm, Null~NULL))

baumep
baumep
You should ALWAYS disable pre-compiled headers. Those things cause more problems than all the virii listed on www.mcafee.com . Microsoft should perminently remove those things from visual studio, I can''t think of a single case where having them turned on actualy did anyone any good.
Jeff D: You probably created a "Simple Win32 Application" project, which automatically has pre-compiled headers enabled. What you need to do is to #include "stdafx.h" in every source-file you have (or just turn off pre-compiled headers as baumep suggested).

When pre-compiled headers are enabled the compiler will pre-compile everything that is included in stdafx.cpp (through stdafx.h). Then when another source-file includes the stdafx.h file the compiler has already compiled all the stuff in it (and its #included headers), so compilation of everything except the stdafx.cpp file should be sped up.

I never use PCH myself, though, so I cant say whether it works well or not. For the small projects you will be creating as you learn to program, the compilation times (probably) won''t be very long anyway, so you might want to follow baumep''s and AP''s advice, just to be sure.
Blueboycurtis...

Are you trying to make a console program instead? Console apps use Main instead of WinMain, and that *may* be the source of your problem.
Thx guys it worked.
Suffered seven plagues, but refused to let the slaves go free. ~ Ross Atherton

This topic is closed to new replies.

Advertisement