Help needed.

This is a discussion on Help needed. within the Windows Programming forums, part of the Platform Specific Boards category; Hi All, I am very new to VC++ programming. I tried to create a window. That's all. I wrote a ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    102

    Help needed.

    Hi All,
    I am very new to VC++ programming. I tried to create a window. That's all. I wrote a program and it runs but doesn't shows the window. When I tried to Build it again it needs the exe file to overwrite it. But since the window is invisible and it has not ended, VC++ compiler says that
    fatal error LNK1168: cannot open Debug/test.exe for writing.
    I will paste code here
    Code:
    #include<windows.h>
    long _stdcall WndProc(HWND,UINT,UINT,long);
    WNDCLASS wndclass;
    int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,char *CmdLine,int nCmdShow)
    {
    	HWND hwnd;
    	MSG msg;
    	wndclass.hInstance=hInstance;
    	wndclass.lpfnWndProc=WndProc;
    	wndclass.lpszClassName="My Class";
    	wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    	RegisterClass(&wndclass);
    	hwnd=CreateWindow("My Class","First Window",WS_OVERLAPPEDWINDOW,20,20,100,100,0,0,hInstance,0);
    	ShowWindow(hwnd,nCmdShow);
    	while(GetMessage(&msg,0,0,0))
    	{
    		DispatchMessage(&msg);
    	}
    	return 0;
    }
    long _stdcall WndProc(HWND hwnd,UINT x ,UINT y,long z)
    {
    	switch(x)
    	{
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		DefWindowProc(hwnd,x,y,z);
    	}
    	return 0L;
    }
    Saravanan.T.S.
    Beginner.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    I'm pretty sure there's should be a TranslateMessage in that message loop too.

    Also, you can press CTRL_ALT_DEL to bring up the task list and end your process before recompiling.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    I ran the program Again. Now it gives strange error message
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/test.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    Whatz happening.
    Can any one rescue me.
    Saravanan.T.S.
    Beginner.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> fatal error LNK1168: cannot open Debug/test.exe for writing.

    A common cause of that error is still having the program open somewhere. Make sure you close the last version of the program before trying to compile it again. (Code does not cause this problem).

    >>> error LNK2001: unresolved external symbol _main

    That is typical when you have mixed up a windows app and a console app. Windows apps use WinMain, console apps main(). When you start your project make sure you start it with the correct application type.

    By the way...

    >>> should be a TranslateMessage in that message loop too

    ... he's right.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by _Elixia_
    I'm pretty sure there's should be a TranslateMessage in that message loop too.
    It is just for translating virtual key messages into character messages, so the program will still work, just certain key combinations will not work, if you wish to process the character messages.

    You should also have an UpdateWindow() call after ShowWindow() to force the window to be shown.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> It is just for translating virtual key messages into character messages,

    True, let me re-phrase, for the vast majority of Windows app, there should be a call to TranslateMessage() in the dispatch loop.

    Putting it in doesn't really do any harm, and it'll fix an otherwise confusing problem should you actually need it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Yes, of course. I should have pointed out that I only stated that it was not needed in his program so that the original poster would understand that it was not the cause of his problems. A proper windows program should call it, just in case you require it in a future update.

    I should also point out that not calling UpdateWindow() is also not the cause of his problems.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    1. Declare wndclass in WinMain, not global (optional).
    
    2. Clear wndclass struct before use:
    ZeroMemory(&wndclass,sizeof(WNDCLASS));
    
    3. Use the correct prototype for WndProc (optional):
    LRESULT CALLBACK WindowProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam
    );
    
    4. Provide the correct return value for WndProc:
    switch(uMsg) {
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    
    	// Handle other messages you care about here
    
    	default:
    		return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    // Below the switch - we return zero for messages
    // that we handle.
    return 0;
    
    5. Use appropriate window styles for your main window:
    WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
    WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU |
    WS_THICKFRAME
    6. Read the tutorial:
    http://www.winprog.org/tutorial/

    7. Copy code from the tutorial.

    8. Have Fun!

    8.1 Use the WINAPI define in place of _stdcall (optional).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

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