如何创建中间库?
我正在寻找一些有关如何使用 C++ 和 C# 库实现某些目标的信息。 我想知道的是如何解决以下问题:
- C# 应用程序:-有一个窗口
- C++ 库:-有一个名为 create_button(x,y) 的函数,当
调用时,它将在
上创建一个按钮 C# 应用程序的窗口。 (如果 C# 应用程序未运行,
什么都不会发生) - C++ 应用程序:-动态链接到 C++ 库并调用 create_button() 功能。
我将如何解决这个问题,我很高兴听到您的一些想法。 平台是windows。我的问题是,如何让 C++ 库与 C# 应用程序通信以创建新按钮?它是链接的吗,套接字,...我特别想到linux中的GTK+,你链接到gtk+库,但是该库如何与GNOME接口来创建一个新窗口等,类似的东西。我对编写 dll 并将其链接到 ac# 应用程序不感兴趣,我对创建一个中间库感兴趣。
I am looking for some information on how to achieve something with libraries in C++ and c#.
What I would like to know is how to approach the following problem:
- C# application: -has a window
- C++ library: -has a function called create_button(x,y), when
invoked, it will create a button on
the c# application's window. (if the
C# application is not running,
nothing will happen) - C++ application: -dynamicaly links to the C++ library and calls the create_button()
function.
How would I approach this problem, I would be glad to hear some of your ideas.
The platform is windows. My question is, how would I let the C++ library communicate to the c# application to create a new button? Is it linked, sockets, ... I'm particulary thinking of GTK+ in linux, you link to the gtk+ library, but how does the library interface with GNOME to create a new window etc, something like that. I'm not interested in writing dlls and linking those to a c# application, I'm interested in creating an in-between library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想不出任何明智的方法来做你想做的事。我认为您应该做的是创建函数来在 C# 应用程序中进行绘图,然后公开一些消息传递接口(例如套接字),该接口允许外部应用程序发送消息,命令 C# 应用程序执行您告诉它的操作。当 C# 应用程序收到消息类型为 DRAW_BUTTON 的消息时,它会使用收到的消息中指定的任何参数来绘制按钮。
I can't think of any sane way to do what you want to do. What I believe you should be doing is creating functions to do the drawing in the C# app and then exposing some messaging interface, such as a socket, that allows external apps to send messages that command the C# app to do what you tell it. When the C# app receives messages of with message type DRAW_BUTTON, it draws the button, with whatever parameters were specified in the message it received.
由于 C# 是托管的,而 C++ 是本机的,因此您将在这里遇到问题。据我所知,从 CLI 中托管调用本机代码的唯一方法是使用 P/Invoke 层,在这种情况下,您需要导入 DLL、编写原型等。
此外,我相信P/Invoke 调用是针对 C 函数,而不是 C++,尽管您可以通过添加要调用的 C 库来绕过这一点,而该库又会调用您的 C++ 库。
You're going to run into problems here since C# is managed and C++ is native. As far as I know, the only way of calling native code from managed in the CLI is by using the P/Invoke layer, in which case you would need to import a DLL, write a prototype, etc.
In addition, I believe that the P/Invoke calls are to C functions, and not C++, although you can get past this by adding a C library to call into which in turn calls your C++ library.
如果您可以在 C# 中创建一个小但合适的 HWND(不知道该小部件),您可以使用它并使用 C# 的窗口作为父窗口创建一个 C++ 窗口。
几年前,我们对 java/c++ 进程对正是这样做的。但是,Java 应用程序可以通过 RPC 将 HWND 值报告给 C++ 应用程序,因此设置并不难。
If you can create a small but proper HWND in C# (don't know the widget), you can use that and create a c++ window using the C#'s window as parent.
We did exactly this a few years ago for a java/c++ process pair. However, the Java app could report the HWND value to the c++ app over RPC, so it wasn't that hard to setup.
假定您控制 C# 应用程序的代码,并且控制 C++ 应用程序的代码,并且可能还控制“中间库”。我发现自己在问“你为什么要这样做?”
如果您希望 C++ 应用程序能够在 C# 应用程序上添加给定按钮,并且按下该按钮能够与 C++ 应用程序进行通信,那么就个人而言,我会使用标准 IPC 并拥有一个C++ 应用程序和 C# 应用程序之间的通信通道,只需让 C++ 应用程序通过 IPC 请求 C# 应用程序显示按钮,然后让 C# 应用程序将任何详细信息(很可能是按下按钮的事实)发送到通过相同 IPC 通道的 C++ 应用程序。
如果这条路线行不通,那么我认为您需要澄清您正在尝试解决的问题,因为目前我认为您当前的“解决方案”走在了错误的轨道上,因此答案告诉您如何实现当前的“解决方案”会被误导。
Given that you control the code of the C# application and you control the code of the C++ application and, presumably, the 'in between library'. I find myself asking "why are you doing it like this?"
If you want the C++ app to be able to cause the addition of a given button on the C# app and for the press of that button to be able to communicate with the C++ app then, personally, I'd use standard IPC and have a communications channel between the C++ app and the C# app and simply have the C++ app ask the C# app, via IPC, to display the button and then have the C# app send whatever detail (most probably the fact that the button was pressed) to the C++ app via the same IPC channel.
If this route wont work then I think you need to clarify the problem that you're trying to solve as at present I think your current 'solution' is on the wrong track and so an answer telling you how to achieve your current 'solution' would be misguided.