欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# 调用Delphi dll 实例代码

时间:2021-11-12 09:16:42|栏目:.NET代码|点击:

delphi dll 源码:

复制代码 代码如下:

library dllres;

  type
     char10 = array[0..9] of char;
     TMydata = packed record
       id: Integer;
       name: char10;
       married: Boolean;
       salary: Double;
     end;
    PMydata = ^TMydata;

  const
    RESSTR: array[0..4] of string = ('HELLO', 'COLOR', 'DELPHI', 'shared', 'library');
    NO_RESULT=   'no result';
  var
   mydata: TMydata;

{$R *.res}
  // 返回字符串指针
  function  getResStr(aindex: Integer): PChar;  stdcall;
  begin
    if aindex < Length(RESSTR) then
    begin
      Result := pchar(RESSTR[aindex]);
    end
    else
    begin
      Result := pchar(NO_RESULT);
    end;
  end;

  // 返回结构体指针
  function getMydata: PMydata; stdcall;
  begin
    with mydata do
    begin
      id := 123;
      name := 'obama';
      married := false;
      salary := 1200;
    end;
    Result := @mydata;
  end;

exports   getResStr, getMydata;

begin
end.


C# 调用示例:

复制代码 代码如下:

class Invoke_Delphi_Dll_Exam
    {
        [DllImport("dllres.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr getResStr(int index);

        [DllImport("dllres.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern IntPtr getMydata();

        public struct Mydata
        {
            public int id; //0
            public string name; //4
            public bool married; //24
            public double salary; //25

            public Mydata(byte[] data)
            {
                if (data != null && data.Length == 33) {
                    id = BitConverter.ToInt32(data, 0);
                    name = Encoding.Unicode.GetString(data, 4, 20).Replace("\0",""); // 去掉尾部的0字符
                    married = BitConverter.ToBoolean(data, 24);
                    salary = BitConverter.ToDouble(data, 25);
                }
                else {
                    id = 0;
                    name = String.Empty;
                    married = false;
                    salary = 0;
                }
            }

            public override string ToString()
            {
                return String.Format("id: {0}, name: {1}, married: {2}, salary: {3}",
                    id, name, married, salary);
            }
        }

        private static void Main(string[] args)
        {
            Console.WriteLine(Marshal.PtrToStringAuto(getResStr(0)));

            byte[] data = new byte[33];
            Marshal.Copy(getMydata(), data, 0, 33);
            Mydata mydata = new Mydata(data);
            Console.WriteLine(mydata);
        }
    }

上一篇:asp.net后台如何动态添加JS文件和css文件的引用

栏    目:.NET代码

下一篇:C#编写ActiveX网页截图控件

本文标题:C# 调用Delphi dll 实例代码

本文地址:http://www.codeinn.net/misctech/183331.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有