This article is a mirror article of machine translation, please click here to jump to the original article.

View: 2093|Reply: 7

[Source] .NET 8 calls functions in the C++ Dynamic Link library

[Copy link]
Posted on 2024-12-8 18:36:17 | | | |
Requirements: Some algorithm libraries are developed in C++, and there is no C# version, how to use C# to call functions in C++?

First, create a new demo.h header file and define an add addition function, the code is as follows:

Create a new demo.cpp file with the following code:

Download the mingw64 tool at:The hyperlink login is visible.

Extract to the D:\mingw64 folder, temporarily add the mingw64/bin folder to the environment variable in the cmd window, and use the following command:

Call the g++ command to compile the source code into a dynamic link library with the following command:

As shown below:



Two files are generated, demo.dll and demo.lib.

Create a new .NET 8 console project

After referencing the CppSharp package, the .csproj project file reads:

Create a new SampleLibrary object to implement the ILibrary interface, the code is as follows:

by .h, . lib file generates a .cs file that can be called by C# from a function in C++, the code is as follows:

As shown below:



Try calling the add method inside demo.dll, as shown in the image below:



Full Code:


Source code download:

Tourists, if you want to see the hidden content of this post, pleaseReply


Reference:
The hyperlink login is visible.
The hyperlink login is visible.





Previous:Use mkcert to generate a self-signed domain name certificate
Next:Download the FastDeploy source code compilation for Windows
Posted on 2024-12-9 13:26:40 |
Learn to learn
Posted on 2024-12-9 16:32:11 |
Come and watch
 Landlord| Posted on 2025-4-23 10:51:21 |
C++ Standard Library Download:The hyperlink login is visible.
 Landlord| Posted on 2025-4-23 16:49:21 |
 Landlord| Posted on 2025-6-17 17:19:08 |
Xiao Zhazha Posted on 2025-4-23 16:49
https://github.com/dotnet/ClangSharp

Execute using a Docker container with the following command:

Reference:The hyperlink login is visible.
 Landlord| Posted on 2025-6-18 09:43:12 |
The hyperlink login is visible.
The hyperlink login is visible.(Copy Program.cs to your own project and execute it after compilation, the test generation is very simple and incomplete)

 Landlord| Posted on 2025-6-18 14:05:22 |
SWIG (Simplified Wrapper and Interface Generator) is an open-source tool used to convert C/C++ code into interface code for multiple high-level programming languages, supporting languages such as Python, Java, Ruby, C#, etc., and is often used for cross-language development or improving the performance of scripting languages.

Official Website:The hyperlink login is visible.
Source:The hyperlink login is visible.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com