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

Commit f4fa94d

Browse files
committed
cpp secret
1 parent 13e924a commit f4fa94d

File tree

8 files changed

+52
-6
lines changed

8 files changed

+52
-6
lines changed

.vscode/launch.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"preLaunchTask": "build C# project",
3535
"program": "${workspaceFolder}/csharp/bin/Debug/net6.0/csharp-cross-platform.dll",
3636
"args": [],
37+
"justMyCode": false,
3738
"cwd": "${workspaceFolder}/csharp",
3839
"environment": [
3940
{

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
App Center Interop example for c++/c#(winforms).
44

5+
# App Center Build with this project
6+
7+
> this steps will be called from the custom build scripts on App Center
8+
> So you will need to set the following environment variables
9+
> APPCENTER_APP_SECRET
10+
11+
- on this example the c++ project will contain the app center application secret
12+
- build the project with the secret like this:
13+
```ps1
14+
meson builddir '-DAPPCENTER_APP_SECRET="<your secret>"'
15+
cd builddir
16+
meson compile
17+
```
18+
- this will generate a dll that needs to be copied to the c# project binary folder
19+
- do the common steps for the c# project
20+
21+
# From scratch guide
22+
523
## C# first Steps
624

725
- Create a new C# project.

cpp/meson.build

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ if host_machine.system() == 'linux'
1616
myApp_deps += [gtkmm_dep]
1717
endif
1818

19+
## add app center app secret to arguments
20+
myApp_args += [
21+
'-DAPPCENTER_APP_SECRET="'+get_option('APPCENTER_APP_SECRET')+'"',
22+
]
23+
1924

2025
# we compile the c++ application as a shared library
2126
myApp_lib = library('myapp',

cpp/src/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <dialogBox.hpp>
33
#include <iostream>
44

5+
#ifndef APPCENTER_APP_SECRET
6+
#define APPCENTER_APP_SECRET ""
7+
#endif
8+
59
#define myAppLIBRARY_EXPORT
610
#include <exportAPI.hpp>
711

@@ -24,5 +28,9 @@ myAppAPI void setupAppCenterCallbacks(
2428
Interop::AppCenter::setTrackEventCallback(trackEventCallback);
2529
Interop::AppCenter::setTrackEventCallback(trackEventExtraCallback);
2630
}
27-
31+
// this will retrieve the AppCenter app secret
32+
myAppAPI const char* getAppCenterAppSecret() {
33+
constexpr const char *appSecret = APPCENTER_APP_SECRET;
34+
return appSecret;
35+
}
2836
}

csharp/csharp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
<Nullable>enable</Nullable>
77
<UseWindowsForms>true</UseWindowsForms>
88
<ImplicitUsings>enable</ImplicitUsings>
9+
<UserSecretsId>2116154c-7e25-4d69-b324-cdf42e3a7b68</UserSecretsId>
910
</PropertyGroup>
1011

1112
<ItemGroup>
1213
<PackageReference Include="Microsoft.AppCenter" Version="4.4.0" />
1314
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="4.4.0" />
1415
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="4.4.0" />
16+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
1517
</ItemGroup>
1618

1719
</Project>

csharp/source/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace csharp;
77
using Microsoft.AppCenter.Analytics;
88
using Microsoft.AppCenter.Crashes;
99

10+
// secrets
11+
using Microsoft.Extensions.Configuration;
12+
1013
static class Program
1114
{
1215
/// <summary>
@@ -26,7 +29,7 @@ static void Main()
2629
Crashes.TrackError(args.Exception);
2730
};
2831
// this will start the App Center SDK
29-
AppCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Crashes));
32+
AppCenter.Start(AppCenterCpp.getAppSecret(), typeof(Analytics), typeof(Crashes));
3033
// this will setup the callbacks for C++
3134
AppCenterCpp.setup();
3235
// this will initialize the C++ app

csharp/source/interop/AppCenter.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ public static void initApp()
2525
dllEntry();
2626
}
2727

28+
public static string getAppSecret()
29+
{
30+
var result = getAppCenterAppSecret();
31+
var strResult = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(result);
32+
return strResult ?? "";
33+
}
34+
35+
[DllImport("myApp")]
36+
private static extern IntPtr getAppCenterAppSecret();
37+
2838
// the C++ dll entry point
29-
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
39+
[DllImport("myApp", EntryPoint = "dllEntry")]
3040
private static extern void dllEntry();
31-
[DllImport("myApp.dll")]
41+
[DllImport("myApp")]
3242
private static extern void setupAppCenterCallbacks(TrackEventDelegate normal, TrackEventExtraDelegate extra);
3343

3444
private static void trackEventFunc(string eventName)
3545
{
36-
Console.WriteLine("trackEventFunc: " + eventName);
3746
Analytics.TrackEvent(eventName);
3847
}
3948
private static void trackEventFunc(string eventName, string properties)
4049
{
41-
Console.WriteLine("trackEventFunc: " + eventName + " " + properties);
4250
// convert the properties string(json) to a dictionary
4351
var props = JsonConvert.DeserializeObject<Dictionary<string, string>>(properties);
4452
Analytics.TrackEvent(eventName, props);

meson_options.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
option('APPCENTER_APP_SECRET', type : 'string', value : '', description : 'AppCenter app secret')

0 commit comments

Comments
 (0)