How to use a C# class library function in a C++ Project?

I’m struggling to find a working solution, despite there being many documents on the subject. Perhaps the volume of this topic obfuscates the solution but hoping someone can help me out here, might be something simple I’m missing.

I created a solution in Visual Studio 2019 as a Windows Desktop Application and using wxWidgets to create a GUI. I intend on using C++ back-end to utilize WASAPI to record playback audio and store that in a buffer, and then use C# to use Google’s client libraries for DialogFlow gRPC. Google doesn’t support C++ client libraries which is why I’m using C#.

I created a .NET Class Library project within the same solution with the DialogFlow code ready to be used as a callable function. I created a reference to this project under References of my C++ application, but this is where I’m stuck as I can’t find a way to call this function.

I’ve been reading about DLL’s and microsoft’s docs mainly focus on including C++ dlls. I can’t seem to find anything on using C# libraries/DLL’s within the same solution. Do these still have to be included as DLL imports?

enter image description here

C# Code here (from Google Dialog Flow Stream Intent), the function I wish to call from my C++ app is DetectIntentFromStreamAsync()

using Google.Cloud.Dialogflow.V2; using System; using System.Threading.Tasks;  namespace DialogFlowRPC {     public class DetectIntentStream     {          // [START dialogflow_detect_intent_streaming]         public static async Task<object> DetectIntentFromStreamAsync(string projectId, string sessionId, UInt32 sampleRate, byte[] pData, int bytes)         {             var sessionsClient = SessionsClient.Create();             var sessionName = SessionName.FromProjectSession(projectId, sessionId).ToString();              // Initialize streaming call, retrieving the stream object             var streamingDetectIntent = sessionsClient.StreamingDetectIntent();              // Define a task to process results from the API             var responseHandlerTask = Task.Run(async () =>             {                 var responseStream = streamingDetectIntent.GetResponseStream();                 while (await responseStream.MoveNextAsync())                 {                     var response = responseStream.Current;                     var queryResult = response.QueryResult;                      if (queryResult != null)                     {                         Console.WriteLine($"Query text: {queryResult.QueryText}");                         if (queryResult.Intent != null)                         {                             Console.Write("Intent detected:");                             Console.WriteLine(queryResult.Intent.DisplayName);                         }                     }                 }             });              // Instructs the speech recognizer how to process the audio content.             // Note: hard coding audioEncoding, sampleRateHertz for simplicity.             var queryInput = new QueryInput             {                 AudioConfig = new InputAudioConfig                 {                     AudioEncoding = AudioEncoding.Linear16,                     LanguageCode = "en-US",                     SampleRateHertz = (int)sampleRate                 }             };              // The first request must **only** contain the audio configuration:             await streamingDetectIntent.WriteAsync(new StreamingDetectIntentRequest             {                 QueryInput = queryInput,                 Session = sessionName             });              // Second request with audio data             await streamingDetectIntent.WriteAsync(new StreamingDetectIntentRequest             {                 Session = sessionName,                 InputAudio = Google.Protobuf.ByteString.CopyFrom(pData, 0, bytes)             });               // Tell the service you are done sending data             await streamingDetectIntent.WriteCompleteAsync();              // This will complete once all server responses have been processed.             await responseHandlerTask;              return 0;         }         // [END dialogflow_detect_intent_streaming]     } } 
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.