C# 调用Delphi dll 实例代码

时间:2023-06-12 22:46:06 

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);
        }
    }

标签:C#,Delphi,dll
0
投稿

猜你喜欢

  • idea在用Mybatis时xml文件sql不提示解决办法(提示后背景颜色去除)

    2023-11-09 01:45:51
  • C++中智能指针如何设计和使用

    2023-03-02 01:43:53
  • java sql ResultSet 之getRow()用法说明

    2022-07-01 02:43:15
  • Android利用AsyncTask异步类实现网页内容放大缩小

    2022-11-28 05:34:47
  • Spring Boot支持Crontab任务改造的方法

    2023-08-08 20:20:24
  • java生成json实现隐藏掉关键属性

    2021-12-07 17:31:29
  • Java日常练习题,每天进步一点点(32)

    2022-04-29 07:12:53
  • java实现简单的小超市程序

    2023-05-17 00:04:29
  • Java查找不重复无序数组中是否存在两个数字的和为某个值

    2023-08-22 16:44:40
  • Java通过底层原码了解数组拷贝

    2021-10-07 12:47:47
  • C# JsonHelper 操作辅助类,拿来直接用

    2023-11-28 18:55:42
  • 详解Java中的封装、继承、多态

    2022-09-24 06:26:23
  • c#获取存储过程返回值示例分享

    2021-08-24 18:45:48
  • C#不重复输出一个数组中所有元素的方法

    2022-07-02 14:25:58
  • spring mvc高级技术实例详解

    2022-10-11 12:50:15
  • redis与ssm整合方法(mybatis二级缓存)

    2022-02-27 22:14:27
  • WPF实现动画效果(六)之路径动画

    2022-02-05 01:42:15
  • Android开发之FloatingActionButton悬浮按钮基本使用、字体、颜色用法示例

    2022-12-21 05:38:26
  • Android4.0平板开发之隐藏底部任务栏的方法

    2023-11-29 15:11:44
  • 解决Spring Cloud feign GET请求无法用实体传参的问题

    2023-11-17 14:14:05
  • asp之家 软件编程 m.aspxhome.com