CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2008
    Posts
    119

    My First Win32 API Program! Non-Sense Errors!

    I have VC++ Express 2008

    my main program

    Code:
    #include<windows.h>
    // LESSON TWO
    // specify a tag name of our main window (called window class name) , it will be registered in WNDCLASSEX.
    const char mainWindowClassName[] = "MainWindowClass";
       
    LRESULT CALLBACK WndProc(HWND hwnd, UNIT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc(hwnd, msg, wParam,lParam);
        }
    
        // Typically if you handle a message in the WndProc, you return zero.
        return 0;
    }
    
    // Start program
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        // Windows class structure is used to register our window class name
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    
        // Step 1: Registering the Window Class
        wc.cbSize        = sizeof(WNDCLASSEX);              // the size of the structure.
        wc.style         = CS_HREDRAW | CS_VREDRAW;         // look at GameTutorial for explaination.
        wc.lpfnWndProc   = WndProc;                         // assign pointer to the window procedure for this window class.
        wc.cbClsExtra    = 0;                               // amount of extra data allocated for this class in memory. Usually 0.
        wc.cbWndExtra    = 0;                               // amount of extra data allocated in memory per window of this type. Usually 0.
        wc.hInstance     = hInstance;                       // assign our hInstance to our window.
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION); // large (usually 32x32) icon shown when the user presses Alt+Tab.
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);     // cursor that will be displayed over our window.
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);        // background Brush to set the color of our window.
        wc.lpszMenuName  = NULL;                            // name of a menu resource to use for the windows with this class.
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION); // small (usually 16x16) icon to show in the taskbar and in the top left corner of the window.
    
        if(!RegisterClassEX(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            exit(-1);
        }
    
        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, mainWindowClassName, "The title of my window",
            WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            exit(-1);
        }
    
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
       
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
    
            TranslateMessage(&Msg);
    
            DispatchMessage(&Msg);
        }
    
        UnregisterClass(mainWindowClassName, hInstance);       
    
        return Msg.wParam;
    }
    These are the errors:

    ------ Rebuild All started: Project: test, Configuration: Debug Win32 ------
    Deleting intermediate and output files for project 'test', configuration 'Debug|Win32'
    Compiling...
    test.cpp
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(6) : error C2061: syntax error : identifier 'UNIT'
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(8) : error C2065: 'msg' : undeclared identifier
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(8) : error C2050: switch expression not integral
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'msg' : undeclared identifier
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'wParam' : undeclared identifier
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(17) : error C2065: 'lParam' : undeclared identifier
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(35) : error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND)' to 'WNDPROC'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(45) : error C3861: 'RegisterClassEX': identifier not found
    c:\documents and settings\fahmi\desktop\test\test\test.cpp(47) : error C3861: '_T': identifier not found
    Build log was saved at "file://c:\Documents and Settings\Fahmi\Desktop\test\test\Debug\BuildLog.htm"
    test - 9 error(s), 0 warning(s)
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


    Note: i set character to Multi-Byte in config. box

    Can you help please?!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: My First Win32 API Program! Non-Sense Errors!

    Code:
    UNIT
    What is a "UNIT"? The Windows constant name is UINT, not UNIT.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Sep 2008
    Posts
    119

    Re: My First Win32 API Program! Non-Sense Errors!

    Thanks, i wasn't aware about it....!

    Compiles, but the program breaks - unexpected error occurs while debugging, i copied this code from a tutorial.

    Anyway, i will double check it.

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: My First Win32 API Program! Non-Sense Errors!

    Quote Originally Posted by f.ben.isaac View Post
    i copied this code from a tutorial.
    It seems to be a tutorial with some (typing) errors.
    Beside that Paul already discovered, change also RegisterClassEX with RegisterClassEx (in C/C++ identifiers are case-sensitive), then add this line before call it
    Code:
       wc.lpszClassName = mainWindowClassName;
    [ Redirected thread ]
    Last edited by ovidiucucu; November 22nd, 2008 at 05:36 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Sep 2008
    Posts
    119

    Re: My First Win32 API Program! Non-Sense Errors!

    WORKED! Perfect

  6. #6
    Join Date
    Nov 2008
    Posts
    39

    Re: My First Win32 API Program! Non-Sense Errors!

    two suggestions:
    1) Dev-C++ IDE has a "template" win32 project that is a simple-but-complete program that compiles.

    2) I'm not sure what tutorial you are using but when i was learning, i found this one to be the best: http://www.winprog.org/tutorial/

    also, Charles Petzold's "Programming Windows" is an invaluable book, and also the online MSDN library is an invaluable resource for windows programming.

    regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured