Home > Net >  C # call c dynamic libraries
C # call c dynamic libraries

Time:01-07

C # call dynamic library of vc + +
Function prototypes: int Test (char * pcInput, char * pcOutput)
PcInput structure:
 
Typedef struct
{
Char transypte [2].
Char misNo [6].
Char amount [12].
Char cashier [8].
Char bar_code [50].
Char old_shop_code [50].
Char old_cardback [4].
Char old_trace [6].
Char old_reference [8].
Char old_date [8].
Char old_time [6].
Char TerNo [15].
Char MerchNo [12].
Char barcode_type [4].
} pcInput;

PcOutput structure:
 
Typedef struct
{
Char transypte [2].
Char misNo [6].
Char amount [12].
Char cashier [8].
Char resp_code [2].
Char rspinfo [100].
Char mask_cardno [19].
Char shop_code [50].
Char barcode_type [4].

Char pay_type;

Char trace [6].
Char reference [8].
Char date [8].
Char time [6].
Char batch [8].
Char TerNo [15].
Char MerchNo [12].
Char act_amt [12].
} OutPutData;


In c # statement
 
[DllImport (" test. DLL, "EntryPoint=" test ")]
Public static extern int Test ([MarshalAs (UnmanagedType. LPStr)] PcInfo data, [MarshalAs (UnmanagedType. LPStr)] ref OutData res);

PcInfo structure:
 
[StructLayout (LayoutKind. Sequential CharSet=CharSet. Ansi)]
Public struct PcInfo
{

[MarshalAs (UnmanagedType ByValArray, SizeConst=2)]
Public string transypte;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string misNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=12)]
Public string amount;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string cashier;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=50)]
Public string bar_code;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=50)]
Public string old_shop_code;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=4)]
Public string old_cardback;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string old_trace;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string old_reference;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string old_date;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string old_time;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=15)]
Public string TerNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=12)]
Public string MerchNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=4)]
Public string barcode_type;
}


OutData structure:
 
[StructLayout (LayoutKind. Sequential CharSet=CharSet. Ansi)]
Public struct OutData
{
[MarshalAs (UnmanagedType ByValTStr, SizeConst=2)]
Public string transypte;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string misNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=12)]
Public string amount;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string cashier;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=2)]
Public string resp_code;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=100)]
Public string rspinfo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=19)]
Public string mask_cardno;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=50)]
Public string shop_code;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=4)]
Public string barcode_type;

Public char pay_type;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string trace;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string reference.
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string date;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=6)]
Public string time;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=8)]
Public string batch;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=15)]
Public string TerNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=12)]
Public string MerchNo;
[MarshalAs (UnmanagedType ByValTStr, SizeConst=12)]
Public string act_amt;
}



Then call complains:

Answer, o what reason is this,,, no points

CodePudding user response:

Should be the parameter types do not match

CodePudding user response:

I change to public static extern int TransDo (IntPtr data, ref IntPtr res);

 
IntPtr infosIntptr=StructToIntPtr (oc);

Int sizeOut=Marshal. SizeOf (typeof (OutData));
IntPtr outptr=Marshal. AllocHGlobal (sizeOut);

Int x=TransDo (infosIntptr, ref outptr);

Var output=Marshal. PtrToStructure (outptr, typeof (OutData));

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related