在 CLR 应用程序中调用托管代码
我在 C++/CLI 类库 DLL 中有以下函数:
public delegate void StandardOutputError(String^ Message);
bool foo(System::String^% arg1, System::String^% arg2, System::String^% arg3, StandardOutputError^ Output);
我如何导出它以便可以由任意托管 (C++/CLI) 代码调用它?我主要关心的是委托参数 - 是否可以安全地将其转换为函数指针并返回(也就是说,如果无法直接导出 __clrcall 函数)?
I've got the following function in a C++/CLI class library DLL:
public delegate void StandardOutputError(String^ Message);
bool foo(System::String^% arg1, System::String^% arg2, System::String^% arg3, StandardOutputError^ Output);
How would I go about exporting it so that it can be called by arbitrary managed (C++/CLI) code ? My primary concern is the delegate argument - Would it be possible to safely convert it to a function pointer and back (that is if it isn't possible to directly export __clrcall functions) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要
foo
位于公共托管类型内,您就可以添加对 C++/CLI 程序集的引用,并在 C# 等中使用它,就像使用任何其他托管程序集一样。StandardOutputError
委托已经很好了。更重要的一点是,托管类型仅凭借托管类型并使用 /clr 编译而导出到 .NET 元数据中。不需要 __declspec(dllexport) 恶作剧。
As long as
foo
is inside a public managed type, you can add a reference to your C++/CLI assembly and use it from e.g. C# just as you would any other managed assembly. TheStandardOutputError
delegate is already fine.The larger point is, managed types are exported in .NET metadata just by virtue of being managed types and compiling with /clr. No
__declspec(dllexport)
shenanigans necessary.