Autor Beitrag
Boldar
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 1555
Erhaltene Danke: 70

Win7 Enterprise 64bit, Win XP SP2
Turbo Delphi
BeitragVerfasst: Mo 26.07.10 10:53 
Hallo,
ich habe folgende Struktur in Delphi-Code, den ich nach C# übersetzen muss
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
type _USBCANSEND = record
    dwID:         dword;
    byNBytes:     BYTE;
    dwRX_TX_no:   dword;
    dat:          Array[0..7of Byte;
    end;


Wie mache ich ein stastisches array in ein struct?
Es geht um die Übergabe an eine dll, also müssen die Typen genau stimmen.
mfg Boldar
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Mo 26.07.10 11:36 
Hallo Boldar,

müsste meiner Meinung nach eigentlich so aussehen:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
public struct USBCANSEND
{
  public static uint dwID;
  public static byte byNBytes;
  public static uint dwRX_TX_no;
  public static byte[] dat;
            
  //oder gleich mit Initialisierung
  //public static byte[] dat = new byte[8];
}


Ein dword wäre laut meiner Auffassung ein uint. Habe aber auch schon irgendwo gesehen, dass es zu einem int übersetzt worden ist!?

Naja, kannst Du ja mal testen.

Grüsse, Marko
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4798
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Mo 26.07.10 11:48 
Das Stichwort heißt "Marshal" bzw "Marshalling".
Du erzeugst einfach eine Struktur in C# und verwendest dann das "MarshalAs"-Attribut für die Detailangaben, z.B.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
struct X
{
  public uint Id;
  public byte NBytes;
  public uint RX_TX_no;

  [MarshalAs(UnmanagedType.ByValArray, SizeConst=8)]
  public byte[] dat;
}

Es gibt auch noch ein paar weitere Attribute, z.B. "StructLayout" und "FieldOffset".