Thread: don't make fun of me

  1. #1
    Shadow12345
    Guest

    don't make fun of me

    This code is for setting up an OPENGL-ready window. I honestly don't see why it doesn't work. I get one warning when I compile and I cannot execute the program (or maybe I am executing the program but maybe the window isn't visible for some reason). The warning says that not all control paths in WinMain return a value, I've been scrutinizing this but I just don't see what is wrong. I wasn't sure whether or not to include the entire code...
    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>
    #include <gl/glaux.h>
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
    	HDC hDc;
    
    	PAINTSTRUCT ps;
    	switch(message) {
    	case WM_CREATE:
    		return 0;
    		break;
    	case WM_QUIT:
    		PostQuitMessage(0);
    		return 0;
    		break;
    	case WM_PAINT:
    	hDc =	BeginPaint(hwnd, &ps);
    		EndPaint(hwnd, &ps);
    		return 0;
    		break;
    	default:
    		break;
    	}
    
    	return (DefWindowProc(hwnd, message, wparam, lparam));
    }
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nShowCmd) {
    	HWND hwnd;	//window
    	MSG msg;	//message
    	bool done;
    
    	WNDCLASSEX WindowClass;
    	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(hInstance, IDI_ERROR);
    	WindowClass.hCursor = LoadCursor(hInstance, IDC_CROSS);
    	WindowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);						
    	WindowClass.lpszMenuName = "My first real application";
    	WindowClass.lpszClassName = "This is my window class";
    	WindowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
    
    	if(!RegisterClassEx(&WindowClass))
    		return 0;
    
    	hwnd = CreateWindowEx(
    		NULL,
    		"My Class",
    		"My Window Name",
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU,
    		0, 0,
    		1024, 768,
    		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;
    
    	}
    }
    This compiles with just one warning. Usually when I get one warning I can still execute the program but when I try to execute this code nothing happens. WeIrD1!1!0
    thanks in advanced if you can help me

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    Shadow12345

    There's no reason anyone should laugh at you. You're obviously learning, and you have tried something yourself before asking for help. I salute you! Most of the posts on here are from people who can't be bothered to try things themselves first! Keep it up, and you'll go far.

    OK, you had a few problems with your code.

    1) The reason why you were getting the warning is because you had the 'return msg.wParam' statement inside your while(!done) loop. take it out of the loop and the warning will go away.

    2) you have a case for WM_QUIT, which you dont need. you need to cater for WM_CLOSE instead. in your WM_CLOSE section, you need to DestroyWindow(hwnd) and then PostQuitMessage(0).

    3) You dont need the WM_PAINT case in your code for this example. but leave it in there if you are going to do things later on (i'm guessing you are since you've included some openGL headers).

    4) In your winmain function, you are Registering a class called "This is my window class", but then you are trying to create a window from a class called "My Class". if you want to be able to create a window from a class that you registered, you are going to have to use the same class in your CreateWindowEx() call. you're best off doing this:

    Code:
    char* className = "MyClassName";
    .
    .
    WindowClass.lpszClassName = className;
    .
    .
    hwnd = CreateWindowEx(
    		NULL,
    		className,
    .
    .
    .
    5) Finally, in your PeekMessage call, you'll need to get rid of 'hwnd' and replace it with NULL, if you want it to catch your WM_QUIT message. Like so:

    Code:
    PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE);


    If you do all of the above, you code should compile with no errors, no warnings, show you a blank window, and close down properly when the click the 'X' on the system menu. Hope this helps!

    Good luck!
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Shadow12345
    Guest
    Thank you SOOO much!!! First of all for being understanding, and secondly for helping me!!! Those were seemingly small bugs but I wouldn't've noticed them, or at least not all of them, for quite a while. I should have saw that the return msg.wparam was inside my while not done loop, but some of the other things such as the WM_QUIT as opposed to the WM_CLOSE problem would have never occured to me. Like I said, thanks a bunch. Do you do any opengl? You said that I included 'some' gl headers, are there any more gl headers than what I included??

    Thanks again, I wish there were less flamers (although there aren't many) and more people like you on the c boards.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    No worries. I appreciate your comments. Keep in mind that i have kinda flamed people before on this board. Those people I feel really deserve it. Like i said to you before, i helped you out because you tried something yourself first, and you asked a specific question. people who aren't prepared to give things a go just annoy me. They dont realise that doing things themselves is the best way to learn. So keep it up!

    I play around with OpenGL a little bit. I'm not sure if there are any other headers involved than the ones you have included, but i do know that you have included enough to get things going.

    Anyways, good luck. post again if you get stuck. or you can mail me directly if you like.

    cheers
    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    16
    Just taking a peek here...thought I'd make a quick comment. I would avoid using PeekMessage for the following two reasons:
    1) If there isn't a message in the queue, you're calling TranslateMessage, DispatchMessage anyways. This could cause some "interesting behavior".
    2) PeekMessage is a CPU hog. Load up your system CPU monitor then launch your app, you'll see what I mean. Thing is, GetMessage will let windows schedule some other things until a message *is* there for you to process. PeekMessage just keeps chugging away.

    If you do want to use PeekMessage, then when there isn't a message waiting you can call something other than Translate/DispatchMessage. You can do a lot of 'background' processing this way -- I've done this before.

    Take it easy

    --Chorus

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I cannot execute the program (or maybe I am executing the program but maybe the window isn't visible for some reason)
    I think I can explain a little... the window isn't showing up, and then quits before you can tell that it's started.
    Code:
    while(!done) {
    	PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
    	if(msg.message == WM_QUIT)	
    		done = true;
    	
    	else	
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	
    	
    	return msg.wParam; //Big boo boo!
    
    	}
    Sorry, nobody else seemed to notice or say anything, but the indicated line is very bad. Uraldor noted that this line generated the warning, but there's another problem: I don't know if the moment you return a value in WinMain() the program quits, but that's what happens in Dos. And every time you run through the while loop, it returns msg.wParam. If the program quits when you return, then the message loop will execute only once, then the program will die on you Just thought I'd point that out.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  3. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  4. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM