Guide
A detailed guide on how to use the plugin effectively, ensuring you maximize its features and functionality for your needs.
Calling C/C++ Functions Dynamically in Any Language
It is a plugin that enables dynamic invocation of C functions at runtime. This is particularly useful when the function signatures are not known at compile-time, such as when interfacing with plugins, foreign binaries, or scripting systems. It’s based on the powerful dyncall backend and is designed to be easily integrated into your plugin or scripting environment.
This guide covers:
- Initializing a call VM
- Setting up a function call
- Passing arguments
- Invoking the function
- Handling return values
Step-by-Step Example
Let's say you want to dynamically call this function:
1. Get Function Pointer
In practice, this might come from a shared library or plugin:
2. Create a Call VM
You need to allocate a DCCallVM
and set its calling convention:
3. Push Arguments
Push arguments in the correct order using type-specific dyncall::Arg*
functions:
4. Call Function
Call the function using the correct dyncall::Call*
variant:
Other Argument Types
Here’s a summary of how to push different argument types:
C Type | Push Function | Call Function |
---|---|---|
int | dyncall::ArgInt(vm, x) | dyncall::CallInt(...) |
long | dyncall::ArgLong(vm,x) | dyncall::CallLong(...) |
float | dyncall::ArgFloat(...) | dyncall::CallFloat(...) |
double | dyncall::ArgDouble(...) | dyncall::CallDouble(...) |
void* | dyncall::ArgPointer(...) | dyncall::CallPointer(...) |
char* | dyncall::ArgString(...) | dyncall::CallString(...) |
Cleanup
Free the call VM after use:
Advanced: Calling from Low-Level Language
Suppose the function is:
Then dynamic call would be:
Advanced: Calling from High-Level Language
Suppose the function is:
Then dynamic call would be:
Example taken from here
Signature Format (Used in dyncallback
, dynload
)
Signature strings follow this format (from dyncall_signature.h
):
- Return type prefix + ordered argument types
- Example:
"ifp"
= returnsint
, takesfloat
andvoid*
Code | Type |
---|---|
v | void |
i | int |
l | long |
f | float |
d | double |
p | pointer |
s | string |
Notes & Safety
- Argument order matters: they must match the function signature exactly.
- If the function uses
stdcall
,fastcall
, or others, setdyncall::Mode()
accordingly (e.g.,dyncall::Mode::StdCall
). - Use
dyncall::Reset()
before each call to reuse the VM. - Behavior is undefined if the wrong type is passed.
Minimal Complete Example
See Also
- dyncall GitHub
- Full API Docs
man dyncall