How you use technique (described) to work with C structures and pointers from .Net?

后端 未结 1 1474
不知归路
不知归路 2021-01-22 20:23

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

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

1条回答
  •  囚心锁ツ
    2021-01-22 20:46

    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;
    };
    

    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());
                }
        };
    };
    

    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();
    }
    

    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.

    0 讨论(0)
提交回复
热议问题