为什么我不能使用包含auto作为参数或返回类型的DLL中的函数?

问题描述 投票:0回答:1

我正在为我正在创建的编程语言试验动态内存分配。我的主项目是用C#编写的,但我有一个包含创建变量的方法的C ++ DLL。 DLL方法使用C#的System.Runtime.InteropServices.DllImport()属性加载。我发现在将我的C ++文件构建到DLL中时(使用g ++ v6.3.0),任何返回auto或具有auto参数的函数都不会导出到DLL中。我检查了dumpbin -exports,发现它们不包括在内。我设法修复了DLL,但它让我想知道为什么auto类型不会导出。

我知道这不仅仅是一个C#问题(我用其他语言测试过它,比如Python,这是在我发现问题是编译问题之前),而且它不仅仅是一个g ++问题,因为其他编译器也是无法导出功能。奇怪的是,在此过程中没有任何错误被抛出。

原始代码:

// Variable Manipulation methods
extern "C" {
    auto CreateVariable(auto value) {
        // Create a variable and return its address
        // Here, C++ deduces that "value" is any type and the function creates a new object of that type, pointing to the value
        return new (typeof(value))(value);
    }

    auto GetVariable(auto address) {
        // Return the value of a variable from the specified address
        // Here, C++ deduces that "address" is a pointer of some sort
        return *addr;
    }

    void SetVariable(auto address, auto value) {
        // Set the variable using its specified address
        // Here, C++ deduces that "address" is a pointer and "value" is any type
        *addr = value;
    }

    void DeleteVariable(auto address) {
        // Delete the variable.
        // Here, C++ deduces that "address" is a pointer of some sort
        delete addr;
    }
}

我希望能够使用

[DllImport("dll_path.dll")]
public static extern IntPtr CreateVariable([MarshalAs(UnmanagedType.Any)] object value);

[DllImport("dll_path.dll")]
public static extern object GetVariable(IntPtr address);

[DllImport("dll_path.dll")]
public static extern void SetVariable(IntPtr address, [MarshalAs(UnmanagedType.Any] object value);

[DllImport("dll_path.dll")]
public static extern void DeleteVariable(IntPtr address);

在我的C#程序中,但它一直在扔System.EntryPointNotFoundException,说无法找到切入点。当然,我怀疑C#只是对DLL很挑剔,但我测试了其他语言,如Python的ctypes模块,并且它抛出了同样的错误。我确实找到了解决方案:使用windows.h BYTE类型(unsigned char)。

我的问题是:为什么我不能用auto参数或auto返回类型导出函数?

c# c++ dll auto
1个回答
2
投票

首先,auto不是C ++中的一些动态类型。 auto关键字是编译器推导出的某种占位符:

// int i1 = 0; // same thing in the eyes of the compiler
   auto i1 = 0;

但是编译器只在使用值初始化时推断出类型。

// ???? i2;
// i2 = 0;

   auto i2; // error! deduce what type?
   i2 = 0;  // cannot call operator= with unknown type

但你可以在lambda类型中使用auto,那里有什么不同?

// type of `lambda` is an unnamed type.
auto lambda = [](auto var) { std::cout << var; };

lambda(1);
lambda("string");

即使它看起来很动态,但事实并非如此。通用lambda使用模板实现:

//    v----- unnamed
struct {
    template<typename T>
    auto operator()(auto var) const {
        std::cout << var;
    }
} lambda{};

这意味着编译器将动态生成新的静态代码以获取auto参数。即使你升级到C ++ 20也意味着什么,它允许:

auto CreateVariable(auto value) {
    // ...
}

实际上不存在任何功能。它只是一个等待实例化的模板。

没有从您的dll导出的功能,因为它只是一些模板。

你在寻找的是这样的:

struct CSharpObject;

auto CreateVariable(CSharpObject* value) -> CSharpObject* {
    // reflect on value to check the type
    // construct a new instance of CSharpObject with the
    // right set of metadata for c# to understand
}

遗憾的是,C ++不了解动态,可反射和垃圾收集的C#对象,而C#不了解C ++的模板和值类型的静态性质。

您需要的是提供一组对一组已知类型进行操作的函数:

auto CreateVariableInt(int value) -> int* {
    return new int{value};
}

auto GetVariableInt(int* address) -> int {
    return *addr;
}

auto CreateVariableDouble(double value) -> double* {
    return new double{value};
}

auto CreateVariableDouble(double* address) -> double {
    return *address;
}

// so on and so forth for every supported types.

然后在C#端,保留包含哪种类型的元数据,并调用正确的函数。

© www.soinside.com 2019 - 2024. All rights reserved.