使用dllimport进行扩展 [英] Use dllimport for class extended
本文介绍了使用dllimport进行扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有那段代码:
C ++:
I have that code:
C++:
class testfunction {
public:
virtual double operator()(double) = 0;
};
我希望在C#中使用此课程,例如
C#
and i want use this class in C#, example
C#
class myCSharpClass: testfunction
{
}
我怎么做?
我是什么尝试过:
How i can do it?
What I have tried:
extern "C"
{
class testfunction {
public:
virtual double operator()(double) = 0;
};
}
推荐答案
之前已经提出并回答过:
如何在c#中使用c ++类和函数? [ ^ ]
如何在C#应用程序中使用DllExport C ++类 - Stack Overflow [ ^ ]
一些有用的链接:
如何制作C ++类 [ ^ ]
在.NET应用程序中使用非托管C ++库(DLL) [ ^ ]
HowTo:从DLL导出C ++类 [ ^ ]
托管/非托管代码互操作性概述 [ ^ ]
This has been asked and answered before:
How to use c++ classes and functions in c#?[^]
How do I DllExport a C++ Class for use in a C# Application - Stack Overflow[^]
Some useful links:
How to Marshal a C++ Class[^]
Using Unmanaged C++ Libraries (DLLs) in .NET Applications[^]
HowTo: Export C++ classes from a DLL[^]
An Overview of Managed/Unmanaged Code Interoperability[^]
一个解决方案是使用Bridge模式。
软件设计模式 - 维基百科,免费的百科全书 [ ^ ]
桥模式 - 维基百科,免费的百科全书 [ ^ ]
因此,您创建了一个桥,允许您模拟从非托管C ++类派生的托管C#类。当然,你不能直接这样做。但是,在C ++ / CLI中创建一个模拟原始界面的托管界面并不难,然后还有一个派生自原始界面的类并将所有调用转发到托管界面。
因为它需要几行代码,所以我不会写它。如果你知道如何在本机类中保存托管数据,那么实现它应该相对简单。
另一方面,你真的需要实现一个使用托管C#类的原生C ++抽象类?实际上,混合托管代码和本机代码确实需要一些关于互操作的知识。
顺便说一下,如果你真的想写这样的代码,这样的书可能非常有用: Manning | C ++ / CLI在行动中 [ ^ ]
PS另请参阅我的问题和第二个解决方案。
One solution would be to use Bridge pattern.
Software design pattern - Wikipedia, the free encyclopedia[^]
Bridge pattern - Wikipedia, the free encyclopedia[^]
Thus you create a bridge that would allow you to simulate a managed C# class deriving from an unmanaged C++ class. Of course, you cannot directly do that. But it is not hard to create a managed interface in C++/CLI that mimic your original interface and then have also a class that derive from your original interface and forward all calls to the managed interface.
As it required a few lines of code, I won't write it. If you know how to hold managed data inside a native class, then it should be relatively trivial to implement.
On the other hand, do you really need to implement a native C++ abstract class using managed C# class? In fact, mixing managed and native code does required some knowledge about interop.
By the way a book like this one might be very useful if you really want to write such code: Manning | C++/CLI in Action[^]
P.S. See also my comments under question and second solution.
看一下这个例子:如何使用C#在C ++中创建dll - Stack Overflow [ ^ ]
C ++代码(DLL),例如。 math.cpp,编译为HighSpeedMath.dll:
Take a look at this example: How to create dll in C++ for using in C# - Stack Overflow[^]
C++ Code (DLL), eg. math.cpp, compiled to HighSpeedMath.dll:
extern "C"
{
__declspec(dllexport) int __stdcall math_add(int a, int b)
{
return a + b;
}
}
C#代码,例如。 Program.cs:
C# Code, eg. Program.cs:
namespace HighSpeedMathTest
{
using System.Runtime.InteropServices;
class Program
{
[DllImport("HighSpeedMath.dll", EntryPoint="math_add", CallingConvention=CallingConvention.StdCall)]
static extern int Add(int a, int b);
static void Main(string[] args)
{
int result = Add(27, 28);
}
}
}
这篇关于使用dllimport进行扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文