下面的代码是用delphi写的dll,现在要用C#调用这个dll,并且要在一个按钮的单击事件中根据返回值判断是否成功,请问该如何实现,期待详细实现代码
一、 数据结构
1、商品记录结构
  TPluRecData = record
    Plu: Integer; 
    PluNoLen: Byte; 
    PluNo: array[0..9] of Byte;  
    Mark: Byte;  
    F1F2: array[0..1] of Byte;
    BarCodeLen: Byte; 
    BarCode: array[0..12] of Byte; 
    Slprc: Integer; 
    Csprc: Integer;    
    LabelFmt: Byte; 
    LabelFmt2: Byte;
    BcdFmt: Byte;
    BcdTyp: Byte;
    Addr: Byte;
    Tare: Word;
    Qty: Word;
    DptID: Word;
    SaleDate: Word; 
    SaleTime: Word;
    ValidDate: Word; 
    PackDate: Word; 
    PackTime: Word; 
    Trace: Word;
    PluNameLen: Word;
    PluFont: Byte;
    PluName: array[0..127] of Byte;
    IngreLen: Word;
    IngreFont: Byte;
    Ingre: array[0..127] of Byte; 
    DscLen: Word; 
    DscFont: Byte; 
    Dsc: array[0..127] of Byte;
    IS_PRINT_SaleDate: Boolean;
    IS_PRINT_SaleTime: Boolean;
    IS_PRINT_ValidDate: Boolean; 
    IS_PRINT_PackDate: Boolean;
    IS_PRINT_PackTime: Boolean;
    IS_PRINT_Trace: Boolean;
    IS_OPEN_Price: Boolean; 
    RsvData: array[0..63] of Byte;
  end;
  PPluRecData = ^TPluRecData;二、 接口函数说明
商品资料操作函数
function DB_Read_Plu_Record(IP: PChar; Plu: Integer; PPluRec: PPluRecData): Integer; stdcall; 
功能:获取指定的商品记录
说明:无
参数说明:IP为网络地址,例如192.168.0.16;
Plu为商品秤码;
PPluRec为商品资料记录结构
返回值:-1连接未建立 0成功 1失败(失败信息可通过DB_GetError获得)现在主要是不明白function DB_Read_Plu_Record(IP: PChar; Plu: Integer; PPluRec: PPluRecData): Integer; stdcall方法在事件中怎么用,PPluRec: PPluRecData 这个参数该怎么传?希望高手指点一下!谢谢!

解决方案 »

  1.   

    在c#中声明一样的struct,然后传递指针
      

  2.   

    以前研究过,现在忘干净了,给你点代码,自己研究吧
    ==============================================
    delphi 代码library Project2;uses
      Windows, SysUtils;type
      PStaff = ^TStaff;
      TStaff = record
        data1:integer;
        data2:integer;
      end;function RecordTest(MyTest:PStaff):boolean;stdcall;
    begin
      Result:=True;
      try
        MyTest^.data1 := MyTest^.data1 + 20;
        MyTest^.data2 := MyTest^.data2 + 30;
      except
        Result:=False;
      end;
    end;
    function StrLength(str: Pchar; var len: integer): integer; stdcall;
    begin
      Result := length(str);
      if result > len then len:=result;
    end;
    function GetString(): PWideChar;stdcall;
    var
      ResultValue : string;
    begin
      ResultValue := 'This is a string';
      Result := PWideChar(HeapAlloc(GetProcessHeap(),0, sizeof(WideChar) * Succ(Length(ResultValue)) ));
      StringToWideChar(ResultValue, Result, Succ(Length(ResultValue)));
    end;
    function UCase(str:PChar):boolean;stdcall;
    var
      s: string;
    begin
      s:=Uppercase(strPas(str));
      CopyMemory(str,pchar(s),Length(s));
      result:=true;
    end;exports
      RecordTest,
      StrLength,
      GetString,
      UCase;
    end.
    =====================================================
    c#代码using System.Runtime.InteropServices;
    public partial class _Default : System.Web.UI.Page 
    {
        public struct TStaff{
            public int data1;
            public int data2;
        }    [DllImport("D:\\My Documents\\Visual Studio 2008\\WebSites\\WebSite1\\bin\\Del_dll.dll")]
        public static extern Boolean RecordTest(ref TStaff Var);
        [DllImport("D:\\My Documents\\Visual Studio 2008\\WebSites\\WebSite1\\bin\\Del_dll.dll")]
        public static extern int StrLength(string str, ref int len);
        [DllImport("D:\\My Documents\\Visual Studio 2008\\WebSites\\WebSite1\\bin\\Del_dll.dll")]
        public static extern IntPtr GetString();
        [DllImport("D:\\My Documents\\Visual Studio 2008\\WebSites\\WebSite1\\bin\\Del_dll.dll")]
        public static extern bool UCase(System.Text.StringBuilder str);
        public struct SYSTEM_INFO
        {
            public uint dwOemId;
            public uint dwPageSize;
            public uint lpMinimumApplicationAddress;
            public uint lpMaximumApplicationAddress;
            public uint dwActiveProcessorMask;
            public uint dwNumberOfProcessors;
            public uint dwProcessorType;
            public uint dwAllocationGranularity;
            public uint dwProcessorLevel;
            public uint dwProcessorRevision;
        }
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern void GetSystemInfo(ref SYSTEM_INFO lpSystemInfo);    [DllImport("kernel32")]
        public static extern void GetWindowsDirectory(System.Text.StringBuilder WinDir, int count);     protected void Page_Load(object sender, EventArgs e)
        {
            // 测试 Application 变量
             Application.Lock();
            Application["id"] = Convert.ToInt32(Application["id"]) + 1;
            Application.UnLock();
            Response.Write("ID:" + Application["id"].ToString() + "<br />");        // 测试用结构类型调用 API
            SYSTEM_INFO pSI = new SYSTEM_INFO();
            GetSystemInfo(ref pSI);
            Response.Write( "核心数:" + pSI.dwNumberOfProcessors.ToString() + "<br />" );        // 测试字符串传回 取WINDOW路径
             System.Text.StringBuilder Buff = new System.Text.StringBuilder();
            GetWindowsDirectory(Buff, 128);
            Buff.AppendLine("abcde");
            Response.Write("WinDir:" +Buff.ToString() + "<br />");        // 自定义类型测试
             TStaff red = new TStaff();
            red.data1 = 10;
            red.data2 = 20;
            RecordTest(ref red);
            Response.Write("Record: date1=" + red.data1.ToString() + "  data2=" + red.data2.ToString() + "<br />");        // 测试返回值和通过参数返回值
             int sl_1,sl_2;
            sl_2 = 100;
            sl_1 = StrLength("aaaaaaaaaaaaaaaabbbbbccccccdddddddddddddddd", ref sl_2);
            Response.Write("Result&Ref: Rlt="+sl_1.ToString()+" ref="+sl_2.ToString() + "<br />");        // 测试返回字符串指针
             IntPtr chr = GetString();
            Response.Write("PWideChar:" + chr.ToString() + "<br />");
        
            // 测试字符串参数
             System.Text.StringBuilder l_str = new System.Text.StringBuilder("abcdefg hijklmn OPQRST uvwxyz");
            UCase(l_str);
            Response.Write("UCase:" + l_str.ToString() + "<br />");
        }
    }