Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit e715c04

Browse files
committed
run cpp code from c-sharp
1 parent 7b9b49e commit e715c04

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,23 @@ so you can build a dll that will contain your code that get called from the `C#
4646

4747
For demonstration purposes we will use meson/ninja to create a new `C++ project` and clang or msvc to compile it, but you can use your preferred toolchain (for example CMake) to build the dll.
4848

49-
in this example project it will show a dialog box for simplicity.
49+
in this example project it will show a dialog box for simplicity.
50+
51+
## Loading c++ code from C#
52+
53+
after the dll is compiled, you can use it from the C# project.
54+
55+
- Copy the `c++ dll(and all the required files)` to the `C# project` binary folder.
56+
> This step can be automated with a build script, as it is only needed when you deploy the release of the application.
57+
- Add the following import to the `C# code`.
58+
```csharp
59+
// DllImport
60+
using System.Runtime.InteropServices;
61+
```
62+
- then add the function of the `C++ DLL` in the `C# code` like this:
63+
```csharp
64+
// define the DLL entry point from c++ dll (void dllEntry() from myApp.dll)
65+
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
66+
public static extern void dllEntry();
67+
```
68+

csharp/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
namespace csharp;
22

3+
// DllImport
4+
using System.Runtime.InteropServices;
5+
36
// Microsoft AppCenter SDK
47
using Microsoft.AppCenter;
58
using Microsoft.AppCenter.Analytics;
@@ -19,5 +22,10 @@ static void Main()
1922
// this is only for demo purposes
2023
System.Console.WriteLine("App Center Powered.");
2124
AppCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Crashes));
25+
dllEntry();
2226
}
27+
28+
// define the DLL entry point from c++ dll (void dllEntry() from myApp.dll)
29+
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
30+
public static extern void dllEntry();
2331
}

0 commit comments

Comments
 (0)