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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:53:00+00:00 2026-05-21T04:53:00+00:00

I have these declarations (DLL) and tried to convert it in C# so i

  • 0

I have these declarations (DLL) and tried to convert it in C# so i can call the functions from the DLL.

Same for struct1 to struct3

typedef struct1  
{  
    int num;  
    char chars[25];  
    short shrt;  
    union  
    {  
         struct4 objstruct4;  
    }  
}  

typedef struct
{        
    Long Length;    
    short Type;    
    union    
    {
       struct1 objStruct1;      
       struct2 objStruct2;      
       struct3 objStruct3;    
    }Data;  
} Msg;

In C#, i converted these…same for struct1 to struct3

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct struct1  
{
    [MarshalAs(UnmanagedType.I4)]
    public Int32 num;  

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
    public string chars;  

    [MarshalAs(UnmanagedType.I2)]
    public short shrt;  

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
    public struct struct4
    {                
        [MarshalAs(UnmanagedType.Struct)]
        public ...
        ...
    }

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
protected struct Msg
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.I4)]
    public int Length;

    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.I2)]
    public short Type;

    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.Struct)]
    public Data MsgData;  
}

[StructLayout(LayoutKind.Explicit, Pack = 1, CharSet = CharSet.Ansi)]
public struct Data
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.Struct)]
    public struct1 objStruct1;      

    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.Struct)]
    public struct2 objStruct2;      

    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.Struct)]
    public struct3 objStruct3;    

}

Problem is when I tried to call a function from the DLL and passed the struct MSG as REF,
some member variables of the inner structs/union (struct1 to struct3) don’t have values.
Its like they are rumbled inside the memory…

But when I remove the struct1 or struct2 in struct MSG, all of the member variables inside the remaining inner structs/union were successfully retrieved.

Can I ask your advice if my conversion is correct or did i missed something…
Or is there a better way to convert this or you have answers why this problem occur.
One thing that I suspect is the size of the struct, ANSI or Unicode, and the arrangement of the variables, structs.

Please advise – thanks.

sample 2:
////////////////

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct struct1  
{
    [MarshalAs(UnmanagedType.I4)]
    public Int32 num1;  

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
    public string chars;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct struct2 
{
    [MarshalAs(UnmanagedType.I4)]
    public Int32 num2;  
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct struct3 
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = size + 1)]
    public string chars;

    [MarshalAs(UnmanagedType.I4)]
    public Int32 num3;  
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct struct4
{
    [MarshalAs(UnmanagedType.I4)]
    public Int32 num4;  

    [MarshalAs(UnmanagedType.Struct)]
    public Data DataMsg;

    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
    public struct Data
    {
      //[FieldOffSet(0)]
      public struct1 str1;

      //[FieldOffSet(0)]
      public struct2 str2;

      //[FieldOffSet(0)]
      public struct3 str3;
    }
}
////////////////
  • 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-21T04:53:00+00:00Added an answer on May 21, 2026 at 4:53 am

    There’s no obvious reason why you chose Pack=1, the default for most C compilers is 8, just like .NET. MsgData at offset 6 is iffy. Use sizeof and offsetof() in a test C program to find out where everything is located. Compare with Marshal.SizeOf and OffsetOf() in a test C# program. This isn’t going to work until they agree exactly.

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

Sidebar

Related Questions

I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
I have following typedef declarations in c but confused how I read these declarations.
These are my declarations. Why it doesn't identify signed as a type? I have
I have a dll function that takes BSTR parameters. These are cast as char*
I have a 3rd party Delphi DLL which I am calling from C++. Unfortunately,
I have these 3 icons enclosed in separate DIVs all of which are enclosed
I have these two modules : package G1; sub new { my $class =
I have these two classes public class Person { } public class Company {
I have these two situations: String s = aa; s = s + aa;
I have these lines in my ~/.inputrc : set editing-mode vi set keymap vi

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.