Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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++?

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;
}

![]() |
Installer program
|
reading html form into pe...
|

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