开发者

How do I convert this to c# (Marshalling)

开发者 https://www.devze.com 2023-02-22 10:18 出处:网络
I have these declarations (DLL) and tried to convert it in C# so i can call the functions from the DLL.

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;
    }
}
////////////////


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

精彩评论

暂无评论...
验证码 换一张
取 消