1+ namespace csharp . interop ;
2+
3+ // DllImport
4+ using System . Runtime . InteropServices ;
5+
6+ // Microsoft AppCenter SDK
7+ using Microsoft . AppCenter . Analytics ;
8+
9+ // JSON conversion
10+ using Newtonsoft . Json ;
11+
12+ static class AppCenterCpp
13+ {
14+
15+ // setup the required data and callbacks for C++
16+ // it is require that the AppCenter SDK is initialized before calling this function
17+ public static void setup ( )
18+ {
19+ setupAppCenterCallbacks ( trackEvent , trackEventFunc ) ;
20+ }
21+ // initialize the C++ dll entry point
22+ // it is required that the setup is called before calling this function
23+ public static void initApp ( )
24+ {
25+ dllEntry ( ) ;
26+ }
27+
28+ // the C++ dll entry point
29+ [ DllImport ( "myApp.dll" , EntryPoint = "dllEntry" ) ]
30+ private static extern void dllEntry ( ) ;
31+ [ DllImport ( "myApp.dll" ) ]
32+ private static extern void setupAppCenterCallbacks ( TrackEventDelegate normal , TrackEventExtraDelegate extra ) ;
33+
34+ private static void trackEventFunc ( string eventName )
35+ {
36+ Console . WriteLine ( "trackEventFunc: " + eventName ) ;
37+ Analytics . TrackEvent ( eventName ) ;
38+ }
39+ private static void trackEventFunc ( string eventName , string properties )
40+ {
41+ Console . WriteLine ( "trackEventFunc: " + eventName + " " + properties ) ;
42+ // convert the properties string(json) to a dictionary
43+ var props = JsonConvert . DeserializeObject < Dictionary < string , string > > ( properties ) ;
44+ Analytics . TrackEvent ( eventName , props ) ;
45+ }
46+
47+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
48+ private delegate void TrackEventDelegate ( string eventName ) ;
49+
50+ [ UnmanagedFunctionPointer ( CallingConvention . Cdecl ) ]
51+ private delegate void TrackEventExtraDelegate ( string eventName , string eventProperties ) ;
52+ private static TrackEventDelegate trackEvent = trackEventFunc ;
53+ private static TrackEventExtraDelegate trackEventExtra = trackEventFunc ;
54+ }
0 commit comments