导出功能与来自dll的clr参数? [英] export function with clr parameters from dll?

查看:141
本文介绍了导出功能与来自dll的clr参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对于没有托管类型的dllexport,这是一个传统的管理c ++ dll,我需要调用一些返回托管类型的功能。容易,我只是在这样一个标题中定义我的静态c(++)函数:

  externC
{
__declspec(dllexport)int __cdecl InitSystem();
}

但是现在静态c(++)函数应该返回一个托管类型,这里我有一个问题。如果我尝试(例如):

  externC
{
__declspec(dllexport)系统:: Collections :: Generic :: List< System :: String ^> ^ __cdecl InitSystem();
}

我收到编译器错误(函数定义需要__clrcall签名)



由于DLL不是一个程序集(我想),我有点失落如何使用.net / clr参数导出一个简单的函数调用。这可能很简单,我只是在错误的方向看?

解决方案

它必须是一个程序集,没有其他使用这样的托管类型构建代码的方式。在一个程序集中混合管理和本地代码是很好的。哪些自动解决问题,该功能将在元数据中可用,无需导出。你需要删除所有的装饰,它不能应用于托管方法。而且它应该在一个引用类中,允许其他管理的语言使用它。

  public ref class Mumble {
public:
static System :: Collections :: Generic :: List< System :: String ^> ^ InitSystem();
};

C#程序现在可以使用var lst = Mumble.InitSystem()。建议使用命名空间。



注意/ clr btw,它很容易将本机代码转换为IL,而不用投诉。关闭/ clr源代码文件或使用#pragma管理。


I've got a legacy managed c++ dll, and I need to call some function which is returning a managed type.

For dllexports without managed types, this is easy, I just define my static c(++) function in a header like this:

extern "C" 
{
  __declspec(dllexport)  int  __cdecl  InitSystem();
}

But now the static c(++) function should return a managed type, and here I got a problem. If I try (for example):

extern "C" 
{
  __declspec(dllexport)  System::Collections::Generic::List<System::String^>^  __cdecl  InitSystem();
}

I get a compiler error (function definition needs __clrcall signature).

Since the DLL is not an assembly (I think), I'm a bit at a loss how to export a simple function call using .net/clr parameters. This probably is simple and I'm just looking in the wrong direction?

解决方案

It has to be an assembly, there's no other way to build code with a managed type like that. Mixing managed and native code in one assembly is fine. Which automatically solves the problem, the function will be available in the metadata, no need to export it. You need to drop all the decoration, it cannot be applied to a managed method. And it should be in a ref class to allow other managed languages to use it.

public ref class Mumble {
public:
    static System::Collections::Generic::List<System::String^>^  InitSystem();
};

A C# program now can use var lst = Mumble.InitSystem(). Using a namespace is recommended.

Watch out for /clr btw, it will readily convert native code to IL without complaint. Either turn off /clr on a source code file or use #pragma managed.

这篇关于导出功能与来自dll的clr参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆