在 C++ 之间编组类实例的指针; 和 C#
我有一个 ActiveX 控件(用 C++ 编写),并从 C# 项目引用它的 RCW 程序集(由 aximp.exe 创建)。
在实现 Ax 控件的 C++ 代码中,我有一个实现接口的类,该接口作为 Ax 控件的属性公开。
查看生成的 RCW 程序集,我看到了界面。 我可以尝试声明其类型的变量。
现在,如果我只有一个指向内存中实现接口的 C++ 类实例的指针,是否可以使用该指针将其数据编组到表示接口的托管 C# 对象中?
请注意,它不是接口指针。 它是指向我所拥有的类的实例的指针。
I have an ActiveX control (written in C++) and reference it's RCW assemblies (created by aximp.exe) from a C# project.
In the C++ code implementing the Ax control, I have a class implementing an interface which is exposed as a property of the Ax control.
Looking at the generated RCW assemblies, I see the interface. And I can attempt to declare a variable of its type.
Now, if I only have a pointer to the instance of the C++ class implementing the interface in memory, is it possible to marshal its data into the managed C# object representing the interface using that pointer?
Please note that it's not the interface pointer. It's the pointer to the instance of the class that I have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想尝试一下 C++ / CLI。 编写在 C++ 和 C# 之间互操作的代码非常简单。
You might want to give C++ / CLI a try. Writing code that interoperates between C++ and C# is a snap.
我认为这里有两个问题。
您可以将“this”指针编组到托管代码,但不能将其作为特定接口
吗? 这可以通过在 C# 应用程序中实现 COM 接口,然后将“this”作为参数之一传递来实现。 接受“this”指针的托管类型应该是 Int32 或 Int64(无符号 OK),具体取决于您的平台
我可以使用此指针做任何有用的事情
是和否。
您不能直接在此指针上调用任何实例方法,因为没有您可以将值转换为的类型。 所以它不能像使用 COM 接口那样使用。
您可以做的是在本机应用程序中定义一组外部“C”方法,该方法采用该类型的指针作为第一个参数,然后调用该对象上的特定方法。 然后,您可以使用 C# PInvoke 进入该方法
C++
C#
I think there are 2 questions here.
Can you marshal a "this" pointer to managed code but not as a specific interface
Yes. This is possible by implementing a COM interface in the C# application and then passing "this" as one of the parameters. The managed type accepting the "this" pointer should be Int32 or Int64 (unsigned OK) depending on your platform
Can I do anything useful with this pointer
Yes and No.
You can't directly call any instance methods on this pointer because there is no type to which you can cast the value. So it cannot be used like a COM interface would be used.
What you can do is define a set of extern "C" methods in your native application which take the a pointer of that type as the first parameter and then call a particular method on that object. You can then use C# to PInvoke into that method
C++
C#