Solved

C++ Windows API entry points and function calling

Posted on 2007-12-04
4
867 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
ID: 20404227
"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
ID: 20404294
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
ID: 20405076
>>>> 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
ID: 20408863
>>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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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