C++
  Home arrow C++ arrow Page 2 - Function Pointers, part 3
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBMฎ developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Function Pointers, part 3
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 13
    2005-06-07

    Table of Contents:
  • Function Pointers, part 3
  • Windows Calling Conventions
  • Application of Callback Functions
  • Functors Encapsulate Function Pointers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Function Pointers, part 3 - Windows Calling Conventions


    (Page 2 of 4 )

    Alright, here is an overview of how different some Windows calling conventions can be (__pascal, __fortran and __syscall are no longer supported):

    __cdecl
    Argument Passing: right to left
    Stack Maintenance: Calling function pops arguments from the stack
    Name Decoration (C only): ‘_’ prefixed to function names (e.g. ‘_foo’)

    __stdcall
    Argument Passing: right to left
    Stack Maintenance: Called function pops its own arguments from the stack
    Name Decoration (C only): ‘_’ prefixed to function name, ‘@’ appended followed by the number of decimal bytes in the argument list. (e.g. ‘_foo@10’)

    __fastcall (applies to Intel CPUs, this is the default calling convention for Borland)
    Argument Passing: First two DWORD arguments are passed in ECX and EDX, the rest is passed right to left
    Stack Maintenance: Called function pops its own arguments from the stack
    Name Decoration (C only): ‘@’ is prefixed to the name, ‘@’ appended followed by the number decimal bytes in the argument list. (e.g. ‘@foo@10’)

    thiscall (used automatically by C++ code)
    Argument Passing: ‘this’ pointer put in ECX, arguments passed right to left
    Stack Maintenance: Calling function pops arguments from the stack
    Name Decoration: None

    You see that there are many ways leading to Rome, and this is just on a Windows machine. What is interesting for this article though, is how we declare a function pointer to the following function definition:

    void __stdcall Convention(int value, char const *string)
    {
      (void)printf(“%d, ‘%s’\n”, value, string);
    }

    Again, the answer here is simple; we simply integrate the calling convention we need into the function pointer declaration:

    void (__stdcall *ConvPtr)(int, char const*) = &Convention;

    The principle remains the same whether it is a member function, a function argument, etc. Just bolt what you need into the declaration.

    When your linker is giving you errors that it cannot resolve external symbols while you know that you are linking with the correct libraries; be sure to check which calling conventions are being used.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT