如何将C#中的表单中的数字发送到C ++中的控制台程序 [英] How to send a number from a form in C# to a console program in C++

查看:77
本文介绍了如何将C#中的表单中的数字发送到C ++中的控制台程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个非托管的C ++应用程序,这个应用程序必须连接到C#中的库(dll,加载时必须显示带有按钮的表单)并等待表单关闭事件终止。



在加载表单时,在按下按钮时应该执行click事件,该事件应该执行一个函数,该函数可以在C ++中向控制台应用程序发送一些返回值并且这个应用程序将在控制台的输出中显示结果数字。



我需要的是解决点5.如下所示,我读了很多文件但是几乎所有都是指C ++ dll和C#应用程序之间的互操作而不是反向(从C#dll返回一个整数值到C ++控制台应用程序)就是这里所需的。



执行必须是动态的,也就是说,在显示表单时,应该从C#库的函数和父C ++控制台ap的函数建立通信。 plication。



我能找到的所有例子几乎都是一样的,加载库,建立通信,并从C ++控制台应用程序中调用它们C#库的功能并显示生成的返回值和应用程序结束,但没有示例显示如何加载C#库,显示表单,按下按钮,并将结果作为结果发送到C ++父控制台应用程序和在控制台的输出中显示结果。



如果你知道如何实现回调,我将非常感激。



我的尝试:



取得的成果是:



1.加载C ++控制台应用程序(非托管),实现

2.从C ++控制台应用程序加载C#库,实现

3导入并与从C#库导出的一些函数进行通信,实现了

4.加载C#库时,从这个库中显示一个带有按钮的表单,实现了
5.显示表单时,按下按钮并在C#中执行一个函数,该函数向C ++控制台应用程序返回一个整数并将结果打印在来自C ++控制台应用程序的控制台输出,待处理

解决方案

您可以在本机和.NET互操作性 [ ^ ]。


阅读我的关于互操作性的技巧来理解机制。



您可以添加一些额外的层,如附加线程或一些等待机制,如信号量在C ++应用程序中,它等待交互,而不是进一步运行。



听起来像是我觉得你应该重新考虑设计 - 如果有可能的话。 ; - )

I need to run an unmanaged C++ application, this application must be connected to a library in C# (dll which when loading must show a form with a button) and wait for form close event to terminate.

While the form is loaded, at the moment of pressing the button the click event should be executed, which should execute a function that can send a number of return to the console application in C ++ and this application will show the resulting number in the output of the console.

What I need is to solve point 5. shown below, I read a lot of documents but almost all refer to the interoperation between a C++ dll and a C# application and not the reverse (Return an integer value from C# dll to a C++ console application) is what is needed here.

The execution must be dynamic, that is, while the form is displayed, a communication should be established from a function of the C# library and a function of the parent C++ console application.

All the examples that I have been able to find do almost the same, load the libraries, establish the communication and, from the C++ console application they call a function of the C# library and show the resulting return value and the application ends, but no example shows how to load the C# library, show the form, press a button, and send a integer number as a result to the C++ parent console application and show the result in the output of the console.

If you have any idea how to implement the Callback, I would be very grateful.

What I have tried:

What has been achieved is:

1. Load C++ console application (unmanaged), achieved
2. Load a C# library from the C++ console application, achieved
3. Import and communicate with some functions exported from the C# library, achieved
4. When loading the C# library, from this library a form with an button is shown, achieved
5. While the form is displayed, press the button and execute a function in C# that returns an integer to the C++ console application and print the result in the output of the console from the C++ console application, pending

解决方案

You can find more information about this subject at Native and .NET Interoperability[^].


Read my artcle about interoperability to understand the mechanismus.

You may add some additional layer like an addtional thread or some wait mechanismus like semaphores in the C++ app, which waites for the interaction and than runs further.

It sounds a bit weired, I think you really should reconsider the design - if it is possible. ;-)


这篇关于如何将C#中的表单中的数字发送到C ++中的控制台程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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