While using P/Invoke for c to c# and mashalling c struct to c# generates...

  • Thread starter Thread starter waheedkkj
  • Start date Start date
W

waheedkkj

Guest
I have to use a c++ dll in my project with P/Invoke im calling a function which involves a struct as well, which is generate AccessViolationException.

The specifics are below: following is the c++ function.


Int BII_Read_Transaction_Log_Ex(int iOption, int iUpdateFlag, int *iMaxEntries, BII_Transaction_Log_Ex *log)

Here is my method declaration.


[DllImport(@"C:\Program Files (x86)\Bioscrypt\SecureSDK\DLL\BII_V1100.dll", EntryPoint = "BII_Read_Transaction_Log_Ex")]
public unsafe static extern int GetTransactionLogs(int option, int updateFlag, ref int maxEnteries, ref BII_Transaction_Log_Ex[] transactionLogs);



It involves following struct.

typedef struct{
unsigned int id; // ID of the template whose action was recordedunsigned
char reserved_1;
unsigned char index; // Index of the template whose action was recorded
unsigned short year; // Years since 2002 action was recorded
unsigned char month; // Months since January action was recorded (0-11)
unsigned charunsigned char day; // Day of month action was recorded (1-31)
unsigned char hour; // Hour (0-23), Minute and Second (0-59) – Time of action
unsigned char min;
unsigned char sec;
unsigned char trans_code; // Trans_code, Data1, Data2, Data3 – See table below
unsigned char flag_port; //Bit 5. Bit 6 Bit 7 unused Bit 0-4: port (0 – host, 1 – aux, 3–Wiegand, 5 – GPI0, 6 – GPI1, 8 – Search)
unsigned char trans_log_data_1;
unsigned char trans_log_data_2;
unsigned char trans_log_data_3;
unsigned char reserved_2;
unsigned char status; //0 if action failed, 1 if action succeeded
char name[16]; // Name field of Template for which log is stored
unsigned short duration; // Enroll / Verify duration in milliseconds
unsigned short template_size; // Size of template in bytes for which log is stored
signed short error_code; // Error code if enroll / verify failed
unsigned char sensor_type; // Sensor type connected to device
unsigned char unit_id; // Unit ID assigned to device, same as net ID (Get/Set command for net ID BII_GET_NETID / BII_SET_NETID)
unsigned char admin_level; // Admin level of template for which log is stored
unsigned char rsz_3; /* Reserved */
unsigned char rsz_4; /* Reserved */
unsigned char rsz_5; /* Reserved */
char ta_msg[16]; /*F1,F2,F3,F4 key message */
} BII_Transaction_Log_Ex;


My implemenation of this struct in c# is following.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct BII_Transaction_Log_Ex
{
public UInt32 id; // ID of the template whose action was recorded
public Byte reserved_1;
public Byte index; // Index of the template whose action was recorded
public UInt16 year; // Years since 2002 action was recorded
public Byte month; // Months since January action was recorded (0-11)
unsigned char
public Byte day; // Day of month action was recorded (1-31)
public Byte hour; // Hour (0-23), Minute and Second (0-59) – Time of action
public Byte min;
public Byte sec;
public Byte trans_code; // Trans_code, Data1, Data2, Data3 – See table below
public Byte flag_port; //Bit 5. Bit 6 Bit 7 unused Bit 0-4: port (0 – host, 1 – aux, 3– Wiegand, 5 – GPI0, 6 – GPI1, 8 – Search)
public Byte trans_log_data_1;
public Byte trans_log_data_2;
public Byte trans_log_data_3;
public Byte reserved_2;
public Byte status; //0 if action failed, 1 if action succeeded
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] name; //char name[16]; // Name field of Template for which log is stored
public UInt16 duration; // Enroll / Verify duration in milliseconds
public UInt16 template_size; // Size of template in bytes for which log is stored
public SByte error_code; // Error code if enroll / verify failed
public Byte sensor_type; // Sensor type connected to device
public Byte unit_id; // Unit ID assigned to device, same as net ID (Get/Set command for net ID BII_GET_NETID / BII_SET_NETID)
public Byte admin_level; // Admin level of template for which log is stored
public Byte rsz_3; /* Reserved */
public Byte rsz_4; /* Reserved */
public Byte rsz_5; /* Reserved */
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] ta_msg; /*F1,F2,F3,F4 key message */
}


Now when i call the function using, it generates the AccessViolationException and shows following message: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

int transactionsCount=-1;
BII_Transaction_Log_Ex[] logs = new BII_Transaction_Log_Ex[1000];
int result = GetTransactionLogs(0, 0, ref transactionsCount, ref logs);




I think that im making some mistak in marshelling the struct involved, but so far im unable find out the exact problem. Any help would be appreciated..

Continue reading...
 
Back
Top