日期:2014-05-17  浏览次数:21339 次

C#调用Delphi dll
C# code

函数原型:
语法:
   function BatchReadRecord(hPort: THandle; var Records: array of TICRecord{; ArraySize: Integer}): Integer;

 参数说明:
   hPort: 端口句柄, 调用OpenCommPort函数得到,需要联机
   Records: 返回记录数组, 最少需要16个单元
   ArraySize: Delphi不需要,如果是VB或是C++需要传数组大小参数   
   TICRecord :结构中包含一条记录

//TICRecord结构体:
PICRecord=^TICRecord;
 TICRecord=packed record
    card: array[0..19] of char; 
    timeString: array[0..19] of char; 
    EmpId: array[0..9] of char;
    mark: Integer;                      
    flag: Integer;                      
    cardTimes: Integer;                 
    consume : WORD;                     
    balance : WORD;                    
 end;




C# code

//我在C#中的调用:
  [DllImport("EastRiver.dll", EntryPoint = "BatchReadRecord", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  public static extern IntPtr BatchReadRecord(IntPtr hPort, ref TICRecord[] array);

//定义结构体
 [StructLayout(LayoutKind.Sequential)] 
        public struct TICRecord
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public char[] Card;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public char[] TimeString;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
            public char[] EmpId;
            public int Mark;
            public int Flag;
            public int CardTimes;
            public int Consume;
            public int Balance;
        }

 private void btn_BatchReadRecord_Click(object sender, EventArgs e)
        {
            EastRiver.TICRecord[] array = new EastRiver.TICRecord[16];
            for (int i = 0; i < array.Length; i++)
            {
                EastRiver.TICRecord tr = array[i];
                tr.Card = new char[20];
                tr.TimeString = new char[20];
                tr.EmpId = new char[10];
                tr.Mark = 0;
                tr.Flag = 0;
                tr.CardTimes = 0;
                tr.Consume = 0;
                tr.Balance = 0;
            }
            IntPtr ret = EastRiver.BatchReadRecord(hPort,ref array);
           //这里报错:无法封送处理类型为“TICRecord”的字段“Card”: 无效的托管/非托管类型组合(数组字段必须与 ByValArray 或 SafeArray 成对出现)。
        }



请问大家这个问题如何解决?谢谢!

------解决方案--------------------
http://topic.csdn.net/u/20080407/17/f37fb593-d1b1-49b1-a0a5-5d62002e958c.html
------解决方案--------------------
C#哪里来char[]....
C#的struct不能像C++那样写
举个例子你好好看看
C# code
using   System.Runtime.InteropServices; 

[StructLayout(LayoutKind.Sequential,   CharSet=CharSet.Ansi,   Pack=1)]   
public   struct   TDataBrokerInputOptions 
{ 
[MarshalAs(UnmanagedType.ByValArray,   SizeConst=128)]   
public   byte[]   szProxyName   ; 

[MarshalAs(UnmanagedType.LPStr)]   
public   string   pszIPRestrictList; 
} 

[StructLayout(LayoutKind.Sequential,   CharSet=CharSet.Ansi,   Pack=1)]   
public   struct   TDataBrokerOptions 
{ 
[MarshalAs(UnmanagedType.LPStr)]   
public   string   pzServerType; 

public   IntPtr   zVSize; 
}

------解决方案--------------------
调用不起来。除非 Delphi 将函数声明为 stdcall 方式。
------解决方案--------------------
去掉诸如new char[20];的初始化试试
------解决方案--------------------
C# code

        //定义结构体
         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] 
        public struct TICRecord
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
            public string Card;
            [Mars