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 
Sun Developer Network 
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

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT