如何使用技术(描述)与净C结构和指向工作?如何使用、结构、工作、技术

2023-09-03 06:37:13 作者:如初太难

如何使用technique这里描述从.NET中使用C结构的工作?

How you use technique described here to work with C structures from .Net?

Ofcourse我需要一个code例子 - 3部分:C声明部分,C ++缠绕C和C#acsessing

Ofcourse I need a code example - on 3 parts: C declaring parts, C++ wrapping around C and C# acsessing.

所以,我奇怪的是

Çstructture A具有作为其PARAMS结构B,其由至少2种类型的其中一个是指向一些变量C应声明。之一

C structture A has as one of its params structure B which consists of at least 2 types one of which is pointer to some variable C which should be declared.

我们whant有从C#.NET访问所有的A和B的结构及其PARAMS和变量C。

We whant to have access from C# .Net to all A and B structures and its params and that variable C.

如何做这样的事?

推荐答案

假设这些都是C的结构在一个名为structs.h文件

Suppose these are the C structs in a file named structs.h

struct StructB
{
    int bMember1;
    int bMember2;
    int* bMember3;
};

struct StructA
{
    struct StructB aMember1;
};

在一个新的VC ++ DLL项目,使公共语言运行库支持(旧语法),并确保C ++异常将被禁用。建立此发布的目标。

In a new VC++ DLL project, enable Common Language RunTime Support (Old Syntax) and make sure C++ Exceptions are disabled. Build this for release target.

extern "C"
{
    #include "structs.h"
}

namespace Wrapper
{
    public __gc class NewStructB
    {
        private:
            StructB b;

        public:
            NewStructB()
            {
            }

            ~NewStructB()
            {
            }

            int getBMember1()
            {
                return b.bMember1;
            }

            void setBMember1(int value)
            {
                b.bMember1 = value;
            }

            int getBMember2()
            {
                return b.bMember2;
            }

            void setBMember2(int value)
            {
                b.bMember2 = value;
            }

            int* getBMember3()
            {
                return b.bMember3;
            }

            void setBMember3(int* value)
            {
                b.bMember3 = value;
            }
    };

    public __gc class NewStructA
    {
        public:
            NewStructB* b;

            NewStructA()
            {
                b = new NewStructB();
            }

            ~NewStructA()
            {
                delete b;
            }

            void ShowInfo()
            {
                System::Console::WriteLine(b->getBMember1().ToString());
                System::Console::WriteLine(b->getBMember2().ToString());
                System::Console::WriteLine((*b->getBMember3()).ToString());
            }
    };
};

然后创建一个新的C#控制台应用程序,并引用我们刚刚建好的.dll文件。在项目属性>构建,勾选允许不安全code。

Then create a new C# Console Application and reference the .dll file we just built. In Project Properties > Build, check "Allow unsafe code".

static void Main(string[] args)
{
    int toBePointed = 12345;

    Wrapper.NewStructA a = new Wrapper.NewStructA();

    a.b.setBMember1(10);
    a.b.setBMember2(20);

    unsafe
    {
        a.b.setBMember3(&toBePointed);
    }

    a.ShowInfo();

    Console.ReadKey();
}

正如你所看到的,原来StructA在某种程度上消除!而且我不知道直接从C#访问的C结构成员的任何其他方式,由于准入问题。

As you can see, the original StructA is in a way eliminated!! And I'm not aware of any other way to access C structure members directly from C# due to access issues.