?
Solved

C++ Windows API entry points and function calling

Posted on 2007-12-04
4
Medium Priority
?
893 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1200 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 300 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

800 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