IJW:托管代理结构

发布于 2024-07-19 10:53:33 字数 1399 浏览 1 评论 0 原文

我正在致力于将一些 C++ 代码移植到托管 .NET。 我需要以本机形式保留一些 C++ 代码,并尝试使用 IJW 方法。 我知道可以声明一个非托管结构,以便将其正确封送到 .NET 代码,但 C++ 编译器似乎没有这样做。

例如,我有这个代码(托管):

struct MyStruct
{
     int some_int;
     long some_long;
};

public ref class Class1
{
    static MyStruct GetMyStruct()
    {
        MyStruct x = { 1, 1 };
        return x;
    }
};

它可以编译,但是当我使用 Reflector< /a>,代码如下所示:

public class Class1
{
    // Methods
    private static unsafe MyStruct GetMyStruct()
    {
        MyStruct x;
        *((int*) &x) = 1;
        *((int*) (&x + 4)) = 1;
        return x;
    }
}

[StructLayout(LayoutKind.Sequential, Size=8), NativeCppClass,
                      MiscellaneousBits(0x41), DebugInfoInPDB]
internal struct
{
}

基本上,MyStruct 中的任何字段对 .NET 都是不可见的。 有没有办法告诉 C++ 编译器生成那些?

回答时,请考虑这一点:我知道如何创建一个对.NET框架可见的托管类。 我对此感兴趣。 我希望 C++ 编译器以 .NET 能够理解的方式声明非托管结构。 就像是:

[StructLayout(LayoutKind::Sequential, blablabla ... )]
struct MyStruct
{
    [MarshalAs ....... ]
    System::Int32 some_int;
    [MarshalAs ....... ]
    System::Int32 some_long;
};

I am working on porting some C++ code to managed .NET. I will need to retain some C++ code in native form and am trying to use an IJW approach to it. I know it's possible to declare an unmanaged struct so that it will get correctly marshaled to .NET code, but the C++ compiler doesn't seem to do it.

For example, I have this code (managed):

struct MyStruct
{
     int some_int;
     long some_long;
};

public ref class Class1
{
    static MyStruct GetMyStruct()
    {
        MyStruct x = { 1, 1 };
        return x;
    }
};

It compiles, but when I look at it using Reflector, the code looks like this:

public class Class1
{
    // Methods
    private static unsafe MyStruct GetMyStruct()
    {
        MyStruct x;
        *((int*) &x) = 1;
        *((int*) (&x + 4)) = 1;
        return x;
    }
}

[StructLayout(LayoutKind.Sequential, Size=8), NativeCppClass,
                      MiscellaneousBits(0x41), DebugInfoInPDB]
internal struct
{
}

Basically, no fields in MyStruct are visible to .NET. Is there a way to tell the C++ compiler to generate ones?

When answering, please consider this: I know how to create a managed class which could be visible to .NET framework. I am not interested in doing this. I want for the C++ compiler to declare an unmanaged struct in a way that .NET will understand it. Something like:

[StructLayout(LayoutKind::Sequential, blablabla ... )]
struct MyStruct
{
    [MarshalAs ....... ]
    System::Int32 some_int;
    [MarshalAs ....... ]
    System::Int32 some_long;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-07-26 10:53:34

如果要从 C++ 托管代码定义对 .NET 应用程序可见的公共结构,可以使用以下方法(注意“public”和“value”关键字):

[StructLayout(LayoutKind::Sequential)]
public value struct MyStruct
{
    int some_int;
    long some_long;
};

如果希望在 C# 之间移动非托管结构和 C/C++,你必须在两边都声明它。 在 C 中就像这样:

struct MyUnmanagedStruct
{
    int some_int;
    long some_long;
};

在 C# 中,例如:

[StructLayout(LayoutKind.Sequential)]
struct MyUnmanagedStruct
{
    public int some_int;
    public long some_long; // Or as int (depends on 32/64 bitness)
};

您必须确保 .NET 中的字段与 C 中的字段匹配。您可能需要使用 StructLayout 属性(特别是 Pack)。 请参阅两个教程结构教程< /a>如何:使用 PInvoke 编组结构

If you want to define a public struct visible to .NET applications, from C++ managed code, here is a way to do it (note the 'public' and 'value' keywords):

[StructLayout(LayoutKind::Sequential)]
public value struct MyStruct
{
    int some_int;
    long some_long;
};

If you want an unmanaged struct to be moved between C# and C/C++, you'll have to declare it on both sides. Like this in C:

struct MyUnmanagedStruct
{
    int some_int;
    long some_long;
};

And like this, say, in C#, for example:

[StructLayout(LayoutKind.Sequential)]
struct MyUnmanagedStruct
{
    public int some_int;
    public long some_long; // Or as int (depends on 32/64 bitness)
};

You'll have to ensure the fields in .NET match the fields from C. You may want to use StructLayout attributes (notably Pack). See the two tutorials Structs Tutorial and How to: Marshal Structures Using PInvoke.

涫野音 2024-07-26 10:53:34

这个问题近两年被浏览了194次,不乏关注度。 把缺乏答案作为充分的证据,证明这是不可能的。

非托管结构必须进行封送,并且结构的 .NET 布局规则从根本上与 C 或 C++ 编译器的规则不兼容。 此答案提供了有关 .NET 为何以这种方式工作的一些背景信息。

This question was viewed 194 times in the past two years and has no lack of attention. Take the lack of answers as sufficient proof that this just is not possible.

An unmanaged struct has to be marshaled, and the .NET layout rules for structs are fundamentally incompatible with those for a C or C++ compiler. This answer provides some background information on why .NET works this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文