呼叫 C++ C# 的模板函数
我对 C# 的了解非常有限。我的目标是为我的 C# 同事提供 C++ dll API。由于遗留原因,该 dll 必须采用 C++ 语言。
问题 - C++ 模板函数(如下所示,来自 VS)可以在 C# 中封送吗?
class __declspec(dllexport) Foo
{
public:
template <typename T> T* getFoo(T* fooData){return fooData;};
};
如果没有,有什么建议吗?传递到模板函数的每个类型是否都应该有自己的函数,以便 C# 可以对其进行封送?
I have very limited knowledge of C#. My goal is to provide a C++ dll API to my C# co-worker. The dll has to be in C++ for legacy reasons.
Question - Can a C++ template function (shown below from VS) be marshaled in C#?
class __declspec(dllexport) Foo
{
public:
template <typename T> T* getFoo(T* fooData){return fooData;};
};
If not, any suggestions? Should each type that is passed into the template function have their own function so C# can marshal it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以几周后,我能够运行一些东西,我想我会与团队分享它。 (请原谅伪代码的出现)。我基本上自学了 C# 而不是 C++/CLI。
记住这个问题 - C++ 模板函数(如下所示,来自 VS)可以在 C# 中封送吗?
我的解决方法如下:对 C++ 函数进行封送非托管 C# 调用,这些函数可以将调用转换为模板方法。
这是代码:
希望有帮助!
So after a few weeks I was able to get something running and I thought I would share it with the group. (Pardon the Pseudo code Appearance). I basically taught myself C# instead of C++/CLI.
Remember the Question - Can a C++ template function (shown below from VS) be marshaled in C#?
My work around is as followed: Make marshaled unmanaged C# calls to C++ functions that can translate calls to the template method.
Here is the Code:
Hope that helps!
不。没有从 C# 到 C++ 的兼容二进制接口。您只能从 C# 调用导出的 C 符号。
理论上,您可以在 C++ DLL 中显式实例化模板,这将使它们获得外部链接和导出符号表中的条目。但名称修改将使这些函数无法用于所有实际目的。因此,最好的方法是拥有一个调用底层 C++ 函数的中间 C 兼容层。
No. There is no compatible binary interface from C# to C++. You can only call exported C symbols from C#.
Theoretically, you could explicitly instantiate the templates in the C++ DLL, which will cause them to get external linkage and entries in the export symbol table. But name mangling will make the functions unusable for all practical purposes. The best way is therefore to have an intermediate C-compatible layer which calls the underlying C++ functions.
我认为最好的选择是用 C++/CLI 编写代码。您可以公开可由 C# 代码使用的托管 API,但在需要时仍使用本机 C++。
I think your best bet is to write your code in C++/CLI. You can expose a managed API that can be consumed by the C# code, but still use native C++ when required.