Home Backend Development C++ What are the function calling conventions in C++?

What are the function calling conventions in C++?

Apr 12, 2024 pm 01:51 PM
linux c++ typedef Function calling convention

Calling conventions in C define how function arguments are passed and values ​​are returned, including cdecl, fastcall, thiscall, and stdcall. In practice, the stdcall calling convention can be used to load and call functions in a DLL.

C++ 中函数调用约定有哪些?

C Function Calling Convention

In C, the calling convention defines how arguments to a function are passed and returned. Different calling conventions have different trade-offs in terms of performance, memory usage, and code portability.

Common calling conventions

  • cdecl (also known as stdcall): Applicable to Windows and Linux, parameters are pushed from right to left Stack, pop from left to right.
  • fastcall: Only used in Windows, the first two parameters are passed through registers, and the other parameters are pushed onto the stack.
  • thiscall: Used in object-oriented programming for member functions, the this pointer is passed through a register as the first parameter.
  • stdcall: Similar to cdecl, but uses Windows-style name decoration.

Practical case

The following C code demonstrates the use of stdcall to call a function:

#include <windows.h> // 只适用于 Windows

typedef void (WINAPI *pfnPrintString)(const char*);

int main()
{
    // 加载 DLL 并获取函数指针
    HMODULE hDll = LoadLibrary("mydll.dll");
    pfnPrintString PrintString = (pfnPrintString)GetProcAddress(hDll, "PrintString");

    // 调用函数,传递参数
    PrintString("Hello, world!");

    // 卸载 DLL
    FreeLibrary(hDll);

    return 0;
}
Copy after login

In this example, stdcall is used to call The convention is to load and call the PrintString function from the DLL.

The above is the detailed content of What are the function calling conventions in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Performance Race: Golang vs. C The Performance Race: Golang vs. C Apr 16, 2025 am 12:07 AM

Golang and C each have their own advantages in performance competitions: 1) Golang is suitable for high concurrency and rapid development, and 2) C provides higher performance and fine-grained control. The selection should be based on project requirements and team technology stack.

Golang and C  : The Trade-offs in Performance Golang and C : The Trade-offs in Performance Apr 17, 2025 am 12:18 AM

The performance differences between Golang and C are mainly reflected in memory management, compilation optimization and runtime efficiency. 1) Golang's garbage collection mechanism is convenient but may affect performance, 2) C's manual memory management and compiler optimization are more efficient in recursive computing.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to switch Chinese mode with vscode How to switch Chinese mode with vscode Apr 15, 2025 pm 11:39 PM

VS Code To switch Chinese mode: Open the settings interface (Windows/Linux: Ctrl, macOS: Cmd,) Search for "Editor: Language" settings Select "Chinese" in the drop-down menu Save settings and restart VS Code

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to use VSCode How to use VSCode Apr 15, 2025 pm 11:21 PM

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages ​​and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version

vscode Previous Next Shortcut Key vscode Previous Next Shortcut Key Apr 15, 2025 pm 10:51 PM

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

How to type multiple lines of comments in vscode How to type multiple lines of comments in vscode Apr 15, 2025 pm 11:57 PM

VS Code The methods of multi-line commenting are: 1. Shortcut keys (Ctrl K C or Cmd K C); 2. Manually add comment symbols (/ /); 3. Select menu ("Comment Block"); 4. Use extensions; 5. Recursive comments (/* /) and block comments ({/ and /}). Multi-line comments help improve code readability and maintainability, but overuse should be avoided.

See all articles