-1

我必须在我的项目中使用带有 P/Invoke 的 c++ dll 我正在调用一个涉及结构的函数,该函数生成 AccessViolationException。

具体如下:以下是c++函数。

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

这是我的方法声明。

[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);

它涉及以下结构。

typedef struct
{
unsigned int id; // ID of the template whose action was recorded
unsigned 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 char
unsigned 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;

我在 c# 中对这个结构的实现如下。

[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 */
   }

现在,当我调用该函数时,它会生成 AccessViolationException 并显示以下消息:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

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

我认为我在整理所涉及的结构时犯了一些错误,但到目前为止我无法找出确切的问题。任何帮助,将不胜感激..

编辑:我没有 C++ 源代码。

4

1 回答 1

0

C++ 函数声明如下:

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

最后一个参数可以是单个结构,通过引用传递。在这种情况下,C# 将是:

[DllImport(@"...", CallingConvention=CallingConvention.Cdecl,
    EntryPoint = "BII_Read_Transaction_Log_Ex")]
public static extern int GetTransactionLogs(
    int option, 
    int updateFlag, 
    ref int maxEntries, 
    ref BII_Transaction_Log_Ex transactionLogs
);

注意这里不需要不安全,所以我把它去掉了。它看起来很像 C++ 函数使用的cdecl.

但是,复数 intransactionLogsmaxEntries参数的使用让我怀疑 C++ 代码需要一个数组。在这种情况下,你写:

[DllImport(@"...", CallingConvention=CallingConvention.Cdecl,
    EntryPoint = "BII_Read_Transaction_Log_Ex")]
public static extern int GetTransactionLogs(
    int option, 
    int updateFlag, 
    ref int maxEntries, 
    [Out] BII_Transaction_Log_Ex[] transactionLogs
);

我也对你Pack=1在结构定义中的使用持怀疑态度。是什么让你这样做。我希望有一个对齐的结构,在这种情况下你应该删除Pack. 我认为这并不重要,因为 C++ 结构将保留成员放在将添加填充的空间中,但我仍然希望看到声明为对齐的结构。

于 2013-07-18T11:10:07.137 回答