Link to home
Start Free TrialLog in
Avatar of mattososky
mattososky

asked on

C++ Windows API entry points and function calling

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


ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial