1

アンマネージ C++ dll で関数を呼び出そうとしています。

次のプロトタイプがあります。

  [DllImport("C:\\Program Files\\MySDK\\VSeries.dll", EntryPoint = "BII_Send_Index_Template_MT" )]
internal unsafe static extern Int32 BII_Send_Index_Template_MT(IntPtr pUnitHandle, ref BII_Template template, Int32 option, Boolean async);

  BII_Template template = new BII_Template();
  error_code = BII_Send_Index_Template_MT(pUnitHandle, ref template, option, false);

これは、C# で BII_Template 構造体を定義する方法です。

public unsafe struct BII_Template {
public ulong id;    
public ulong employee_id;  
public ulong password; 

public byte sensor_version;  
public byte template_version; 
public fixed char name[16];   
public byte finger;  
public byte admin_level;  
public byte schedule;  
public byte security_thresh; 
public fixed byte noise_level[18];   
public byte corramb;
public byte reference_x;
public byte reference_y;
public fixed byte ihcore[3];   
public fixed byte ivcore[3]; 
public byte temp_xoffset;  
public byte temp_yoffset;  
public byte index;    
public fixed byte inphase[5500]; 
};

ビルドして実行すると、dll が error_code = "The record checksum is invalid." を返します。

間違った方法で使用refしているか、構造体の一部の要素のサイズが間違っていると思います。

- - - 編集 - - - - - -

C++ の構造体は次のとおりです。

typedef struct {
unsigned long   id;     
unsigned long   employee_id;    
unsigned long   password;   
unsigned char   sensor_version;     
unsigned char   template_version;   
char        name[16];   
unsigned char   finger;     
unsigned char   admin_level;        
unsigned char   schedule;           
unsigned char   security_thresh;    
unsigned char   noise_level[18];    
unsigned char   corramb ;           
unsigned char   reference_x ;
unsigned char   reference_y ;
unsigned char   ihcore[NUM_CORE];
unsigned char   ivcore[NUM_CORE];   
unsigned char   temp_xoffset;       
unsigned char   temp_yoffset;       
unsigned char   index;              
unsigned char   inphase[PACKED_ARRAY_SIZE]; 
} BII_Template;
4

4 に答える 4

1

多くのテストを行った後、fixedchar...を使用しても[MashalAs...]と同じ結果が得られないことがわかりました。この変更を行った後、それは機能しました。

 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]

public struct BII_Template {public uint id; public uint employee_id; パブリックuintパスワード;

public byte sensor_version;
public byte template_version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] name;
public byte finger;

public byte admin_level;
public byte schedule;
public byte security_thresh;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
public byte[] noise_level;
public byte corramb;
public byte reference_x;
public byte reference_y;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ihcore;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ivcore;

public byte temp_xoffset;
public byte temp_yoffset;
public byte index;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5500)]
public byte[] inphase;

};

于 2009-12-08T16:18:02.357 に答える
0

C++ の「unsigned long」は、C# の ulong と等しくありません。

代わりに uint を使用してください。

于 2009-12-07T10:42:45.960 に答える
0

私の標準的な答えは、C++/CLI アセンブリを C# プロジェクトに追加することです。その後、問題は自然に解決します。

于 2009-12-07T09:53:43.983 に答える
-2

詳細については、BII_Template で structlayout.sequal プロパティを使用する必要があります。

于 2009-12-07T09:46:34.810 に答える