normal code goes wrong with MVS++

This is a discussion on normal code goes wrong with MVS++ within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys, i've been facing this problem with my winAPI32 program. this is the infamous code Code: #include <windows.h> HWND ...

  1. #1
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186

    normal code goes wrong with MVS++

    Hi guys, i've been facing this problem with my winAPI32 program.
    this is the infamous code
    Code:
    #include <windows.h>
    
    HWND      gMainWnd = 0;
    HINSTANCE gAppInst = 0;
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CREATE:
    		return 0;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		return 0;
    	}
    
    	return DefWindowProc(hWnd, msg, wParam, lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int showWnd)
    {
    	const char* className = "RTS-test";
    	
    	gAppInst = hInstance;
    
    	WNDCLASS wc;
    	wc.style         = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = gAppInst;
    	wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
    	wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
    	wc.lpszMenuName  = 0;
    	wc.lpszClassName = className;
    
    	if(RegisterClass(&wc) == 0)
    	{
    		MessageBox(0, "RegisterClass(&wc) function failed.", "Critical error", MB_OK);
    		return 0;
    	}
    
    	gMainWnd = ::CreateWindow(className, "CAPTION HERE", WS_OVERLAPPED, 0, 0, 500, 500, 0, 0, gAppInst, 0);
    
    	if(gMainWnd == 0)
    	{
    		MessageBox(0, "CreateWindow function failed.", "Critical error.", MB_OK);
    		return 0;
    	}
    
    	ShowWindow(gMainWnd, showWnd);
    	UpdateWindow(gMainWnd);
    
    	MSG msg;
    	ZeroMemory(&msg, sizeof(MSG));
    
    	while(GetMessage(&msg, 0, 0, 0))
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return (int)msg.wParam;
    }
    when I debug it, the
    Code:
    MessageBox(0, "RegisterClass(&wc) function failed.", "Critical error", MB_OK);
    pops up, then moving the mouse a little gives me this:
    http://img356.imageshack.us/img356/1813/errorey1.gif

    i'm working on Microsoft Visual Studio 2005 pro

    what gives?
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    All that's telling you is that you don't have the source code for MessageBox(). Which seems likely, since that belongs to MS.

    But if you use the debugger to display the stack trace, the click on the stack frame for WinMain() you should see your code.
    If you don't, then check you compiled the debug version.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    i don't think so, since i already have included
    Code:
    #include <windows.h>
    i don't get this error before I uninstalled MVS, but after a new installation i get this error
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Hussain Hani View Post
    i don't think so, since i already have included
    Code:
    #include <windows.h>
    i don't get this error before I uninstalled MVS, but after a new installation i get this error
    But that only contains the declaration of the MessageBox function - not the actual code that goes on when you call the function. The source code for MessageBox is NOT ON YOUR MACHINE (unless you have a license for Windows source code, and it's highly likely that you fall into the "you don't" variant in this case - you would know that you do, since you'd have to be in a room that is secure and locked 24/7 with special legal [contract law, not criminal law, but still LAW] restrictions on who can enter, etc).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int showWnd)
    {
    	const char* className = "RTS-test";
    	
    	gAppInst = hInstance;
    
    	WNDCLASS wc = {0};	
            wc.style         = CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc   = WndProc;
    	wc.cbClsExtra    = 0;
    	wc.cbWndExtra    = 0;
    	wc.hInstance     = gAppInst;
    	wc.hIcon         = ::LoadIcon(0, IDI_APPLICATION);
    	wc.hCursor       = ::LoadCursor(0, IDC_ARROW);
    	wc.lpszMenuName  = 0;
    	wc.lpszClassName = className;
    
    	if(RegisterClass(&wc) == 0)
    	{
    		MessageBox(0, "RegisterClass(&wc) function failed.", "Critical error", MB_OK);
    		return 0;
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 09:11 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21