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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:51:52+00:00 2026-05-30T09:51:52+00:00

I have C dll with some complicated struct and I ma really a newbie

  • 0

I have C dll with some complicated struct and I ma really a newbie in C#:

typedef struct {
    int a;
    int b;
} simple_struct;

typedef struct {
    int d;
    int e;
    simple_struct f[20];
    short g;
    simple_struct h[20];
    short i;
} complex_struct;

The issue is that I am not able to interface my C# application with this structure!!

In the DLL there is a function GetData(complex_struct* myStruct) and I shoud call it from C#, so I created:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct simple_struct {
        public int a;
        public int b;
    } ;

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct complex_struct {
        public int d;
        public int e;
        public simple_struct[] f;
        public short g;
        public simple_struct[] h;
        public short i;
    } ;

but the problem is that when I pass complex_struct as argument of GetData, all the fields are filled back form me, but not my two array of simple_struct (I mean f and h)!! Their values are null!!

Can some one help me please, thanks


Hi and thanks for your reply,

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

This is my C sharp code:
namespace dll_test_import_c_sharp
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct simple_struct {
public int a;
public int b;
} ;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

I have done a lot of printf i GetData and all of them are well executed, it seems like the ‘return’ instruction crashes!!

I tried to call GetData by ref or by out and both of them don’t work…


Hi and thanks for your reply,

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

This is my C sharp code:
namespace dll_test_import_c_sharp
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct simple_struct {
public int a;
public int b;
} ;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

I have done a lot of printf i GetData and all of them are well executed, it seems like the ‘return’ instruction crashes!!

I tried to call GetData by ref or by out and both of them don’t work…

  • 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-30T09:51:54+00:00Added an answer on May 30, 2026 at 9:51 am

    You need to change the array definition on the struct to specify that it’s a by value / inline array

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct complex_struct {
        public int d;
        public int e;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public simple_struct[] f;
        public short g;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public simple_struct[] h;
        public short i;
    } ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dll that I distribute that will not run on some windows
I have DLL and application that will call some function in this dll. For
I have a SharePoint DLL that does some licensing things and as part of
I need some clarification. I have a Reportwriter dll that uses Crystal Reports. It
I have some binary files that I'd like to embed in a dll I'm
I have some dll files(not custom and not written by me) and I need
I know that some dll files have to be registered with the regsvr32 command.
I have a DLL file that has some helpful functions I want to call
I have a DLL, that Dll needs some configurations to work, mostly for WCF.
I have a C# dll that has some embedded resources and i'm having troubles

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.