gpt4 book ai didi

c# - 无法从 C# 中看到托管 C++ DLL 中的公共(public)结构

转载 作者:行者123 更新时间:2023-11-30 17:54:53 25 4
gpt4 key购买 nike

我有一个 C# 应用程序。它引用了一个 c++-cli dll。在 c++ dll 的命名空间中,声明了一个公共(public)结构:

namespace Wrapper {

public struct maxs
{
public:
char Name[255];
int num;
};
etc

在 c++ dll 中有一个函数返回一个指向结构指针的指针。

Wrapper::Maxs** Wrapper::Class1::CallMax();

C# 应用程序引用 c++ dll 并声明结构的实例(不安全代码)

 Wrapper.Class1 oMax = new Wrapper.Class1()
Wrapper.Maxs** maxs;
maxs= oMax.CallMax();
int num = oMax.m_num;

然后我尝试像这样访问 maxs 的字段:

for(int i = 0; i < num; ++i)
{
name = maxs[i]->name;
}

但是,“名称”字段在 c# 智能感知中不可见,并且无法编译。有趣的是,我去看了c++元数据中结构的定义,结构是空白的..

using System;
using System.Runtime.CompilerServices;

namespaceWrapper
{
[CLSCompliant(false)]
[NativeCppClass]
[UnsafeValueType]
public struct Maxs
{
}
}

这正常吗?那么问题来了:为什么c#看不到public struct Maxs的字段?为什么元数据信息显示为空白结构?谢谢。

最佳答案

这是 AFAIK 的正确答案:

因为它不是托管结构。您需要将其声明为公共(public)值结构。 – Hans Passant 3 月 24 日 2:33

关于c# - 无法从 C# 中看到托管 C++ DLL 中的公共(public)结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15594434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com