从非托管 C++ 调用 C#传递或返回“复杂”类型

发布于 2024-08-24 16:26:18 字数 1097 浏览 2 评论 0原文

我正在寻求有关如何使用复杂对象作为返回值或作为参数传递给作为 COM 组件暴露给非托管 C++ 的 C# 类方法的帮助,

原因如下:

我正在开发一个项目,其中有六个非托管 C++ 应用程序每个都直接访问相同的 Microsoft SQL Server 数据库。我们希望能够以最小的更改使用 MS-Sql/Oracle/MySql,并且我们决定实现业务逻辑以及通过 WCF 服务公开的数据层,以获得所需的灵活性。

此策略取决于能否使非托管 C++ 与 WCF 服务进行互操作。有多种方法可以做到这一点,但我想要遵循的策略是创建一个公开为 COM 组件的 C# 程序集,它将充当 C++ 和 WCF 层之间的桥梁。此 C# 程序集将作为 COM 组件加载到非托管 C++ 进程中。

C# 桥接程序集将包含一个帮助程序类,该类具有许多方法来描述以前在 C++ 代码中表示为直接 sql 或存储过程调用的操作。

我有两个问题需要解决

1)对于 INSERT,我需要传递一个表示要插入的实体的对象。在非托管 C++ 方面,我已经知道其中一个实体有大约 40 个属性,必须将其放入 SQL 中 - 我不想要具有 40 个参数的 C# 方法,我想传递一个对象;我不知道如何通过 COM 将 C++ 对象编组到 C# 中,因此我考虑在 C# 端定义一个 Stuct,然后使 Struct COM 可见。

2) 如何返回“SELECT this, that, other, ...”的结果。我见过两个例子。一个返回 struct[],另一个返回单个结构,其中包含每个列字段的 string[] 和描述其他成员数组长度的 int count 成员。

在 C# 方面,我认为这将是定义和公开许多将用于传入/传出数据的请求/响应结构的情况。这些结构需要用属性来装饰,使它们的成员不会因优化而“改变位置”。并且结构成员可能需要使用向编组器提示应如何在 COM 中公开该成员的属性进行修饰。

当然,我必须弄清楚如何实例化和填充这些结构,将其视为来自非托管 C++ 的 COM 对象,然后我必须将它们传递到方法调用中并将它们作为返回值进行处理。

这对我来说是最困难的部分;我理解 C++ 和一些 MFC/ATL,但 C++ 下的 COM 是一个额外的复杂级别。正如我所描述的,任何关于参数传递和返回值处理主题的推荐书籍、博客、教程都会非常有帮助。

I'm after help on how to use complex objects either as return values or passed as parameters to C# class methods exposed to unmanaged C++ as COM components

Here's why:

I'm working on a project where we have half a dozen unmanaged C++ applications that each directly access the same Microsoft SQL Server database. We want to be able to use MS-Sql/Oracle/MySql with minimum changes and we've decided to implement a business logic plus data layer exposed via WCF services to get the required flexibility.

This strategy hinges on being able to get the unmanaged C++ to interop with the WCF service. There are a number of ways to do this, but the strategy I want to follow is to create a C# assembly exposed as a COM component which will act as a bridge between C++ and the WCF layer. This C# assembly will be loaded into unmanaged C++ process as COM component.

The C# bridge assembly will contain a helper class which has a number of methods that describe the operations that were formerly expressed as direct sql or stored proc calls in the C++ code.

I have two problems to solve

1) For an INSERT, I need to pass an object representing the entity to be inserted. On the unmanaged C++ side, the I already know that one of the entities has about 40 properties which have to make it into SQL - I don't want a C# method with 40 parameters, I want to pass an object; I don't know how to marshal a C++ object via COM into C#, so I thought about defining a Stuct on the C# side and then make the Struct COM visible.

2) How to return the result of a "SELECT this, that, other, ... ". I've seen two examples. One returns a struct[] and another returns a single struct containing a string[] for each column field and an int count member describing the length of the other member arrays.

On the C# side, I think it will be a case of defining and exposing a number of request/response structs which will be used to pass data in/out. These structs will need to be decorated with attributes that cause their members not to "change position" as a result of optimization. And the struct members may need to be decorated with the attribute that hints to the marshaller how the member should be exposed in COM.

Then of course I'll have to work out how to instantiate and populate these structs as seen as COM objects from the unmanaged C++, then I'll have to pass them in method calls and process them as return values.

This is the most difficult part for me; I grok C++ and some MFC/ATL but COM under C++ is a whole extra level of complexity. Any recommended books, blogs, tutorials on the subject of parameter passing and return value processing as I've described would be very helpful indeed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-08-31 16:26:18

如果可能的话,我会避免将 COM 引入其中。如果您控制 C++ 代码(听起来像您所做的那样),那么添加调用 C# 代码的单个 C++/CLI cpp 文件应该会更容易。 C++/CLI 可以直接访问和创建托管和非托管类型并在它们之间进行复制。

If possible, I'd avoing bringing COM into the picture. If you control the C++ code (it sounds like you do), it should be easier to add a single C++/CLI cpp file that calls into your C# code. C++/CLI can directly access and create managed and unmanaged types and copy between them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文