Open Hancapo opened 8 months ago
When using PInvoke to call a class implemented in C++, it's important to note that the same C++ class method may have different decorated names in x86 and x64 environments. This means you'll need two separate PInvoke declarations for the same method, each targeting x86 and x64 platforms respectively, and then you'll need to manually determine which version to call in your code.
https://github.com/myd7349/Ongoing-Study/tree/master/c%23/Console/PInvoke/MarshalCppClassV1
ClangSharpPInvokeGenerator also provides the following options:
I just need x64 modules, nothing else, I tried using the -m64 flag and I still get the same error.
You can use this tool to inspect the exposed functions/methods in the C++ DLL. Make sure to check the option to leave the decorations alone.
What is the target architecture of your C# project? Is it AnyCPU or x64?
In general binding against C++ is error prone and undesirable due to having an unstable ABI and no standard for exports across platforms/architectures.
Its typically recommended to have a C wrapper over your C++ and then bind against the C wrapper instead.
ClangSharp does get the mangled name directly from Clang
but what that name is depends on several factors such as the CPU architecture (x86, x64, Arm64, etc) and the OS (Windows, Linux, MacOS, etc). So you may need to specify -m64
to ensure you have the right bitness and -c unix-types
or -c windows-types
if you want to explicitly use Unix vs Windows mangling.
Clang however does not work "perfectly" for cross compilation and so simplify specifying -c unix-types
on Windows or -c windows-types
on Unix may still result in other quirks in the codegen. That is a limitation of Clang
I have the intention to generate bindings for a certain library, so in order to get used to it I did my own addition and subtraction C++ library.
Windows 11 23H2 (Build 22631.3235) ClangSharpPInvokeGenerator 17.0.1 JetBrains Rider 2023.3.3
sumarrestar.h
sumarrestar.cpp
I would ideally like to avoid any changes to the original C++ code or at least they should be as minimal as possible.
Compilation settings:
The command line I used to generate it:
The generated .cs file:
The error I'm getting from the IDE:
Unhandled exception. System.EntryPointNotFoundException: Unable to find an entry point named '?sumar@SumarRestar@@SAHHH@Z' in DLL 'SumarRestar.dll'.