Computing.Net > Forums > Programming > C++ main vs. winmain

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ main vs. winmain

Reply to Message Icon

Name: Kaveh
Date: May 23, 2003 at 18:20:32 Pacific
OS: Windows XP Pro
CPU/Ram: 128
Comment:

Hi, I have always programmed in dos using ANSI C++ and using the STL functions. As I know every program should have a "main()" function. But then how is it that c++ programs for windows start with "WinMain()" ?
Did Bjarne Stroustrup add this substitute for main to the language? becuase otherwise how can they modify the language's main function to something else without inventing a new c++?



Sponsored Link
Ads by Google

Response Number 1
Name: egkenny
Date: May 24, 2003 at 07:40:19 Pacific
Reply:

In Microsoft Visual C++ if you choose "Win32 Console Application" you produce a DOS-like applictation that uses the regular "main" function. The "hello world" console application:
#include <stdio.h>
main()
{
   printf("Hello, world!\n";
}

If you choose "Win32 Application" you will be required to have a WinMain function. The system automatically calls this function for you when your program starts. This application opens a dialog box:
#include <windows.h>
int WINAPI WinMain(HINSTANCE d1, HINSTANCE d2, LPSTR 
d3, int d4)
{
        MessageBox(NULL, "Hello, World!", "", MB_OK);
}

This applications open a Window:
#include <windows.h>

void DrawHello(HWND hwnd)
{
    HDC hDC;
    PAINTSTRUCT paintStruct;
    RECT clientRect;

    hDC = BeginPaint(hwnd, &paintStruct);
    if (hDC != NULL)
    {
        GetClientRect(hwnd, &clientRect);
        DPtoLP(hDC, (LPPOINT)&clientRect, 2);
        DrawText(hDC, "Hello, World!", -1, &clientRect,
                 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        EndPaint(hwnd, &paintStruct);
    }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg,
                         WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_PAINT:
            DrawHello(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, 
lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                        LPSTR d3, int 
nCmdShow)
{
    MSG msg;
    HWND hwnd;
    WNDCLASS wndClass;

    if (hPrevInstance == NULL)
    {
        memset(&wndClass, 0, sizeof(wndClass));
        wndClass.style = CS_HREDRAW | CS_VREDRAW;
        wndClass.lpfnWndProc = WndProc;
        wndClass.hInstance = hInstance;
        wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW 
+ 1);
        wndClass.lpszClassName = "HELLO";
        if (!RegisterClass(&wndClass)) return FALSE;
    }
    hwnd = CreateWindow("HELLO", "HELLO",
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT, 0, CW_USEDEFAULT, 
0,
                        NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
        DispatchMessage(&msg);
    return msg.wParam;
}


0
Reply to Message Icon

Related Posts

See More


Installer program reading html form into pe...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ main vs. winmain

Microsoft Visual C++ Making Me Mad! www.computing.net/answers/programming/microsoft-visual-c-making-me-mad/10473.html

windows.h library in C++ www.computing.net/answers/programming/windowsh-library-in-c/9308.html

Loading C code without a OS www.computing.net/answers/programming/loading-c-code-without-a-os/10923.html