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

  • SEARCH
  • Home
  • 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 543213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:31:40+00:00 2026-05-13T10:31:40+00:00

I’m working with an unmanaged library through P/Invoke and it uses three structs (although

  • 0

I’m working with an unmanaged library through P/Invoke and it uses three structs (although they all have the same basic layout, so I’ll only post one):

struct Agraph_t {
    int tag:4;
    int kind:4;
    int handle:24;
    char **attr;
    char *didset;
    char *name;
    Agdata_t *univ;
    Dict_t *nodes, *inedges, *outedges;
    Agraph_t *root;
    Agnode_t *meta_node;
    Agproto_t *proto;
    Agraphinfo_t u;
};

Because of the way my wrapper uses these objects, I have to refer to structs inside Agraph_t as IntPtrs. I added properties that make accessing the values of the bit fields easier.

public struct Agraph_t {
    public uint tag_kind_handle;
    public IntPtr attr;
    public string didset;
    public string name;
    public IntPtr univ;
    public IntPtr nodes, inedges, outedges;
    public IntPtr root;
    public IntPtr meta_node;
    public IntPtr proto;
    public IntPtr u;

    public uint Tag {
        get { return (tag_kind_handle & 15u); }
    }

    public uint Kind {
        get { return (tag_kind_handle & 240u) / 16; }
    }

    public uint Handle {
        get { return (tag_kind_handle & 4294967040u) / 256; }
    }
}

Before doing anything, I have to initialize the unmanaged library by giving it the size of each of the three structs.

aginitlib(Marshal.SizeOf(typeof(Agraph_t)), ..., ...);

I don’t get an error when doing that and I can use the library just fine. However, part of the library makes its own call to aginitlib (I have no control over this) using the size of the unmanaged structs. At that point, the library warns me that it has been initialized with two different sizes, which is making it unstable (throwing AccessViolationExceptions after certain operations).

Are the added properties being factored into the size of the struct and making it larger than the unmanaged version? I would remove them and see what happens, but my code depends heavily on them, which makes that difficult.

Do I need to use StructLayoutAttribute with the Size property? The only thing that confuses me about that is the IntPtrs. The library is strictly 32-bit, so can I go ahead and safely assume that those fields will be 32 bits all the time?

  • 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-13T10:31:40+00:00Added an answer on May 13, 2026 at 10:31 am

    The difference is because of the declaration for u. The unmanaged declaration has this:

    Agraphinfo_t u;
    

    This means that an Agraphinfo_t is allocated inline in the Agraph_t struct. If Agraphinfo_t is, say, 16 bytes in size, then it contributes 16 bytes to sizeof(Agraph_t).

    However, in your managed declaration, you declare u like this:

    public IntPtr u;
    

    This means that a pointer is allocated in the Agraph_t struct. On a 32-bit system, this will contribute 4 bytes to sizeof(Agraph_t). Hence the two sizes computed for Agraph_t are out of sync.

    To fix the problem, declare a managed equivalent to Agraphinfo_t and create an instance of that in Agraph_t:

    public struct Agraphinfo_t
    {
      // fields go here as per unmanaged definition
    }
    
    public struct Agraph_t
    {
      // ....
      public Agraphinfo_t u;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Considering a function created with create_function this way : $func… May 14, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer I SD really there? Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) == true If targeting 1.6+,… May 14, 2026 at 4:29 pm
  • Editorial Team
    Editorial Team added an answer Both statements usually mean composition. But it seems to me… May 14, 2026 at 4:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.