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 7926965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:03:56+00:00 2026-06-03T19:03:56+00:00

There appears to be multiple ways to do this, but the examples I’ve tried

  • 0

There appears to be multiple ways to do this, but the examples I’ve tried haven’t worked for me.

I read somewhere that using unsafe pointers would be the way to go for more complex structures that require pointers. (sorry but I haven’t been able to find the source again)

My latest attempt looks something like this:

unsafe public struct ComplexStruct {
    public Int32 NumSubStruct;
    public SubStruct*[] arr;
}

unsafe public static extern UInt32 DLL_Call(ref ComplexStruct p);

function {
unsafe {
    ComplexStruct p = new ComplexStruct();
    p.NumSubStruct = 2;
    p.arr= new SubStruct*[p.NumSubStruct];
    SubStruct p1 = new SubStruct();
    SubStruct p2 = new SubStruct();
    p.arr[0] = &p1;
    p.arr[1] = &p2;


    testLogHandlerHandle = DLL_Call(ref p);
}
}

I get an error which says that SubStruct cannot be marshaled (Signature is not interop compatible).

Is it possible to pass the object without marshaling? (the DLL should be loaded in the same process space as the C# application).

If not, what would be the simplest way to pass a copy of the object?

Note: changing the DLL is a non-issue. I am vaguely aware of some other solutions, such as C++/CLI, but I only have a relatively small number of structure types that need to be passed in such a way.

EDIT:
A few notes:

  1. The array needs to by dynamic
  2. The actual structs a bit more complicated (there’s 4 layers of nested structures like this) so I think adding them would detract from the question, but for completeness, here is how I would declare these in C++:

    struct ComplexStruct {
    
         unsigned int NumSubStruct;
    
         SubStruct** arr;
    
    };
    
    struct SubStruct {
    
         unsigned int NumSubStruct;
    
         //some more datamembers here as well
    
         SubSubStruct** arr;
    
    };
    

(the extra indirection isn’t necessary, but I figured it might be useful to be able to declare the array of SubStruct pointers as IntPtrs)

  • 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-06-03T19:03:57+00:00Added an answer on June 3, 2026 at 7:03 pm

    You should use something like this and this.

    [StructLayout(LayoutKind.Sequential)]
    public struct ComplexStruct
    {
        public Int32 NumSubStruct;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NO_SUB_STRUCTS)]
        public SubStruct[] arr;
    }
    
    public static extern UInt32 DLL_Call(IntPtr p);
    

    Then marshal it with something like this:

    IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ComplexStruct)));
    
    try
    {
        // Copy the struct to unmanaged memory.
        Marshal.StructureToPtr(p, ptr, false);
    
        testLogHandlerHandle = DLL_Call(ptr);
    }
    finally
    {
        // Free the unmanaged memory.
        Marshal.FreeHGlobal(ptr);
    }
    

    Try to get rid of all the unsafe stuff, I doubt its necessary!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the answers to this question it appears there's a file somewhere on our
Is there an option to enable the drop down menu that appears when coding
Is there an event that fires in vb.net just before a contextMenuStrip appears when
It appears that ffmpeg now has a segmenter in it, or at least there
Okay everyone, I have found multiple ways of doing this and I have even
I know there are a few regex/ lastIndex discrepancies but this one is new
There are multiple related questions, but I'm looking for a solution specific to my
There are multiple related questions, but I'm looking for a solution specific to my
Because there are multiple ways of encoding special characters, in particular the ampersand, how
In CruiseControl.NET there appears to be the same xsl stylesheets in the server directory

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.