Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C++ Windows API entry points and function calling

Posted on 2007-12-04
4
Medium Priority
?
908 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

610 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