Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 949999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:29:39+00:00 2026-05-15T23:29:39+00:00

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

  • 0

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# acsessing.

So what I wonder Is

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.

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

How to do such thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:29:40+00:00Added an answer on May 15, 2026 at 11:29 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.