Track issues, software defects, enhancements and change request - FREE Trial

eBusiness Help
Pay Pennies For New Clients
Expose your site to over 1/2 BILLION searches per month
Get A FULL RANGE of Advanced Features to Enhance Your Meetings
Slide shows, Desktop & Application Sharing, Web Page Viewing, Recording and more

WebProWorld Dev Forum

JAVA
I have my application folder in the root dir of tomcat so how can i run my servlet file by providing the necessary config in the web.xml file.
Click to read more...

Is Linux a Threat to the US?
The SCO Group Inc. this month, sent the 535 members of the U.S. Congress a letter that called Linux and open-source software a threat to the security and economy of the U.S..
Click to read more...

Outlook .pst File too Large to Manage
One of my co-workers has grown his Outlook .pst file to 1.99 gigs (apparently he’s very popular). Outlook’s limit for .pst (prior to the 2003 release) is 2 gigs
Click to read more...



Recent Articles

Oracle Unleashes Army of Developers
Oracle CEO Larry Ellison announced the release by 2008 of "Project Fusion," the company's next-generation information- oriented application architecture and application set.

Using PHP CURL Library to Scrape the Internet
Have you ever though how much information is there in DMOZ? Your entire life won't be enough to collect and sort it. Well, we had to do part of that. P.I.M. Team Bulgaria was involved in scraping the technology directories of DMOZ, google, yahoo and many more.

PHP On-The-Fly!
PHP can be used for a lot of different things, and is one of the most powerful scripting languages available on the web. Not to mention it's extremely cheap and widely used. However, one thing that PHP is lacking, and in fact most scripting languages are, is a way to update pages in real-time, without having to reload a page or submit a form.

Sun Trying Something New ... Like Giving Away An Operating System
Sun Microsystems is trying a new marketing strategy, giving away its new Solaris 10 operating system for free. Hewlett Packard sells a printer at a low price and makes a lot of money on printer cartridges.

MySQL Database Handling in PHP
Most interactive websites nowadays require data to be presented dynamically and interactively based on input from the user. For example, a customer may need to log into a retail website to check his purchasing history.

Developing State-enabled Applications With PHP
Installment 1 Developing State-enabled Applications With PHP

Generate HTML files with PHP to Reduce the Server Load
PIM Team Case Study How you can have a site with tons of visitors without to crash the server?

XP, Component Services and .NET
First of all, COM+ does revolutionize COM; it is not only a superior new version of the COM programming model it is also a new platform to design and develop components.

Google, Oracle, and Others Involved in SDForum Event
SDForum announced the Developer Testing Forum, the first event to bring together leading luminaries and practitioners of developer testing.

Programming Auto-apply in Accounts Receivable
Microsoft Great Plains is one of three Microsoft Business Solutions mid-market ERP products: Great Plains, Solomon, Navision.

01.26.05


Using Win32 Calling Conventions

By Steve Friedl

When writing code for the Win32 platform, most developers don't pay attention to selecting a "calling convention", and in most cases it doesn't really matter much. But as systems get larger and split into more modules (especially when third-party modules are to be included), this becomes something that cannot really be ignored.

In this Tech Tip, we discuss what the MSVC calling conventions are, why to choose one over the other, and "big system" considerations that may not be obvious.

Calling Conventions

Traditionally, C function calls are made with the caller pushing some parameters onto the stack, calling the function, and then popping the stack to clean up those pushed arguments.

/* example of __cdecl */
push arg1
push arg2
push arg3
call function
add sp,12 // effectively "pop; pop; pop"


It turns out that Microsoft compilers on Windows (and probably most others) support not just this convention, but two others as well. The technical details are found at Microsoft's MSDN site, but we'll touch on them here as well.

The default convention - shown above - is known as __cdecl, and the other most popular one is __stdcall. In this convention, the parameters are pushed by the caller, but the stack is cleaned up by the callee.

Zero-Downtime Network - 1 Hour Hardware Replacement - Instant Emergency Response
Learn more about our award-winning fanatical support.

This is the standard convention for Win32 API functions (as defined by the WINAPI macro in <windows.h>), and it's also sometimes called the "Pascal" calling convention.

/* example of __stdcall */
push arg1
push arg2
push arg3
call function
// no stack cleanup - callee does this


This looks like a minor technical detail, but if there is a disagreement on how the stack is managed between the caller and the callee, the stack will be destroyed in a way that is unlikely to be recovered.

A mismatch in calling convention is catastrophic for a running program.

At first this seems like a not-that-interesting distinction (to many it is in fact not-that-interesting), but there are several implications that arise when considering one or the other.

We'll note that there is also a __fastcall convention that uses registers, but we don't believe it's really that useful in the general case - the save and restore of the registers often removes any speed benefit of using the register for arg passing. We'll only touch on it in passing.

  • Since __stdcall does stack cleanup, the (very tiny) code to perform this task is found in only one place, rather than being duplicated in every caller as it is in __cdecl. This makes the code very slightly smaller, though the size impact is only visible in large programs.

  • Variadic functions like printf() are almost impossible to get right with __stdcall, because only the caller really knows how many arguments were passed in order to clean them up. The callee can make some good guesses (say, by looking at a format string), but the stack cleanup would have to be determined by the actual logic of the function, not the calling-convention mechanism itself. Hence only __cdecl supports variadic functions so that the caller can do the cleanup.

  • There isn't really a "right or wrong" with respect to which one is best, but it is positively fatal to "mix and match". The general principle is "the stack-cleanup must match the arg-pushing", and this only happens when caller and callee know what the other is doing. Calling a function with the "wrong" convention will destroy the stack.

    Linker symbol name decorations

    As mentioned in a bullet point above, calling a function with the "wrong" convention can be disastrous, so Microsoft has a mechanism to avoid this from happening. It works well, though it can be maddening if one does not know what the reasons are.


    They have chosen to resolve this by encoding the calling convention into the low-level function names with extra characters (which are often called "decorations"), and these are treated as unrelated names by the linker. The default calling convention is __cdecl, but each one can be requested explicitly with the /G? parameter to the compiler.

    __cdecl (cl /Gd ...)

    All function names of this type are prefixed with an underscore, and the number of parameters does not really matter because the caller is responsible for stack setup and stack cleanup. It is possible for a caller and callee to be confused over the number of parameters actually passed, but at least the stack discipline is maintained properly.

    __stdcall (cl /Gz ...)

    These function names are prefixed with an underscore and appended with the number of bytes of parameters passed. By this mechanism, it's not possible to call a function with the "wrong" type, or even with the wrong number of parameters.

    __fastcall (cl /Gr ...)

    These function names start with an @ sign and are suffixed with the @parameter count, much like __stdcall.

    Read the Rest of the Article.

    About the Author:
    Steve Friedl is a software and network security consultant in Southern California. He runs Unixwiz.net which features a collection of tools, tech tips, and other information in the scope of his consulting practice.

  • About DevNewz
    DevNewz has assembled experts around the world to deliver helpful advice to application developers. Our in-house news staff focuses on keeping you updated with the latest new software and trends in application development. DevNewz provides Knowlege For Application Developers.

    DevNewz is brought to you by:

    SecurityConfig.com NetworkingFiles.com
    NetworkNewz.com WebProASP.com
    DatabaseProNews.com SQLProNews.com
    ITcertificationNews.com SysAdminNews.com
    LinuxProNews.com WirelessProNews.com
    CProgrammingTrends.com ITmanagementNews.com


    -- DevNewz is an iEntry, Inc. publication --
    iEntry, Inc. 880 Corporate Drive, Lexington, KY 40503
    2005 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

    archives | advertising info | news headlines | free newsletters | comments/feedback | submit article

    Knowlege For Application Developers DevNewz News Archives About Us Feedback DevNewz Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact