Click here to Skip to main content
14,391,880 members
Rate this:
Please Sign up or sign in to vote.
See more:
I developed a dll for specific calculations with matlab and compiled it with mcc.
One of the dlls functions uses pointer to records as result types. This works fine with delphi/pascal constructing records with the same field types as used in the C-Code of the dll. For the dll interface, I use pointer to this record types.

C inplementation of my result record
struct TRecordField {
        char Key[100];    
        byte TypID;          
        int ElemSize;       
        int Rows, Cols;     
        void *Buffer;
    };



Now I try to use the same dll in C#. The address of my result records is given to the dll function getResults with the & Operator, like in the code snippet beneath.
Only the char array 'Key' (represented as byte array in C#) of the records is correct filled after the dll call, numbers (of type int or byte) are always zero.

C# Dll function declaration
unsafe
	public static extern int getResults(TRecordField* Root);



C# struct implementation
"
[StructLayout(LayoutKind.Sequential)]
unsafe public struct TRecordField
{
  public fixed byte Key[100];  // --> this one is filled correct with chars from a 
                               //     string
  public byte TypId;   // all other values remain 0
  public int Elemsize;
  public int Rows, Cols;
  public void* Buffer;
}

TRecordField Root;


C# function call
int Erg = NativeMethods.getResults(&Root);


What I have tried:

I tried some things like "[StructLayout(LayoutKind.Sequential)]" and "[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]", but it didnt work.
Posted
Updated 3-Dec-19 4:10am
v2
Comments
Gerry Schmitz 3-Dec-19 12:04pm
   
Rearrange the fields in the record.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100