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

Commit

Permalink
cpp secret
Browse files Browse the repository at this point in the history
  • Loading branch information
UnixY2K committed Jan 11, 2022
1 parent 13e924a commit f4fa94d
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"preLaunchTask": "build C# project",
"program": "${workspaceFolder}/csharp/bin/Debug/net6.0/csharp-cross-platform.dll",
"args": [],
"justMyCode": false,
"cwd": "${workspaceFolder}/csharp",
"environment": [
{
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@

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

# App Center Build with this project

> this steps will be called from the custom build scripts on App Center
> So you will need to set the following environment variables
> APPCENTER_APP_SECRET
- on this example the c++ project will contain the app center application secret
- build the project with the secret like this:
```ps1
meson builddir '-DAPPCENTER_APP_SECRET="<your secret>"'
cd builddir
meson compile
```
- this will generate a dll that needs to be copied to the c# project binary folder
- do the common steps for the c# project

# From scratch guide

## C# first Steps

- Create a new C# project.
Expand Down
5 changes: 5 additions & 0 deletions cpp/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ if host_machine.system() == 'linux'
myApp_deps += [gtkmm_dep]
endif

## add app center app secret to arguments
myApp_args += [
'-DAPPCENTER_APP_SECRET="'+get_option('APPCENTER_APP_SECRET')+'"',
]


# we compile the c++ application as a shared library
myApp_lib = library('myapp',
Expand Down
10 changes: 9 additions & 1 deletion cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <dialogBox.hpp>
#include <iostream>

#ifndef APPCENTER_APP_SECRET
#define APPCENTER_APP_SECRET ""
#endif

#define myAppLIBRARY_EXPORT
#include <exportAPI.hpp>

Expand All @@ -24,5 +28,9 @@ myAppAPI void setupAppCenterCallbacks(
Interop::AppCenter::setTrackEventCallback(trackEventCallback);
Interop::AppCenter::setTrackEventCallback(trackEventExtraCallback);
}

// this will retrieve the AppCenter app secret
myAppAPI const char* getAppCenterAppSecret() {
constexpr const char *appSecret = APPCENTER_APP_SECRET;
return appSecret;
}
}
2 changes: 2 additions & 0 deletions csharp/csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>2116154c-7e25-4d69-b324-cdf42e3a7b68</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AppCenter" Version="4.4.0" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="4.4.0" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion csharp/source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace csharp;
using Microsoft.AppCenter.Analytics;
using Microsoft.AppCenter.Crashes;

// secrets
using Microsoft.Extensions.Configuration;

static class Program
{
/// <summary>
Expand All @@ -26,7 +29,7 @@ static void Main()
Crashes.TrackError(args.Exception);
};
// this will start the App Center SDK
AppCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Crashes));
AppCenter.Start(AppCenterCpp.getAppSecret(), typeof(Analytics), typeof(Crashes));
// this will setup the callbacks for C++
AppCenterCpp.setup();
// this will initialize the C++ app
Expand Down
16 changes: 12 additions & 4 deletions csharp/source/interop/AppCenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@ public static void initApp()
dllEntry();
}

public static string getAppSecret()
{
var result = getAppCenterAppSecret();
var strResult = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(result);
return strResult ?? "";
}

[DllImport("myApp")]
private static extern IntPtr getAppCenterAppSecret();

// the C++ dll entry point
[DllImport("myApp.dll", EntryPoint = "dllEntry")]
[DllImport("myApp", EntryPoint = "dllEntry")]
private static extern void dllEntry();
[DllImport("myApp.dll")]
[DllImport("myApp")]
private static extern void setupAppCenterCallbacks(TrackEventDelegate normal, TrackEventExtraDelegate extra);

private static void trackEventFunc(string eventName)
{
Console.WriteLine("trackEventFunc: " + eventName);
Analytics.TrackEvent(eventName);
}
private static void trackEventFunc(string eventName, string properties)
{
Console.WriteLine("trackEventFunc: " + eventName + " " + properties);
// convert the properties string(json) to a dictionary
var props = JsonConvert.DeserializeObject<Dictionary<string, string>>(properties);
Analytics.TrackEvent(eventName, props);
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('APPCENTER_APP_SECRET', type : 'string', value : '', description : 'AppCenter app secret')

0 comments on commit f4fa94d

Please sign in to comment.