C ++和C#之间的异步通信 [英] Asynchronous communication between C++ and C#

查看:76
本文介绍了C ++和C#之间的异步通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中有一个控制台应用程序,在加载C#时加载一个库,反过来,当加载时显示带有几个按钮的模态窗体。



我需要在C#到C ++中实现一个回调函数,以便在每次以模态形式进行操作时在C ++控制台输出中指示。



当执行C ++应用程序,表单不显示,应用程序在几秒钟(15毫米)后没有答案结束。



如果部分表示为/ /返回调用函数语句被注释(在C#侧),执行应用程序并显示表单。这表明没有声明它应该在C#端。



对于我来说,在类似模式的函数中使C ++与C#融合是全新的表单。



我需要知道如何更正语句,以便识别回调函数。



我尝试了什么:



我已经更正了代码但是当我运行回调函数时它给了我一个ECall安全错误



我已经处理了以下代码C ++:



int wmain(int argc,_TCHAR * argv [如果(hResult == S_OK){

std :: wcout<< returned_thing<< std :: endl;

返回0;

}

返回1;

}





之前的应用程序使用以下代码与C#中的库进行通信:#/>

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.

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.

What I have tried:

I have corrected the code but when I run the callback function it gives me an ECall security error

I have treated the following code C++:

int wmain(int argc, _TCHAR* argv[]) {

if (hResult == S_OK) {
std::wcout << returned_thing << std::endl;
return 0;
}
return 1;
}


The previous application communicates with the library in C # with the following code C #:

namespace CSharpClass
{
    public class The33Class
    {
        CPPCallBack _CPPCallBack = null;

        public void SetCallBacks(IntPtr cppCallBack)
        {
        }


        public String Getng(String arg) // Make sure this is public
        {
        }

    }
}

推荐答案

我在几年前做过类似的事情。一个非常务实的方法(也可能是一个非常脏的方法)。

请记住我在这里注意到我记得的...



c#部分

I did similar things before some years. A very pragmatic Approach (and maybe also a very dirty one).
Keep in mind I noted here what I remember...

The c# part
public class CSharpCom
{
   // c++ Callback stuff
   private delegate void CPPCallBack([MarshalAs(UnmanagedType.BStr)] string text);

   CPPCallBack _CPPCallBack= null;

   // To set callback from c++ 
   public void SetCallBacks(IntPtr cppCallBack)
   {
      _CPPCallBack= (CPPCallBack)Marshal.GetDelegateForFunctionPointer(
                     cppCallBack,
                     typeof(CPPCallBack));
   }

   // Using of the callback
   public void LogMsg(string msg)
   {
      try
      {
          _CPPCallBack?.Invoke(msg);
      }
      catch
      {
         // Error handling
      }
   }


   // You other COM stuff
}





c ++部分



The c++ part

static void __stdcall CPPCallBack(BSTR msg)
{
  // Implement callback stuff here
}

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));

        // Register the callback
        obj->SetCallBacks((INT64)CPPCallBack);

	HRESULT hResult = obj->GetTheThing(thing_to_send, &returned_thing);

	if (hResult == S_OK) {
		std::wcout << returned_thing << std::endl;
		return 0;
	}
	return 1;
}





我希望它有所帮助




根据OP的反馈调整
代码



I hope it helps


code adjusted according to OP's feedback


控制台必须是C ++的原因吗?这是一个愚蠢的例子,所以你可以在另一个更复杂的环境中工作,比如遗留的C ++应用程序或COM对象吗?



如果没有,那么。 ..



1)为什么不使用也是托管代码的C ++ / CLI?







2)你可以用C#编写一个控制台应用程序。这样可以为您解决问题。
Is there a reason the console has to be in C++? Is this a dumbed-down example so you can get this working in another, more complicated context, like a legacy C++ app or COM object?

If not, then ...

1) Why not use C++/CLI which is also managed code?

or

2) You can write a console app in C#. So that may resolve the issue for you.


这篇关于C ++和C#之间的异步通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆