Interop : Import Structure complexe C vers C#
Bonjour,
Je viens à vous car, malgré lecture de la FAQ sur les interop, je n'arrive pas à utiliser mes structures C dans mon programmes C#.
Voici le code C des structures :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| typedef enum
{
...
} PlayerStatus;
typedef enum
{
...
} STPlayerCommandType;
#pragma pack(1)
typedef struct
{
PlayerStatus Status; // The status of the command execution
char Message[2048]; // The message displayed into the console (including color tags)
union
{
struct // The extended info field (subject to evolution)
{
// Common use fields
unsigned long DataByteBufferLength; // The length of the content of following buffers
unsigned char DataByteBufferValue [512]; // The buffer containing byte values of data issued from the command execution
unsigned char DataByteBufferMask [512]; // The buffer containing byte mask of data issued from the command execution
unsigned long DataByteBufferDifferenceCount; // The number of differences in the buffer for comparison commands
unsigned long Value; // The standard single numeric value issued from the command execution
// Specialized fields :
// Reader's commands related fields
unsigned long RdRrc; // The reader return code
unsigned long RdSw1; // The transaction's status word byte 1
unsigned long RdSw2; // The transaction's status word byte 2
// Emulator's commands related fields
unsigned long EmuCpuRegPc; // The emulator's CPU register PC
unsigned long EmuCpuRegSp; // The emulator's CPU register SP
unsigned long EmuCpuRegAccu; // The emulator's CPU register ACCU
unsigned long EmuCpuRegX; // The emulator's CPU register X
unsigned long EmuCpuRegY; // The emulator's CPU register Y
unsigned long EmuCpuRegCsr; // The emulator's CPU register CSR
unsigned long EmuCpuRegDsr; // The emulator's CPU register DSR
unsigned long EmuCpuRegCc; // The emulator's CPU register CC
// Player helper fields
PlayerCommandType CommandType; // The type of the command as parsed by STPlayer
} Info;
char Reserved[2048]; // For internal use only
};
} PlayerAnswer, *PPlayerAnswer;
#pragma pack() |
Donc, pour créer le même type de structure en C#, je commence par créer mes énumérateurs, facilement.
Ensuite, je crée ma structure englobant l'autre ainsi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| // PlayerAnswer structure
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class PlayerAnswer
{
public PlayerStatus Status; // The status of the command execution
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
public char[] Message; // The message displayed into the console (including color tags)
public Info Info;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2048)]
public char[] Reserved;
} |
Puis la structure "Info" :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| // Info Structure
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class Info
{
// Common use fields
public ulong DataByteBufferLength; // The length of the content of following buffers
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public char[] DataByteBufferValue; // The buffer containing byte values of data issued from the command execution
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
public char[] DataByteBufferMask; // The buffer containing byte mask of data issued from the command execution
public ulong DataByteBufferDifferenceCount; // The number of differences in the buffer for comparison commands
public ulong Value; // The standard single numeric value issued from the command execution
// Specialized fields :
// Reader's commands related fields
public ulong RdRrc; // The reader return code
public ulong RdSw1; // The transaction's status word byte 1
public ulong RdSw2; // The transaction's status word byte 2
// Emulator's commands related fields
public ulong EmuCpuRegPc; // The emulator's CPU register PC
public ulong EmuCpuRegSp; // The emulator's CPU register SP
public ulong EmuCpuRegAccu; // The emulator's CPU register ACCU
public ulong EmuCpuRegX; // The emulator's CPU register X
public ulong EmuCpuRegY; // The emulator's CPU register Y
public ulong EmuCpuRegCsr; // The emulator's CPU register CSR
public ulong EmuCpuRegDsr; // The emulator's CPU register DSR
public ulong EmuCpuRegCc; // The emulator's CPU register CC
// Player helper fields
public PlayerCommandType CommandType; // The type of the command as parsed by Player
} |
Cette structure est utilisé par le biais de la méthode suivante :
Code:
1 2 3 4 5 6 7
| PlayerAnswer answer;
public int SendCommand()
{
commandState = P_Command(out answer, "AA_I2C_READ 02");
return commandState;
} |
Aucune erreur apparente, or les valeurs que je devrais obtenir dans ma structure Info ne sont pas celles souhaitées.
Ainsi, lorsque j'exécute cette commande, les variables de la structure Info devrait contenir ceci :
- CommandType = PlayerCommandType_Undetermined -> OK
- DataByteBufferDifferenceCount = 0 -> OK
- DataByteBufferLength = 0x0000000000000002
- DataByteBufferMask = 0xFF... -> OK
- DataByteBufferValue = 0xa0
Or,
- DataByteBufferLength = 0x000000a000000002
- DataByteBufferValue = ""
Savez-vous d'où viendrait le problème ? Peut-être un problème d'alignement ?
Cordialement,
Tehko