Solved

C++ Windows API entry points and function calling

Posted on 2007-12-04
4
856 Views
Last Modified: 2013-12-03
I want to understand on a fundamental level how the entry points ( and the message pump) work in the winows api.

If you create a simple c++ program the entry point is the 'main' function. This seems very logical. (feel free to correct me any place I'm wrong from here on out) However if you are writing a program that utilizes the Windows API (presumably one that will use a window) the entry point becomes 'WinMain'. If you create  a win32 console application in VS you get an entry point of '_tmain'.

Why?  My presumption is this: The entry point for the aplication should always be main, if the program was using calls to the Windows API it should still start at 'main', but just make the API calls, (and fail if the API wasn't there). Why is this wrong? Also, why with a console app does the entry point become '_tmain'?. How does a binary application know where to start executing code if it's not 'main'?

Also, what exactly is the meaning of the 'WINAPI' in the statement:
"INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )"

This statement seems to break sytax convention of how i understand functions to be written. The return type (INT) should come right before the function name (WinMain). But WINAPI is stuck between them. What exactly is this doing? This question is also relevant for the message pump function:

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

How does Windows know that this applications message handleing function is this? Is it the functions name 'MsgProc'? Is that a predefined thing in the Windows API?

Thanks much,
Matt


0
Comment
Question by:mattososky
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
Comment Utility
"WINAPI" is set as

WINDEF.H:#define WINAPI      __stdcall

Regarding '__stdcall', see http://msdn2.microsoft.com/en-us/library/zxk0tw93(VS.80).aspx

The __stdcall calling convention is used to call Win32 API functions.

>>How does Windows know that this applications message handleing
>>function is this?

When registering the 'WNDCLASS' (http://msdn2.microsoft.com/en-us/library/ms633576.aspx) of your window, you specify that function in the 'lpfnWndProc' member of that structure, that's how Windows knows that this message handler is associated with the window in question
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
To elaborate on the calling convention, see http://en.wikipedia.org/wiki/X86_calling_conventions#stdcall

"The stdcall[1] calling convention is a variation on the pascal calling convention in which parameters are passed on the stack, pushed right-to-left. Registers EAX, ECX, and EDX are designated for use within the function. Return values are stored in the EAX register.

Stdcall is the standard calling convention for the Microsoft Win32 API."

Note that this different from the C/C++ calling convention which is explaind in http://en.wikipedia.org/wiki/X86_calling_conventions#cdecl - that's why it is necessary to specify that convention explicitly.
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 100 total points
Comment Utility
>>>> However if you are writing a program that utilizes
>>>> the Windows API (presumably one that will use a
>>>> window) the entry point becomes 'WinMain'.

Actually the entry point is still main, but the main function was *provided* by MS in order to make some initializations regarding WINAPI. Then they call WinMain which you need to provide a body for.

>>>> If you create  a win32 console application in VS you get an entry point of '_tmain'.
The _tmain is a macro that defaults to main in case of a non-UNICODE project and to wmain in case of UNICODE. Beginning with WINNT 4.0 MS tried to force all internal string handling to UNICODE strings. That's why they also tried to make UNICODE available for console programs and did that (similar to the WinMain above) by providing a main function of their own (in the UNICODE case only) and calling a wmain which has UNICODE strings for the arguments.  

Regards, Alex


0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 total points
Comment Utility
>>Actually the entry point is still main

Err, no, the entry point is what you specify with '/ENTRY' (http://msdn2.microsoft.com/en-us/library/f9t8842e(vs.80).aspx) - it just defaults to

Function name Default for
mainCRTStartup (or wmainCRTStartup) An application using /SUBSYSTEM:CONSOLE; calls main (or wmain)
WinMainCRTStartup (or wWinMainCRTStartup) An application using /SUBSYSTEM:WINDOWS; calls WinMain (or wWinMain), which must be defined with __stdcall
_DllMainCRTStartup A DLL; calls DllMain, which must be defined with __stdcall, if it exists
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now