[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 922
  • Last Modified:

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


0
mattososky
Asked:
mattososky
  • 3
3 Solutions
 
jkrCommented:
"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
 
jkrCommented:
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
 
itsmeandnobodyelseCommented:
>>>> 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
 
jkrCommented:
>>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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now