Asynchronous communication between C++ and C#

  • Thread starter Thread starter Fisad
  • Start date Start date
F

Fisad

Guest
I have a console application in C++ that when loaded load a library in C#, which, in turn, when loaded shows a modal form with several buttons.

I need to implement a call back function from C# to C++ to indicate, in the C++ console output, every time an action is made in the modal form.

I have treated the following code C++:

#include "pch.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <tchar.h>
#import ".\CSharpDll.tlb" raw_interfaces_only

int wmain(int argc, char* argv[]) {
CoInitialize(0); // Init COM

BSTR thing_to_send = ::SysAllocString(L"10 20");
BSTR returned_thing;
CSharpDll::_TheClassPtr obj(__uuidof(CSharpDll::TheClass));
HRESULT hResult = obj->GetTheThing(thing_to_send, &returned_thing);

if (hResult == S_OK) {
std::wcout << returned_thing << std::endl;
return 0;
}
return 1;
}
//Call back function to test
typedef void(*CallbackFunction)(const char*);
void setCallback(CallbackFunction delegate)
{
std::cout << &delegate << std::endl;
}

La aplicacion anterior se comunica con la libreria en C# con el siguiente codigo C#:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace CSharpClass
{
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("485B98AF-53D4-4148-B2BD-CC3920BF0ADF")]
public class TheClass
{
int Num1;
int Num2;
public String GetTheThing(String arg)
{
string s = arg;
string[] words = s.Split(' ');

Num1 = Int32.Parse(words[0]);
Num2 = Int32.Parse(words[1]);

//Load the form when the call occurs from the C ++ application
ShowForm(Num1, Num2);

//Answer once the form is closed
int retVal = 0;
return retVal.ToString();// arg + "the thing";
}

static void ShowForm(int a, int b)
{
Form Form4 = new Form1(a, b);
Form4.ShowDialog();
}


//Return call function statement
public delegate void CallbackDelegate(string message);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern void SetCallback(IntPtr aCallback);

private CallbackDelegate del;
public void TestCallbacks()
{
System.Console.Write("Registering C# callback...\n");
del = new CallbackDelegate(Callback01);
SetCallback(Marshal.GetFunctionPointerForDelegate(del));

System.Console.Write("Calling passed C++ callback...\n");
}

public void Callback01(string message)
{
System.Console.Write("callback 01 called. Message: " + message + "\n");
}
}
}
When the C ++ application is executed, the form is not displayed and the application ends without an answer after a few seconds (15 mm).

If the section indicated as //Return call function statement is commented (on C# side), the application is executed and the form is displayed. This indicates that something is not declared as it should be on the C# side.

For me it is totally new to make C++ converge with C# during a modal function like that of the form.

I need to know how to correct the statement so that the callback function is recognized.

Continue reading...
 
Back
Top